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
based on other answers and comment of @phk, using fd #3:
(which still allows to use stdin inside the loop)
while IFS= read -r f <&3; do
echo "$f"
done 3< <(find . -iname "*filename*")
What ever you do, don't use a for
loop:
# Don't do this
for file in $(find . -name "*.txt")
do
…code using "$file"
done
Three reasons:
find
must run to completion.for
loop returns 40KB of text. That last 8KB will be dropped right off your for
loop and you'll never know it.Always use a while read
construct:
find . -name "*.txt" -print0 | while read -d $'\0' file
do
…code using "$file"
done
The loop will execute while the find
command is executing. Plus, this command will work even if a file name is returned with whitespace in it. And, you won't overflow your command line buffer.
The -print0
will use the NULL as a file separator instead of a newline and the -d $'\0'
will use NULL as the separator while reading.
How about if you use grep instead of find?
ls | grep .txt$ > out.txt
Now you can read this file and the filenames are in the form of a list.
(Updated to include @Socowi's execellent speed improvement)
With any $SHELL
that supports it (dash/zsh/bash...):
find . -name "*.txt" -exec $SHELL -c '
for i in "$@" ; do
echo "$i"
done
' {} +
Done.
Original answer (shorter, but slower):
find . -name "*.txt" -exec $SHELL -c '
echo "$0"
' {} \;
find <path> -xdev -type f -name *.txt -exec ls -l {} \;
This will list the files and give details about attributes.
I like to use find which is first assigned to variable and IFS switched to new line as follow:
FilesFound=$(find . -name "*.txt")
IFSbkp="$IFS"
IFS=$'\n'
counter=1;
for file in $FilesFound; do
echo "${counter}: ${file}"
let counter++;
done
IFS="$IFSbkp"
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)