From man find
:
The find utility recursively descends the directory tree for each path listed, evaluating an expression … in terms of each file in the tree.
Prerequisites
Given the following directory tree:
tree
.
├── file1.txt
├── file2.txt
├── folder
│ └── file3
└── tmp
└── file4
2 directories, 4 files
To recreate the directory layout, run the command:
mkdir -p folder tmp && touch file1.txt file2.txt folder/file3 tmp/file4
Examples
find
To list all files and directories in your current working directory:
find .
Output:
.
./file2.txt
./file1.txt
./folder
./folder/file3
./tmp
./tmp/file4
To list all files and directories in folder
:
find folder
Output:
folder
folder/file3
-type
To list only files:
find . -type f
Output:
./file2.txt
./file1.txt
./folder/file3
./tmp/file4
To list only directories:
find . -type d
Output:
.
./folder
./tmp
To list all files in folder
:
find folder -type -f
Output:
folder/file3
-name
To list all files with the extension .txt
:
find . -type f -name '*.txt'
Output:
./file2.txt
./file1.txt
Which is the same as:
find . -type f -name \*.txt
-path
To list all files excluding those in tmp
:
find . -type f ! -path './tmp/*'
Output:
./file2.txt
./file1.txt
./folder/file3
To list all files excluding those in folder
and tmp
:
find . -type f ! -path './tmp/*' ! -path './folder/*'
Output:
./file2.txt
./file1.txt
-or
To test against multiple conditions:
find . -name '*.txt' -or -name '*tmp*'
Output:
./file2.txt
./file1.txt
./tmp
This is the same as:
find . -name '*.txt' -o -name '*tmp*'
The following test operators are available to be used:
-and
-or
-not
-exec
To execute a command for each match:
find . -name '*.txt' -exec echo {} \;
Output:
./file2.txt
./file1.txt
The {}
is the argument and \;
is to ensure the semicolon is escaped.
This is the same as:
echo ./file2.txt && echo ./file1.txt
To execute a single command for all matches:
find . -name '*.txt' -exec echo {} +
Output:
./file2.txt ./file1.txt
This is the same as:
echo ./file2.txt ./file1.txt
Resources
Here’s an article with more usage examples of the find
command.