The find command is used to locate files and directories in the file system. It offers more search options compared to the locate command, including:
- Search only files
- Search only directories
- Search by name
- Search by size
- Use search results with other commands
- Restrict search to a maximum depth
$ findThis command will find all files and directories in the current working directory and below in the Linux file system. This is the default behavior.
You can find all files and directories in a specified directory and its subdirectories with the following commands:
$ find /dev
$ find /etcBy default, the find command searches through all depth levels. However, you can limit the search to a specific depth using the -maxdepth option.
Desktop
|- file1.txt
|- level_1_dir
|- file2.txt
|- level_2_dir
|- file3.txt
|- level_3_dir
|- file4.txt
|- level_4_dir
|- file5.txt
To create this structure, you can use:
$ mkdir -p level_1_dir/level_2_dir/level_3_dir/level_4_dir
$ touch file1.txt level_1_dir/file2.txt level_1_dir/level_2_dir/file3.txt level_1_dir/level_2_dir/level_3_dir/file4.txt level_1_dir/level_2_dir/level_3_dir/level_4_dir/file5.txtOutput
akshay@myubuntu:~/Desktop$ mkdir -p level_1_dir/level_2_dir/level_3_dir/level_4_dir
akshay@myubuntu:~/Desktop$ tree
.
└── level_1_dir
└── level_2_dir
└── level_3_dir
└── level_4_dir
5 directories, 0 files
akshay@myubuntu:~/Desktop$ touch file1.txt level_1_dir/file2.txt level_1_dir/level_2_dir/file3.txt level_1_dir/level_2_dir/level_3_dir/file4.txt level_1_dir/level_2_dir/level_3_dir/level_4_dir/file5.txt
akshay@myubuntu:~/Desktop$ tree
.
├── file1.txt
└── level_1_dir
├── file2.txt
└── level_2_dir
├── file3.txt
└── level_3_dir
├── file4.txt
└── level_4_dir
└── file5.txt
5 directories, 5 filesExecute the following commands to see the effect of maxdepth:
-
$ find . -maxdepth 1 -
$ find . -maxdepth 2 -
$ find . -maxdepth 3 -
$ find . -maxdepth 4 -
$ find . -maxdepth 100
Output
akshay@myubuntu:~/Desktop$ find . -maxdepth 1
.
./level_1_dir
./file1.txt
akshay@myubuntu:~/Desktop$ find . -maxdepth 2
.
./level_1_dir
./level_1_dir/file2.txt
./level_1_dir/level_2_dir
./file1.txt
akshay@myubuntu:~/Desktop$ find . -maxdepth 3
.
./level_1_dir
./level_1_dir/file2.txt
./level_1_dir/level_2_dir
./level_1_dir/level_2_dir/level_3_dir
./level_1_dir/level_2_dir/file3.txt
./file1.txt
akshay@myubuntu:~/Desktop$ find . -maxdepth 4
.
./level_1_dir
./level_1_dir/file2.txt
./level_1_dir/level_2_dir
./level_1_dir/level_2_dir/level_3_dir
./level_1_dir/level_2_dir/level_3_dir/level_4_dir
./level_1_dir/level_2_dir/level_3_dir/file4.txt
./level_1_dir/level_2_dir/file3.txt
./file1.txt
akshay@myubuntu:~/Desktop$ find . -maxdepth 5
.
./level_1_dir
./level_1_dir/file2.txt
./level_1_dir/level_2_dir
./level_1_dir/level_2_dir/level_3_dir
./level_1_dir/level_2_dir/level_3_dir/level_4_dir
./level_1_dir/level_2_dir/level_3_dir/level_4_dir/file5.txt
./level_1_dir/level_2_dir/level_3_dir/file4.txt
./level_1_dir/level_2_dir/file3.txt
./file1.txt
-
The
maxdepthoption requires a single dash (-), not a double dash (--):-maxdepth→ Valid--maxdepth→ Invalid
-
The
findcommand can also locate hidden files and directories.
You can use the -type option with the find command to search for specific types of files:
-type f→ Find only files-type d→ Find only directories
$ find -type fOutput might include:
./level_1_dir/file2.txt
./level_1_dir/level_2_dir/file3.txt
./level_1_dir/level_2_dir/level_3_dir/level_4_dir/file5.txt
./level_1_dir/level_2_dir/level_3_dir/file4.txt
./file1.txt
./.securefile1.txt
$ find -type dOutput might include:
.
./.db_info
./level_1_dir
./level_1_dir/level_2_dir
./level_1_dir/level_2_dir/level_3_dir
./level_1_dir/level_2_dir/level_3_dir/level_4_dir
You can use these options simultaneously, but the order matters. Always specify -maxdepth before -type:
-
$ find -type f -maxdepth 2 # Generates a warning -
$ find -maxdepth 2 -type f # No warnings
To find files and directories by name, use the -name option.
-
Create test files:
$ touch {A..D}.txt $ touch {A,B}{A,B}.txt -
Search for files by name:
$ find . -name 'A.txt'
-
Use wildcards for more complex searches:
$ find . -name '?.txt' $ find . -name '??.txt' $ find . -name '*.txt' $ find . -maxdepth 2 -name '*.txt'
If you want to ignore case when searching, use the -iname option:
$ find -iname 'a.txt'To find files based on their size, you can use the -size option with the find command.
+n→ greater than n-n→ less than nn→ exactly n-empty→ find empty files or directories
-
List all file names where size is over 200 KB:
$ find / -type f -size +200k
This command requires root privileges.
To count the files, use:
$ sudo find / -type f -size +200k | wc -l -
List all file names where size is over 200 KB but less than 4 MB:
$ find / -type f -size +200k -size -4M | wc -l -
List all file names where file size is less than 200 KB or more than 4 MB:
$ find / -type f -size -200k -o -size +4M | wc -lHere,
-omeans "or."
$ find / -type f -size -200k -size +4M | wc -lThe answer should be: 0
You can perform operations on the results of the find command using the -exec option.
-
Example: To copy all files in the
/etcfolder where file size is less than 2 KB to thedir1directory on the Desktop:$ find /etc -type f -size -2k -exec cp {} dir1 \;Here,
{}is a placeholder for the found files.If you want confirmation before executing the command, use the
-okoption instead:$ find /etc -type f -size -2k -ok cp {} dir1 \;
This will prompt you for confirmation before each copy operation.