26 examples of find commands in Linux

In this guide let us see some of the very useful real-time required find commands in our daily operations, Most of below examples are very useful for newbies to prepare them to be familiar with find commands. Open a terminal and run below commands manually without copy-paste to get familiar with it.

find command is just a single command but come with hundreds of options and arguments required to run during our requirement.

Find for Files and Directories

To list all files which named as test in current working directory. The “.” after the find command represent current directory you are in.

# find . -type f -name "*test*"

To list the directory with name as “audit” in current working directory. The “d” at the end of command represent the type as directory.

# find . -name audit -type d

To list only the files with .log extension. We are finding files inside the directory /var/log and looking only for “-f” files. By following executing the command “ls -lthr” to list all the files inside /var/log/.

# find /var/log/ -name "*.log" -type f -exec ls -lthr {} \;

To list the empty directories

# find /var/log/ -empty -type d

To list the empty files

# find /var/log/ -empty -type f

Using “Find” for time based search

To list all the .log extension files which modified older than 7 days.
The “-mtime” represent modified days before.

# find /var/log/ -name "*.log" -type f -mtime +7 -exec ls -lthr {} \;

To list only the files which last changed in n*24 hours ago. Changes done before number of days.

# find /var/log/ -name "*.log" -type f -ctime +7 -exec ls -lthr {} \;

To list only the files which last accessed n*24 hours ago. The file which accessed before two days back.

# find /var/log/ -name "*.log" -type f -atime +2 -exec ls -lthr {} \;

To list the file was last accessed 5 minutes ago

# find /var/log/ -amin +5 -type f

To list the file last modified 5 minutes ago

# find /var/log/ -mmin +5 -type f

To list the file last changed 5 minutes ago

# find /var/log/ -cmin +5 -type f

Listing files recursively with modified time

# find /root/ -type f -print0 | xargs -0 ls -l --time-style="+%F %T"

Using Find to looks for User/Group owned files/Dir

To list the files which are owned by mysql user.

# find /var/log/ -type f -user puppet -exec ls -lthr {} \;

To list the all files which are owned by mysql user and group.

# find / -type f -user puppet -group puppet -exec ls -lthr {} \;

To list the files below the levels of directories below the starting-points for user and group puppet.

# find / -maxdepth 4 -type f -user puppet -group puppet -exec ls -lthr {} \;

List all files which owned by 1000 UID and GID under home directory.

# find /home/ -uid 1000 -gid 1000 -type f

Looking for Size based Files/Dir.

Check that no large files exist on the file system.

# find / -xdev -type f | cut -d '/' -f 2 | sort | uniq -c | sort -n

To list large file more than below size. You can replace 100000 with 10 Megabyte, MB or 1 G for 1 Gigabyte, GB.

# find . -xdev -size +1000000 -exec ls -lthr {} \;

Find and Remove

To list only the deleted open files (Very Important one).

# find /proc/*/fd -ls | grep '(deleted)'

Find and remove the empty files in current directory.

# find . -empty -type f -delete

Find and remove only the extension with .log which are empty.

# find /var/log/ -empty -type f -name *.log -delete

To list file and confirm before removing it.

# find /var/log/ -name *.log -type f -ok rm -rfv {} \;

Finding for Permission based

List files which are with executable permission

# find /tmp/ -executable -type f

To list only the files with executable permission.

# find /tmp/ -perm 700 -type f

To list all the block devices

# find /dev/ -type b

To list the files of users with user and group write access.

# find /home/ -perm /u+w,g+w

We will update this topic with more examples soon.

Conclusion:

Every Linux admin should be familiar with find command and it’s options. In all case, during daily operations and troubleshooting this command is mostly very helpful to locate any files and configurations.