Recursively iterate through files in a directory

user2064000

Recursively iterating through files in a directory can easily be done by:

find . -type f -exec bar {} \;

However, the above does not work for more complex things, where a lot of conditional branches, looping etc. needs to be done. I used to use this for the above:

while read line; do [...]; done < <(find . -type f)

However, it seems like this doesn't work for files containing obscure characters:

$ touch $'a\nb'
$ find . -type f
./a?b

Is there an alternative that handles such obscure characters well?

l0b0

Yet another use for safe find:

while IFS= read -r -d '' -u 9
do
    [Do something with "$REPLY"]
done 9< <( find . -type f -exec printf '%s\0' {} + )

(This works with any POSIX find, but the shell part requires bash. With *BSD and GNU find, you can use -print0 instead of -exec printf '%s\0' {} +, it will be slightly faster.)

This makes it possible to use standard input within the loop, and it works with any path.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Execute files looping recursively through a directory with an exclusion

From Dev

Shell Script to Recursively Loop Through Directory and print location of important files

From Dev

Shell Script to Recursively Loop Through Directory and print location of important files

From Dev

Recursively iterate through a Scala list

From Dev

Qt 4.7 - How to iterate through files in an FTP directory

From Dev

How to iterate through every directory specified and run commands on files (Python)

From Dev

How to iterate through directory and move particular files to a different folder

From Dev

How to iterate through subdirectories in a directory and count the files in the subdirectories in python

From Dev

PowerShell: Iterate Through Directory List, Copy Files with Duplicate Handling

From Dev

Iterate through files

From Dev

Iterate through settings files

From Dev

Iterate through settings files

From Java

Recursively iterate through all subdirectories using pathlib

From Dev

Recursively iterate through all subdirectories using pathlib

From Dev

How to recursively iterate through ViewParent tree?

From Java

Recursively counting files in a Linux directory

From Dev

Recursively find the number of files in a directory

From Dev

NodeJS hash files recursively in a directory

From Dev

Recursively find the number of files in a directory

From Dev

Recursively process files in a directory in unix

From Dev

How do I copy files recursively through a directory tree while appending parent folder names to the new files? Batch or VBScript

From Dev

How do I copy files recursively through a directory tree while appending parent folder names to the new files? Batch or VBScript

From Dev

How to recursively loop through a directory on a specific filetype?

From Dev

Recursively iterating through directory using QDirIterator

From Dev

How to iterate through files in SAS?

From Dev

Trying to iterate through CSV files in a directory and grab a particular cell then post results to a single csv

From Dev

How to iterate over files in a directory?

From Dev

Iterate large set of files in a directory

From Dev

How to iterate over files in a directory?

Related Related

  1. 1

    Execute files looping recursively through a directory with an exclusion

  2. 2

    Shell Script to Recursively Loop Through Directory and print location of important files

  3. 3

    Shell Script to Recursively Loop Through Directory and print location of important files

  4. 4

    Recursively iterate through a Scala list

  5. 5

    Qt 4.7 - How to iterate through files in an FTP directory

  6. 6

    How to iterate through every directory specified and run commands on files (Python)

  7. 7

    How to iterate through directory and move particular files to a different folder

  8. 8

    How to iterate through subdirectories in a directory and count the files in the subdirectories in python

  9. 9

    PowerShell: Iterate Through Directory List, Copy Files with Duplicate Handling

  10. 10

    Iterate through files

  11. 11

    Iterate through settings files

  12. 12

    Iterate through settings files

  13. 13

    Recursively iterate through all subdirectories using pathlib

  14. 14

    Recursively iterate through all subdirectories using pathlib

  15. 15

    How to recursively iterate through ViewParent tree?

  16. 16

    Recursively counting files in a Linux directory

  17. 17

    Recursively find the number of files in a directory

  18. 18

    NodeJS hash files recursively in a directory

  19. 19

    Recursively find the number of files in a directory

  20. 20

    Recursively process files in a directory in unix

  21. 21

    How do I copy files recursively through a directory tree while appending parent folder names to the new files? Batch or VBScript

  22. 22

    How do I copy files recursively through a directory tree while appending parent folder names to the new files? Batch or VBScript

  23. 23

    How to recursively loop through a directory on a specific filetype?

  24. 24

    Recursively iterating through directory using QDirIterator

  25. 25

    How to iterate through files in SAS?

  26. 26

    Trying to iterate through CSV files in a directory and grab a particular cell then post results to a single csv

  27. 27

    How to iterate over files in a directory?

  28. 28

    Iterate large set of files in a directory

  29. 29

    How to iterate over files in a directory?

HotTag

Archive