find . -name "*.txt"|while read fname; do
echo "$fname"
done
Note: this method and the (second) method shown by bmargulies are safe to use with white space in the file/folder names.
In order to also have the - somewhat exotic - case of newlines in the file/folder names covered, you will have to resort to the -exec
predicate of find
like this:
find . -name '*.txt' -exec echo "{}" \;
The {}
is the placeholder for the found item and the \;
is used to terminate the -exec
predicate.
And for the sake of completeness let me add another variant - you gotta love the *nix ways for their versatility:
find . -name '*.txt' -print0|xargs -0 -n 1 echo
This would separate the printed items with a \0
character that isn't allowed in any of the file systems in file or folder names, to my knowledge, and therefore should cover all bases. xargs
picks them up one by one then ...