Linux Command :: find


Bhaskar S 11/10/2013


The Linux find command is a very useful tool that helps in searching and listing directories and files satisfying a specified criteria based on user(s), group(s), permission(s), size(s), etc.

In order to get our hands dirty with the find command, we will setup a simple directory structure with some files.

We will execute the following shell script to setup the desired structure:

Shell Script Listing
#!/bin/bash

cd /tmp

rm -rf find-dir

mkdir -p find-dir/sub-dir1
mkdir -p find-dir/sub-dir2

cd find-dir

echo > file-1.txt
chmod 755 file-1.txt

echo "file2" > file-2.txt
chmod 655 file-2.txt

echo "log" > file-3.log
chmod 777 file-3.log
sudo chown nobody:nogroup file-3.log

chmod 777 sub-dir1

cd sub-dir1

echo '<xyz/>' > file-4.xml
chmod 755 file-4.xml

> file-5.xml
chmod 655 file-5.xml

cd ../sub-dir2

echo 'LOG' > file-6.log
chmod 755 file-6.log

> .file-7.txt
chmod 755 .file-7.txt
sudo chown bswamina:nogroup .file-7.txt

dd if=/dev/zero of=file-8.dat bs=1M count=5

cd ../..

NOTE :: The above shell script and the following find commands have been tested on a Ubuntu based distribution.

To list all the directories and files under our test directory /tmp/find-dir/, issue the following command:

$ find /tmp/find-dir/

The following is the output:

Output

/tmp/find-dir/
/tmp/find-dir/file-2.txt
/tmp/find-dir/sub-dir1
/tmp/find-dir/sub-dir1/file-5.xml
/tmp/find-dir/sub-dir1/file-4.xml
/tmp/find-dir/file-3.log
/tmp/find-dir/sub-dir2
/tmp/find-dir/sub-dir2/.file-7.txt
/tmp/find-dir/sub-dir2/file-8.dat
/tmp/find-dir/sub-dir2/file-6.log
/tmp/find-dir/file-1.txt

To list all the files having the .log extension under our test directory /tmp/find-dir/, issue the following command:

$ find /tmp/find-dir/ -name "*.log"

The following is the output:

Output

/tmp/find-dir/file-3.log
/tmp/find-dir/sub-dir2/file-6.log

To list all the directories (not files) under our test directory /tmp/find-dir/, issue the following command:

$ find /tmp/find-dir/ -type d

The following is the output:

Output

/tmp/find-dir/
/tmp/find-dir/sub-dir1
/tmp/find-dir/sub-dir

To list all the files (not directories) under our test directory /tmp/find-dir/, issue the following command:

$ find /tmp/find-dir/ -type f

The following is the output:

Output

/tmp/find-dir/file-2.txt
/tmp/find-dir/sub-dir1/file-5.xml
/tmp/find-dir/sub-dir1/file-4.xml
/tmp/find-dir/file-3.log
/tmp/find-dir/sub-dir2/.file-7.txt
/tmp/find-dir/sub-dir2/file-8.dat
/tmp/find-dir/sub-dir2/file-6.log
/tmp/find-dir/file-1.txt

To list all the hidden files (not directories) under our test directory /tmp/find-dir/, issue the following command:

$ find /tmp/find-dir/ -type f -name ".*"

The following is the output:

Output

/tmp/find-dir/sub-dir2/.file-7.txt

To list all the files and directories with permissions 755 under our test directory /tmp/find-dir/, issue the following command:

$ find /tmp/find-dir/ -perm 755

The following is the output:

Output

/tmp/find-dir/
/tmp/find-dir/sub-dir1/file-4.xml
/tmp/find-dir/sub-dir2
/tmp/find-dir/sub-dir2/.file-7.txt
/tmp/find-dir/sub-dir2/file-6.log
/tmp/find-dir/file-1.txt

To list all the files and directories owned by the user nobody under our test directory /tmp/find-dir/, issue the following command:

$ find /tmp/find-dir/ -user nobody

The following is the output:

Output

/tmp/find-dir/file-3.log

To list all the files and directories owned by the group nogroup under our test directory /tmp/find-dir/, issue the following command:

$ find /tmp/find-dir/ -group nogroup

The following is the output:

Output

/tmp/find-dir/file-3.log
/tmp/find-dir/sub-dir2/.file-7.txt

To list all the empty files (not directories) under our test directory /tmp/find-dir/, issue the following command:

$ find /tmp/find-dir/ -type f -empty

The following is the output:

Output

/tmp/find-dir/sub-dir1/file-5.xml
/tmp/find-dir/sub-dir2/.file-7.txt

To list all the files (not directories) which have file sizes greater than 1MB and less than 10MB under our test directory /tmp/find-dir/, issue the following command:

$ find /tmp/find-dir/ -size +1M -size -10M

The following is the output:

Output

/tmp/find-dir/sub-dir2/file-8.dat

To list of all the files (not directories) with extensions .txt or .log under our test directory /tmp/find-dir/, issue the following command:

$ find /tmp/find-dir/ -type f \( -name '*.txt' -o -name '*.log' \)

The following is the output:

Output

/tmp/find-dir/file-2.txt
/tmp/find-dir/file-3.log
/tmp/find-dir/sub-dir2/.file-7.txt
/tmp/find-dir/sub-dir2/file-6.log
/tmp/find-dir/file-1.txt

To get a directory listing of all the files (not directories) which have file sizes greater than 1MB under our test directory /tmp/find-dir/, issue the following command:

$ find /tmp/find-dir/ -size +1M -exec ls -l {} \;

The following is the output:

Output

-rw-r--r-- 1 johndoe johndoe 5242880 Nov 10 09:46 /tmp/find-dir/sub-dir2/file-8.dat