Walk a directory/Recursively: Difference between revisions

Content added Content deleted
(→‎{{header|UNIX Shell}}: globstar and dotglob)
Line 1,532: Line 1,532:
The "find" command gives a one-line solution for simple patterns:
The "find" command gives a one-line solution for simple patterns:


<lang bash>find . -name '*.txt'</lang>
<lang bash>find . -name '*.txt' -type f </lang>


"find" can also be used to find files matching more complex patterns as illustrated in the section on [[#UnixPipes|Unix Pipes]] below.
"find" can also be used to find files matching more complex patterns as illustrated in the section on [[#UnixPipes|Unix Pipes]] below.

Using "bash" version 4 or later, you can use "globstar" or "dotglob", depending on whether you want hidden directories to be searched:

<lang bash>#! /bin/bash
# Warning: globstar excludes hidden directories.
# Turn on recursive globbing (in this script) or exit if the option is not supported:
shopt -s globstar || exit

for f in **
do
if [[ "$f" =~ \.txt$ ]] ; then
echo "$f"
fi
done

</lang>



Here is a solution that does not use "find".
Here is a solution that does not use "find".