Last updated on November 18th, 2022 at 01:13 pm

Find command can be very handy if you want to search files in a directory structure. It can also be used to delete files / directories, modify permissions, snapshots of directory structure etc.,

Below are the list of examples with details on using Find command

File with specific name

Let us assume that we have to find a file with the name core in or below the directory /tmp

find /tmp -name core -type f -print 

Now to delete all the files named core run (Make sure you use caution while delete files in bulk)

find /tmp -name core -type f -print | xargs /bin/rm -f


Find files named core in or below the directory /tmp and delete them, processing filenames in such a way that file or directory names containing single or double quotes, spaces or newlines are correctly handled. The -name test comes before the -type test in order to avoid having to call stat(2) on every file.

Run any commands inside directory


In this example we are running the command `file’ on every file in or below the current directory. Notice that the braces are enclosed in single quote marks to protect them from interpretation as shell script punctuation. The semicolon is similarly protected by the use of a backslash, though single quotes could have been used in that case also.

Semicolon ; ends the command executed by exec. Semicolon needs to be escaped with \ so that shell doesn’t treat it as its own special character.

root@production-server:/home# find . -type f -exec file '{}' \;
./myftpuser/.profile: ASCII text
./myftpuser/.bash_history: ASCII text
./myftpuser/.bashrc: ASCII text
./myftpuser/.cache/motd.legal-displayed: empty
./myftpuser/.bash_logout: ASCII text
./ubuntu/.profile: ASCII text
./ubuntu/awstatslogin/data_dir/awstats012015.mysetup.txt: ASCII text, with very long lines
./ubuntu/awstatslogin/data_dir/awstats022015.mysetup.txt: ASCII text, with very long lines
./ubuntu/awstatslogin/data_dir/awstats082014.mysetup.txt: ASCII text, with very long lines
./ubuntu/awstatslogin/data_dir/awstats042015.mysetup.txt: ASCII text, with very long lines
./ubuntu/awstatslogin/data_dir/awstats032015.mysetup.txt: ASCII text, with very long lines
./ubuntu/awstatslogin/data_dir/awstats112014.mysetup.txt: ASCII text, with very long lines
./ubuntu/awstatslogin/data_dir/awstats122014.mysetup.txt: ASCII text, with very long lines
./ubuntu/awstatslogin/data_dir/awstats102014.mysetup.txt: ASCII text, with very long lines
./ubuntu/awstatslogin/data_dir/awstats092014.mysetup.txt: ASCII text, with very long lines
./ubuntu/awstatslogin/awstats.model.conf: ASCII text

List all larger files (specific size)

To list all the files larger than a specific size you can use find command and save the output to different file in this example we are dumping the list of files greater than one MB to /root/big.txt

$ find /tmp \( -size +1M -fprintf /root/big.txt '%s %p\n' \)
$ cat big.txt
3628817 /tmp/go-pear.phar
1082982400 /tmp/backup_demo2022-08-01_19_13_09.tar.gz
1083791360 /tmp/backup_demo2022-05-11_16_33_47.tar.gz
1024102400 /tmp/backup_demo2022-06-02_21_44_19.tar.gz
1038612480 /tmp/backup_demo2022-06-09_20_21_25.tar.gz
1054023680 /tmp/backup_demo2022-04-09_16_15_39.tar.gz
1088942080 /tmp/backup_demo2022-08-12_17_38_55.tar.gz
1142968320 /tmp/backup_demo2022-11-11_20_21_59.tar.gz

Modify the above command according to your requirement.
%s is file size in bytes and %p print the file name.

Find files modified last 24 hours

Search for files in your home directory which have been modified in the last twenty-four hours. This command works this way because the time since each file was last modified is divided by 24 hours and any remainder is discarded. That means that to match –mtime 0, a file will have to have a modification in the past which is less than 24 hours ago.

find /tmp -mtime 0


Search for executable files

This command search for files in multiple locations that are executable but not readable

find /sbin /usr/sbin -executable \! -readable -print

Search files with permission

Search for files which have read and write permission for their owner, and group, but which other users can read but not write to (664). Files which meet these criteria but have other permissions bits set (for example if someone can execute the file) will not be matched.

find . -perm 664

$ find . -perm 664
./ubuntu/awstatslogin/awstats.mysetup.conf
$ ls -l ./ubuntu/awstatslogin/awstats.mysetup.conf
-rw-rw-r-- 1 www-data www-data 242 Apr 10  2015 ./ubuntu/awstatslogin/awstats.mysetup.conf
$

Search files with special permissions

In the example below I am creating a file and adding setuid permission.

$ touch myfile
$ chmod u+s myfile
$ ls -l
total 0
-rwSr--r-- 1 root root 0 Nov 18 20:58 myfile
$ touch test
$ ls -rlt
total 0
-rwSr--r-- 1 root root 0 Nov 18 20:58 myfile
-rw-r--r-- 1 root root 0 Nov 18 20:58 test
$ find . -perm 644
./test
$ find . -perm -644
.
./test
./myfile

Even if you have a file that shows as 644 as its permission it is not listed when we just ran 644 number along with find command. We should add a hyphen(-) before the 644 to list those special permission files.

$ find . -perm -644
.
./test
./myfile

Leave a Reply

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