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:
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:
/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:
/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:
/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:
/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:
/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:
/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:
/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:
/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:
/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:
/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:
/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:
-rw-r--r-- 1 johndoe johndoe 5242880 Nov 10 09:46 /tmp/find-dir/sub-dir2/file-8.dat