Find Command In Linux
The very popular Linux admins friend find command in Linux was created as a part of the Unix operating system by Ken Thompson and Dennis Ritchie at Bell Labs in the golden era of new computer development in the 1970s. Thompson and Ritchie were two of the pioneer developers of the Unix operating system and also developed the mother of most programming language C programming language, C is mostly used for developing software for Unix-based systems. The find command in Linux and Unix operating systems is still widely used today for searching for files and directories on a system and performing operations on them.
Some Alternatives of Find Command
Related Post: MV Command
15 Linux Commands to Boost Productivity
There are only locate is main alternatives to the find command in Linux, which gives it some competition but we have added 4 more alternatives that can be useful in specific tasks.
locate: The locate command is the fastest way to search for files and directories on a Linux system. Unlike the find command, locate command creates its database of file paths that are generated periodically, so it provides faster results than find but it may not always be up to date. It will not come default in the system we have to install it and before first use has to update the Database of locate command. Update duration will depend upon your system file number.
grep: The grep command is an awesome tool for searching for patterns in text files. It can also be used to search for files and directories by name, although it is not as versatile as the find command in Linux because it searches only in text files.
whereis: The whereis command in Linux is used to locate the binary, source, and manual page files for a specified command. It is a simple but useful tool that provides basic information about a command, including its all locations on the system. for example command may be in /bin/ /sbin/ or /usr/bin/ it will show all.
which: The which command is in Linux is also used to locate the binary for a specified command. Unlike whereis, it only provides the location (bin /usr/bin, etc) of the executable file for the command for your bash profile and does not provide information about its source code or manual pages.
findmnt: The findmnt command in Linux is used to find and display information about mounted file systems on a Linux and Unix system. It provides information about the mounted file system type, source, and target mounted on for each mounted file system.
These are some alternatives but the find command is still on top because it gives you real-time all details of the file and provides the ability to perform operations even in the script you can use the find command to get currently accurate results and perform operations on the right file.
Top 100 Use Cases of Find Command
Please find below examples
SN | Use | Command |
1 | Find files in a specific directory: | find /path/to/directory -type f |
2 | Find directories in a specific directory: | find /path/to/directory -type d |
3 | Find files with a specific name: | find /path/to/directory -name “file_name” |
4 | Find files with a specific extension: | find /path/to/directory -name “*.extension” |
5 | Find files based on their size: | find /path/to/directory -size +10M |
6 | Find files based on their modification time: | find /path/to/directory -mtime +7 |
7 | Find files based on their access time: | find /path/to/directory -atime +7 |
8 | Find files based on their inode number: | find /path/to/directory -inum 123456 |
9 | Find files that are readable: | find /path/to/directory -perm -444 |
10 | Find files that are writable: | find /path/to/directory -perm -222 |
11 | Find files that are executable: | find /path/to/directory -perm -111 |
12 | Find files that are owned by a specific user: | find /path/to/directory -user user_name |
13 | Find files that are owned by a specific group: | find /path/to/directory -group group_name |
14 | Find files with a specific user ID: | find /path/to/directory -uid 123456 |
15 | Find files with a specific group ID: | find /path/to/directory -gid 123456 |
16 | Find files that match a specific pattern: | find /path/to/directory -regex “.*pattern.*” |
17 | Find files that contain a specific word or phrase: | find /path/to/directory -type f -exec grep -q “word or phrase” {} \; -print |
18 | Find files that are symbolic links: | find /path/to/directory -type l |
19 | Find files that are hard links: | find /path/to/directory -type f -links +1 |
20 | Find files that are empty: | find /path/to/directory -empty |
21 | Find files that are not empty: | find /path/to/directory ! -empty |
22 | Find files and execute a specific command on each file: | find /path/to/directory -type f -exec command {} \; |
23 | Find files and move them to a new location: | find /path/to/directory -type f -exec mv {} /path/to/new/location \; |
24 | Find files and delete them | find /path/to/directory -type f -exec rm -i {} \; |
25 | Find files and copy them to a new location: | find /path/to/directory -type f -exec cp {} /path/to/new/location \; |
26 | Find files and change the permissions on each file: | find /path/to/directory -type f -exec chmod 755 {} \; |
27 | Find files and change the owner of each file: | find /path/to/directory -type f -exec chown user:group {} \; |
28 | Find files and change the group of each file: | find /path/to/directory -type f -exec chgrp group_name {} \; |
29 | Find files and print the full path of each file: | find /path/to/directory -type f -printf “%p\n” |
30 | Find files and print the size of each file: | find /path/to/directory -type f -printf “%s\n” |
31 | Find files and print the inode number of each file: | find /path/to/directory -type f -printf “%i\n” |
32 | Find files and print the modification time of each file: | find /path/to/directory -type f -printf “%T@\n” |
33 | Find files and print the user name of the owner of each file: | find /path/to/directory -type f -printf “%u\n” |
34 | Find files and print the group name of the owner of each file: | find /path/to/directory -type f -printf “%g\n” |
35 | Find files and print the permissions of each file: | find /path/to/directory -type f -printf “%M\n” |
36 | Find files and execute a command with a specific time limit: | find /path/to/directory -type f -mmin +5 -exec command {} \; |
37 | Find files and execute a command only if the file has been modified in the last 5 minutes: | find /path/to/directory -type f -mmin -5 -exec command {} \; |
38 | Find files and execute a command only if the file has not been modified in the last 5 minutes: | find /path/to/directory -type f ! -mmin -5 -exec command {} \; |
39 | Find files and search for a specific string in each file: | find /path/to/directory -type f -exec grep “string” {} \; |
40 | Find files and list only the first 10 files that match a specific criteria: | find /path/to/directory -type f -print | head -10 |
41 | Find files and list only the last 10 files that match a specific criteria: | find /path/to/directory | head 10 |
42 | Find files and exclude a specific directory from the search: | find /path/to/directory -type f -not -path “/path/to/directory/to/exclude/*” -print |
43 | Find files and exclude multiple directories from the search: | find /path/to/directory -type f \( -not -path “/path/to/directory/to/exclude/*” -and -not -path “/path/to/directory/to/exclude2/*” \) -print |
44 | Find files and search for files with a specific name pattern: | find /path/to/directory -type f -name “*.txt” |
45 | Find files and search for files with a specific size: | find /path/to/directory -type f -size +100M |
46 | Find files and search for files smaller than a specific size: | find /path/to/directory -type f -size -100M |
47 | Find files and search for files larger than a specific size: | find /path/to/directory -type f -size +100M |
48 | Find files and search for files of an exact size: | find /path/to/directory -type f -size 100M |
49 | Find files and search for files with a specific user owner: | find /path/to/directory -type f -user user_name |
50 | Find files and search for files with a specific group owner: | find /path/to/directory -type f -group group_name |
51 | Find files and search for files with a specific type of permissions: | find /path/to/directory -type f -perm 644 |
52 | Find files and search for files that are writable: | find /path/to/directory -type f -perm -222 |
53 | Find files and search for files that are readable: | find /path/to/directory -type f -perm -444 |
54 | Find files and search for files that are executable: | find /path/to/directory -type f -perm -111 |
55 | Find files and search for files with specific type of executability: | find /path/to/directory -type f -perm /u=x,g=x |
56 | Find files and search for files that have changed in the last 7 days: | find /path/to/directory -type f -ctime -7 |
57 | Find files and search for files that have changed more than 7 days ago: | find /path/to/directory -type f -ctime +7 |
58 | Find files and search for files that have been modified in the last 7 days: | find /path/to/directory -type f -mtime -7 |
59 | Find files and search for files that have been modified more than 7 days ago: | find /path/to/directory -type f -mtime +7 |
60 | Find files and search for files that have been accessed in the last 7 days: | find /path/to/directory -type f -atime -7 |
61 | Find files and search for files that have not been accessed in the last 7 days: | find /path/to/directory -type f -atime +7 |
62 | Find files and search for files that match multiple criteria: | find /path/to/directory -type f \( -name “*.txt” -or -name “*.pdf” \) -and -size +100M |
63 | Find files and delete the files that match certain criteria: | find /path/to/directory -type f -name “*.bak” -delete |
64 | Find files and print the output in a specific format: | find /path/to/directory -type f -printf “%p %u %g %s\n” |
65 | Find files and execute a specific command on the matching files: | find /path/to/directory -type f -exec chmod 644 {} \; |
66 | Find files and limit the number of results to display: | find /path/to/directory -type f -print | head -10 |
67 | Find files and sort the results by file size: | find /path/to/directory -type f -printf “%s %p\n” | sort -nr |
68 | Find files and exclude specific files or directories from the results: | find /path/to/directory -type f \( ! -name “*.bak” ! -path “/path/to/directory/to/exclude/*” \) -print |
69 | Find files and search for files based on their inode number: | find /path/to/directory -type f -inum 123456 |
70 | Find files and search for files based on their access time and modification time: | find /path/to/directory -type f -anewer file.txt -or -mnewer file.txt |
71 | Find files and search for files with a specific type of type: | find /path/to/directory -type l -ls |
72 | Find files and search for files with a specific type of type and perform an action on them: | find /path/to/directory -type l -exec ls -l {} \; |
73 | Find files and search for files based on the number of links: | find /path/to/directory -type f -links +5 |
74 | Find files and search for files based on the number of links and perform an action on them: | find /path/to/directory -type f -links +5 -exec ls -l {} \; |
75 | Find files and search for files based on the type of file system they are on: | find /mnt/c/ -type f -fstype xfs |
76 | Find files and search for files with a specific owner: | find /path/to/directory -type f -user username |
77 | Find files and search for files with a specific group: | find /path/to/directory -type f -group groupname |
78 | Find files and search for files with a specific permissions: | find /path/to/directory -type f -perm 0644 |
79 | Find files and search for files with a specific name and perform a certain action on them: | find /path/to/directory -type f -name “*.log” -exec gzip {} \; |
80 | Find files and search for files that match a certain pattern and perform a certain action on them: | find /path/to/directory -type f -regex “.*\.\(jpg\|jpeg\|png\)” -exec cp {} /path/to/destination/ \; |
81 | Find files and search for files that have been modified in the last 7 days and perform a certain action on them: | find /path/to/directory -type f -mtime -7 -exec rm {} \; |
82 | Find files and search for files that have a specific size and perform a certain action on them: | find /path/to/directory -type f -size +100M -exec mv {} /path/to/destination/ \; |
83 | Find files and search for files that have a specific size range and perform a certain action on them: | find /path/to/directory -type f -size +10M -size -100M -exec cp {} /path/to/destination/ \; |
84 | Find files and search for files that have a specific size range and print the results in a specific format: | find /path/to/directory -type f -size +10M -size -100M -printf “%p %u %g %s\n” |
85 | Find files and search for files that have a specific name and move them to a specific location: | find /path/to/directory -type f -name “*.log” -exec mv {} /path/to/destination/ \; |
86 | Find files and search for files that have a specific name and copy them to a specific location: | find /path/to/directory -type f -name “*.log” -exec cp {} /path/to/destination/ \; |
87 | Find files and search for files that have a specific name and count the number of matches: | find /path/to/directory -type f -name “*.log” | wc -l |
88 | Find files and search for files that have a specific name and delete the matches: | find /path/to/directory -type f -name “*.log” -delete |
89 | Find files and search for files that have a specific name and rename the matches: | find /path/to/directory -type f -name “*.log” -exec mv {} {}.bak \; |
90 | Find files that have been modified in the last day and delete them: | find /path/to/directory -type f -mtime -1 -delete |
91 | Find files that have been modified in the last 7 days and compress them: | find /path/to/directory -type f -mtime -7 -exec gzip {} \; |
92 | Find files that have a specific name and show the details in a human-readable format: | find /path/to/directory -type f -name “*.log” -exec stat -c “%n %s %y” {} \; |
93 | Find all symbolic links and delete them: | find /path/to/directory -type l -delete |
94 | Find all symbolic links and replace them with their targets: | find /path/to/directory -type l -exec sh -c ‘ln -sf “$(readlink -f “$1″)” “$1″‘ _ {} \; |
95 | Find all symbolic links that point to a specific location and delete them: | find /path/to/directory -type l -lname “/path/to/link/target” -delete |
96 | Find all files that are larger than a specific size and delete them: | find /path/to/directory -type f -size +100M -delete |
97 | Find mp3 files in ext4 filesystem | find /mnt/usb/ -type f -fstype ext4 | grep -i ‘\.mp3$’ |
98 | Find files that have been modified in the last hour and move them to a different location: | find /path/to/directory -type f -mmin -60 -exec mv {} /path/to/destination/ \; |
99 | Find commnad in linux with sed command . Find file and replace text mylinux to explinux | find my_files -type f -name ‘*.txt’ -exec sed -i ‘s/mylinux/explinux/g’ {} \; |
100 | Find process pid status and print from proc | find /proc/1234 -maxdepth 0 -name status -o -name stat -exec cat {} \; |
Feel free to provide you advice and a new or effective way to use the find command in Linux.
Conclusion:
Here we have seen 100 use cases of the find command in Linux and this can be used in a script or in daily use of system admin and troubleshooting tasks using the find command.