100 Best Use Cases of Find Command in Linux -ExpLinux

Find Command In Linux

Find Command in Linux
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

22 Most Useful Linux Commands

15 Linux Commands to Boost Productivity

Master Guide to Vi Editor

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

SNUseCommand
1Find files in a specific directory:find /path/to/directory -type f
2Find directories in a specific directory:find /path/to/directory -type d
3Find files with a specific name:find /path/to/directory -name “file_name”
4Find files with a specific extension:find /path/to/directory -name “*.extension”
5Find files based on their size:find /path/to/directory -size +10M
6Find files based on their modification time:find /path/to/directory -mtime +7
7Find files based on their access time:find /path/to/directory -atime +7
8Find files based on their inode number:find /path/to/directory -inum 123456
9Find files that are readable:find /path/to/directory -perm -444
10Find files that are writable:find /path/to/directory -perm -222
11Find files that are executable:find /path/to/directory -perm -111
12Find files that are owned by a specific user:find /path/to/directory -user user_name
13Find files that are owned by a specific group:find /path/to/directory -group group_name
14Find files with a specific user ID:find /path/to/directory -uid 123456
15Find files with a specific group ID:find /path/to/directory -gid 123456
16Find files that match a specific pattern:find /path/to/directory -regex “.*pattern.*”
17Find files that contain a specific word or phrase:find /path/to/directory -type f -exec grep -q “word or phrase” {} \; -print
18Find files that are symbolic links:find /path/to/directory -type l
19Find files that are hard links:find /path/to/directory -type f -links +1
20Find files that are empty:find /path/to/directory -empty
21Find files that are not empty:find /path/to/directory ! -empty
22Find files and execute a specific command on each file:find /path/to/directory -type f -exec command {} \;
23Find files and move them to a new location:find /path/to/directory -type f -exec mv {} /path/to/new/location \;
24Find files and delete themfind /path/to/directory -type f -exec rm -i {} \;
25Find files and copy them to a new location:find /path/to/directory -type f -exec cp {} /path/to/new/location \;
26Find files and change the permissions on each file:find /path/to/directory -type f -exec chmod 755 {} \;
27Find files and change the owner of each file:find /path/to/directory -type f -exec chown user:group {} \;
28Find files and change the group of each file:find /path/to/directory -type f -exec chgrp group_name {} \;
29Find files and print the full path of each file:find /path/to/directory -type f -printf “%p\n”
30Find files and print the size of each file:find /path/to/directory -type f -printf “%s\n”
31Find files and print the inode number of each file:find /path/to/directory -type f -printf “%i\n”
32Find files and print the modification time of each file:find /path/to/directory -type f -printf “%T@\n”
33Find files and print the user name of the owner of each file:find /path/to/directory -type f -printf “%u\n”
34Find files and print the group name of the owner of each file:find /path/to/directory -type f -printf “%g\n”
35Find files and print the permissions of each file:find /path/to/directory -type f -printf “%M\n”
36Find files and execute a command with a specific time limit:find /path/to/directory -type f -mmin +5 -exec command {} \;
37Find 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 {} \;
38Find 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 {} \;
39Find files and search for a specific string in each file:find /path/to/directory -type f -exec grep “string” {} \;
40Find files and list only the first 10 files that match a specific criteria:find /path/to/directory -type f -print | head -10
41Find files and list only the last 10 files that match a specific criteria: find /path/to/directory | head 10
42Find files and exclude a specific directory from the search:find /path/to/directory -type f -not -path “/path/to/directory/to/exclude/*” -print
43Find 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
44Find files and search for files with a specific name pattern:find /path/to/directory -type f -name “*.txt”
45Find files and search for files with a specific size:find /path/to/directory -type f -size +100M
46Find files and search for files smaller than a specific size:find /path/to/directory -type f -size -100M
47Find files and search for files larger than a specific size:find /path/to/directory -type f -size +100M
48Find files and search for files of an exact size:find /path/to/directory -type f -size 100M
49Find files and search for files with a specific user owner:find /path/to/directory -type f -user user_name
50Find files and search for files with a specific group owner:find /path/to/directory -type f -group group_name
51Find files and search for files with a specific type of permissions:find /path/to/directory -type f -perm 644
52Find files and search for files that are writable:find /path/to/directory -type f -perm -222
53Find files and search for files that are readable:find /path/to/directory -type f -perm -444
54Find files and search for files that are executable:find /path/to/directory -type f -perm -111
55Find files and search for files with specific type of executability:find /path/to/directory -type f -perm /u=x,g=x
56Find files and search for files that have changed in the last 7 days:find /path/to/directory -type f -ctime -7
57Find files and search for files that have changed more than 7 days ago:find /path/to/directory -type f -ctime +7
58Find files and search for files that have been modified in the last 7 days:find /path/to/directory -type f -mtime -7
59Find files and search for files that have been modified more than 7 days ago:find /path/to/directory -type f -mtime +7
60Find files and search for files that have been accessed in the last 7 days:find /path/to/directory -type f -atime -7
61Find files and search for files that have not been accessed in the last 7 days:find /path/to/directory -type f -atime +7
62Find files and search for files that match multiple criteria:find /path/to/directory -type f \( -name “*.txt” -or -name “*.pdf” \) -and -size +100M
63Find files and delete the files that match certain criteria:find /path/to/directory -type f -name “*.bak” -delete
64Find files and print the output in a specific format:find /path/to/directory -type f -printf “%p %u %g %s\n”
65Find files and execute a specific command on the matching files:find /path/to/directory -type f -exec chmod 644 {} \;
66Find files and limit the number of results to display:find /path/to/directory -type f -print | head -10
67Find files and sort the results by file size:find /path/to/directory -type f -printf “%s %p\n” | sort -nr
68Find 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
69Find files and search for files based on their inode number:find /path/to/directory -type f -inum 123456
70Find 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
71Find files and search for files with a specific type of type:find /path/to/directory -type l -ls
72Find 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 {} \;
73Find files and search for files based on the number of links:find /path/to/directory -type f -links +5
74Find 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 {} \;
75Find files and search for files based on the type of file system they are on:find /mnt/c/ -type f -fstype xfs
76Find files and search for files with a specific owner:find /path/to/directory -type f -user username
77Find files and search for files with a specific group:find /path/to/directory -type f -group groupname
78Find files and search for files with a specific permissions:find /path/to/directory -type f -perm 0644
79Find 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 {} \;
80Find 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/ \;
81Find 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 {} \;
82Find 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/ \;
83Find 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/ \;
84Find 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”
85Find 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/ \;
86Find 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/ \;
87Find 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
88Find files and search for files that have a specific name and delete the matches:find /path/to/directory -type f -name “*.log” -delete
89Find 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 \;
90Find files that have been modified in the last day and delete them:find /path/to/directory -type f -mtime -1 -delete
91Find files that have been modified in the last 7 days and compress them:find /path/to/directory -type f -mtime -7 -exec gzip {} \;
92Find 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” {} \;
93Find all symbolic links and delete them:find /path/to/directory -type l -delete
94Find all symbolic links and replace them with their targets:find /path/to/directory -type l -exec sh -c ‘ln -sf “$(readlink -f “$1″)” “$1″‘ _ {} \;
95Find all symbolic links that point to a specific location and delete them:find /path/to/directory -type l -lname “/path/to/link/target” -delete
96Find all files that are larger than a specific size and delete them:find /path/to/directory -type f -size +100M -delete
97Find mp3 files in ext4 filesystemfind /mnt/usb/ -type f -fstype ext4 | grep -i ‘\.mp3$’
98Find 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/ \;
99Find commnad in linux with sed command . Find file and replace text mylinux to explinuxfind my_files -type f -name ‘*.txt’ -exec sed -i ‘s/mylinux/explinux/g’ {} \;
100Find process pid status and print from proc find /proc/1234 -maxdepth 0 -name status -o -name stat -exec cat {} \;
Find Command in Linux – explinux

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.

Leave a Comment

Your email address will not be published. Required fields are marked *