Introduction

Searching for a particular file in your system can be such a hassle if you do not have the knowledge of the right tools to use to ease your work. Maybe there is that config file you are not sure where to find it and you are left to search for it. The find command will help you find it.

This command simply searches for files in a directory hierarchy. It is a powerful command and can search for files based on various criteria such as permissions, modification time, ownership, size. It can also find files and modify them such as changing their permissions and even deleting them.

The simplified syntax goes like this

$ find [options] [path or directory…] [your-expression]

Examples of using find command

1. Search files with given extension

Search all files with .pdf extension on the /home/tech/ directory

$ find /home/tech/ -name "*.pdf"

Search all files with .pdf extension on the current directory

$ find . -name "*.pdf"

2. Search by type using the -type option

You can specify multiple parameters using the -type option as follows:

  • f: To search for a regular file
  • d: To search for a directory
  • l: To search for a symbolic link
  • c: To search for character devices
  • b: To search for block devices
  • p: To search for a named pipe (FIFO)
  • s: To search for a socket

Search multiple directories for a file httpd.conf

$ find /home /etc /var -name httpd.conf -type f 

Find all directories in root(/) with the name django.

$ find / -type d -name django

3. Find files with specific permissions

This command will find files with mode 644 and prints them out.

$ find /home -type f -perm 0644 -print

4. Locate files based on size

To find files with size bigger than 10 Mebibyte and sort them by size:

find . -size +10M -type f -print0 | xargs -0 ls -Ssh | sort -z

Manipulating files

Find files and directories with a given permission and changing the permissions

## Find files with 755 mode and change to 777
find /home -type f -perm 0755 -print -exec chmod 777 {} \;

## For Directories
find /home -type d -perm 0755 -print -exec chmod 777 {} \;

Find and delete files based on search criteria.

find /etc -type f -name "*.pdf" -exec rm -f {} \;
find ./path/ -name '*.txt' -exec rm '{}' \;

Find files based on the time it was modified

find /etc/selinux -name "*.conf" -mtime 5

The above finds files with names that end with the characters conf in the specified directory and have been modified in the previous 5 days.

Find and delete files compressed files in /data directory older than 7 days.

find /data/*.gz -mtime +7 -exec rm -rf {} \;

Find and delete files and directories

find /var -name "*.backup" -delete
find / -type f -name *.txt -exec rm {} \;
find / -type d -name flask -exec rm -rf {} \; ## directory

Find and delete empty directories in the current working parent directory.

find . -type d -empty -exec rmdir {} \;

Find files by ownership

Find all files owned by tech user

find /home -user tech

Find pdf files owned by user tech in the /var directory

find /var -user tech -iname "*.pdf"

Find all files owned by accounts group

find /home -group accounts

Finding files based on their sizes

The find command can search and find files when you specify the sizes you desire. To find all 70MB files in /var directory, do

find /var -size 70M

Since you can match files with their sizes, you can also delete them as follows

find /var -size +70M -exec rm -rf {} \;

Conclusion

There you go guys. Now you can be able to find your files with ease and achieve even more using the utility. Thank you for reading through and we hope it was informative.

Similar:

How to extract .xz files on Linux – CenOS / Ubuntu / Debian

How to use scp command to securely transfer files with examples

Lnav – Easy way to check Log files on Linux

How to empty (truncate) Log files in Linux

Learning:

Best Linux Books for Beginners & Experts