The Linux find command utility is a powerful command in locating the files and directories at system wide level, the
The basic syntax of find command is very straight forward. The number of filters and types of options you choose while forming the find command determines and complexity of the operation and is directly proportional to the run time of the command.
Lets examine the general syntax of Linux find
command line utility
$ find [path] [Options/filters] [expression]
How to Find all the files that are modified within 1 day.
$ find . -mtime -1
Use find command to find all the files that are modified within last x Minutes
$ find . -mmin -360
How to Find all the files that are created within given Minutes
$ find . -cmin -360
How to Find all the files under a specific size on the filesystem.
$ find . -type f -size -100G
NOTE: the -
will find the files including and under the mentioned size
How to find really big size files with a given size using Linux Find Command.
$ find . -type f -size +100G
NOTE: the +
will find the files exceeding the mentioned size
How to Find all the files with a pattern match using Find command?
vamshi.santhapuri@linux-pc:~/Linux/Programs> find *.py my_program.py myProgram.py My_program.py
OutPut Demonstrated:
vamshi@linuxcent:~/Linux/Programs> ls -l total 0 -rwxr-xr-x 1 vamshi users 0 Apr 8 16:33 my_program.py -rwxr-xr-x 1 vamshi users 0 Apr 8 16:32 myProgram.py -rwxr-xr-x 1 vamshi users 0 Apr 8 16:32 My_program.py ex
Find Files Based on file-type using option -type
Find only the regular file type
$ sudo find / -type f -print
Find only the Directory
$ sudo find / -type d -print
Lets see some special file formats like character files as in network level files and block files as in storage device file type.
$ sudo find /dev/ -type b -print /dev/sdb8 /dev/sdb7 /dev/sdb6 /dev/sdb5 /dev/sdb4 /dev/sdb1 /dev/sdb /dev/sda6 /dev/sda5
How to find empty files and Directories in Linux using Find command?
$ find . -empty # Finds all files and directories that are empty $ find . -type f -empty #Find onlt empty files $ find . -type d -empty #Find only empty Directories
How to Find all the hidden files and directories using Find command
You can also add the type flag as required
$ find . -name ".*" //Prints all the files and directories that are hidden
Find the character stream files and identify them yourselves.
$ sudo find / -type c -print
How to exclude some files or directories while using the find command?
The negation Operator !
in find command line
[vamshi@linuxcent ~]$ find / -type d ! -path /mount ! -path /mnt/3TB/
How to find the files with .py extension and then setting the execute permission on them ?
-exec
is a special builtin option which takes the specified command on the selected files
[vamshi@linuxcent ~]$ find *.py -exec chmod +x {} \;
A practical example of Finding and Deleting Big size files using Find Command
We Demonstrate a Big thread dump file that we generated in our Test environment by finding it on our server and then deleting it.
[vamshi@linuxcent ~]$ find /var -type f -size +100G -exec rm {} \;
To find the files with matching file permissions
[vamshi@linuxcent ~]$ sudo find / -type f -perm 0600 -print
More complex find operations can be performed by combining the negation operator and by excluding the certain mount points and using the logical AND OR operators.
For example:
sudo find / -xdev -type d \( -perm -0002 -a ! -perm -1000 \) -exec ls -ld {} \;
Ignore case using the find command
The filter -iname
enables the find to perform case insesitive search.
[vamshi@node02 Linux-blog]$ find . -iname "rEadMe*" ./Debian-Distro/README-Debian-Distro ./OpenSuse-Distro/README-Opensuse-Distro ./README ./Redhat-Distro/Centos/README-CentOS ./Redhat-Distro/README-Redhat-Distro
How to use Maxdepth Option in the find command to explore the directory depth?
[vamshi@node02 Linux-blog]$ find . -maxdepth 1 -name "README*" ./README
By using find command filter option -maxdepth
Now we can extract the results based on the one level deeper subdirectory, which is demonstrated as follows:
[vamshi@node02 Linux-blog]$ find . -maxdepth 2 -name "README*" ./Debian-Distro/README-Debian-Distro ./OpenSuse-Distro/README-Opensuse-Distro ./README ./Redhat-Distro/README-Redhat-Distro
How to use mindepth Option in the find command to restrict the results based on the subdirectory depth?
The -mindepth
filter option restricts the find command search to nth subdirectory level from the given path.
[vamshi@node02 Linux-blog]$ find . -mindepth 3 -name "README*" ./Redhat-Distro/Centos/README-CentOS