How to search file(s) in linux / unix?

find is very famous and regularly used command to find files in the Linux/UNIX filesystem based on various different conditions. Let us review some practical examples of find command. All system administrators love this command, sometimes a life saver..
Syntax:

find [pathnames] [conditions]

How to find files containing a particular / specific word in its name?
The following command looks for all the files under /etc directory with cron
in the filename.

# find /etc -name "*cron*"

How to find all the files greater than certain size?
The following command will list all the files in the system greater than
10MB.

# find / -type f -size +10M

How to find files that are not modified in the last x number of days?
The following command will list all the files that were modified more than 30
days ago under the current directory.

# find . -mtime +30

How to find files that are modified in the last x number of days?
The following command will list all the files that were modified in the last
five days under the current directory.

# find . –mtime -5

How to delete all the archive files with extension *.tar.gz and greater than 50MB?
Please be careful while executing the following command as you don’t want
to delete the files by mistake. The best practice is to execute the same
command with ls –l to make sure you know which files will get deleted when
you execute the command with rm.

# find / -type f -name *.tar.gz -size +50M -exec ls -l {} \;
# find / -type f -name *.tar.gz -size +50M -exec rm -f {} \;

How to archive all the files that are not modified in the last x
number of days?

The following command finds all the files not modified in the last 30 days
under /home/nilesh directory and creates an archive files under /tmp in the
format of ddmmyyyy_archive.tar.

# find /home/nilesh -type f -mtime +30 | xargs tar -cvf
/tmp/`date '+%d%m%Y'_archive.tar`

On a side note, you can perform lot of file related activities (including finding
files) us

Leave a Reply

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