x=$(find . -name "*.txt")
echo $x
if I run the above piece of code in Bash shell, what I get is a string containing several file names separated by blank, not a list.
Of course, I can further separate them by blank to get a list, but I'm sure there is a better way to do it.
So what is the best way to loop through the results of a find
command?
Filenames can include spaces and even control characters. Spaces are (default) delimiters for shell expansion in bash and as a result of that
x=$(find . -name "*.txt")
from the question is not recommended at all. If find gets a filename with spaces e.g."the file.txt"
you will get 2 separated strings for processing, if you processx
in a loop. You can improve this by changing delimiter (bashIFS
Variable) e.g. to\r\n
, but filenames can include control characters - so this is not a (completely) safe method.From my point of view, there are 2 recommended (and safe) patterns for processing files:
1. Use for loop & filename expansion:
2. Use find-read-while & process substitution
Remarks
on Pattern 1:
nullglob
can be used to avoid this extra line.failglob
shell option is set, and no matches are found, an error message is printed and the command is not executed." (from Bash Manual above)globstar
: "If set, the pattern ‘**’ used in a filename expansion context will match all files and zero or more directories and subdirectories. If the pattern is followed by a ‘/’, only directories and subdirectories match." see Bash Manual, Shopt Builtinextglob
,nocaseglob
,dotglob
& shell variableGLOBIGNORE
on Pattern 2:
filenames can contain blanks, tabs, spaces, newlines, ... to process filenames in a safe way,
find
with-print0
is used: filename is printed with all control characters & terminated with NUL. see also Gnu Findutils Manpage, Unsafe File Name Handling, safe File Name Handling, unusual characters in filenames. See David A. Wheeler below for detailed discussion of this topic.There are some possible patterns to process find results in a while loop. Others (kevin, David W.) have shown how to do this using pipes:
When you try this piece of code, you will see, that it does not work:files_found
is always "true" & the code will always echo "no files found". Reason is: each command of a pipeline is executed in a separate subshell, so the changed variable inside the loop (separate subshell) does not change the variable in the main shell script. This is why I recommend using process substitution as the "better", more useful, more general pattern.See I set variables in a loop that's in a pipeline. Why do they disappear... (from Greg's Bash FAQ) for a detailed discussion on this topic.
Additional References & Sources:
Gnu Bash Manual, Pattern Matching
Filenames and Pathnames in Shell: How to do it Correctly, David A. Wheeler
Why you don't read lines with "for", Greg's Wiki
Why you shouldn't parse the output of ls(1), Greg's Wiki
Gnu Bash Manual, Process Substitution
You can store your
find
output in array if you wish to use the output later as:Now to print the each element in new line, you can either use
for
loop iterating to all the elements of array, or you can use printf statement.or
You can also use:
This will print each filename in newline
To only print the
find
output in list form, you can use either of the following:or
This will remove error messages and only give the filename as output in new line.
If you wish to do something with the filenames, storing it in array is good, else there is no need to consume that space and you can directly print the output from
find
.I like to use find which is first assigned to variable and IFS switched to new line as follow:
Just in case you would like to repeat more actions on the same set of DATA and find is very slow on your server (I/0 high utilization)
With any
$SHELL
that supports it (sh/bash/zsh/...):Done.
Assuming you don't have filenames with embedded newlines, you can get a list like this:
As other people have pointed out, whether this is useful depends on the context.
based on other answers and comment of @phk, using fd #3:
(which still allows to use stdin inside the loop)