Using the “find” command we can search for files in a directory hierarchy.
Basic Usage
Use the command “find”, without any parameter, and it will show all files in the directory. It will also show the directories-
$ find
.
./a
./a/z
./a/my_file_in_a.txt
./a/b
./a/b/c
./a/b/c/d
./file3
./file2
./bigboxcode2.txt
./k
./k/my_file_in_k.txt
./bigboxcode.txt
./file1
./file4
Bash“find *” will give us the same output.
Find File using Name Pattern
$ find bigboxcode*
bigboxcode.txt
bigboxcode2.txt
BashCase-Insensitive File Search
Use the “-iname” param for case insensitive search for file names.
$ find -iname Big*
./bigboxcode2.txt
./bigboxcode.txt
$ find -iname *COde*
./bigboxcode2.txt
./bigboxcode.txt
BashSearch File with Specific Extension
Let’s find all files that have “.txt” extension.
$ find *.txt
bigboxcode.txt
bigboxcode2.txt
BashSearch File with Single Character Pattern
Use question mark(?) to replace one character in the file name pattern.
$ find bigboxcode?.txt
bigboxcode2.txt
bigboxcode3.txt
BashUse a question mark(?) multiple times to indicate multiple characters in the pattern-
$ find bigboxcode??.txt
bigboxcode11.txt
bigboxcode12.txt
BashSearch File in Specific Directory
$ find /home/bigboxcode bigboxcode*
/home/bigboxcode
/home/bigboxcode/a
/home/bigboxcode/a/z
/home/bigboxcode/a/my_file_in_a.txt
/home/bigboxcode/a/b
/home/bigboxcode/a/b/c
/home/bigboxcode/a/b/c/d
/home/bigboxcode/file3
/home/bigboxcode/file2
/home/bigboxcode/bigboxcode2.txt
/home/bigboxcode/k
/home/bigboxcode/k/my_file_in_k.txt
/home/bigboxcode/bigboxcode12.txt
/home/bigboxcode/bigboxcode.txt
/home/bigboxcode/file1
/home/bigboxcode/bigboxcode3.txt
/home/bigboxcode/file4
/home/bigboxcode/bigboxcode11.txt
bigboxcode.txt
bigboxcode11.txt
bigboxcode12.txt
bigboxcode2.txt
bigboxcode3.txt
BashSet Max Depth for Search
$ find ~ bigboxcode* -maxdepth 1
BashFind File with Size Limit
We can define the Max or Min size limit for files while searching for files. The size can be defined using the “-size” option and size options can be a 512-byte block(b), byte(c), two-byte word(w), kilobyte(k), megabytes(M), gigabytes(G).
Set the max limit using the Plus(+) sign.
$ find * -size +1k
a
a/z
a/b
a/b/c
a/b/c/d
k
BashSet the min limit using the Minus(-) sign.
$ find * -size -1k
a/my_file_in_a.txt
bigboxcode11.txt
bigboxcode12.txt
bigboxcode3.txt
file1
file2
file3
file4
k/my_file_in_k.txt
Bash