Learn Find Command and usages
Learn Find Command and usages
Date Posted: 28-03-2017
Find is a command which is used to find files and directories based on various scenario. In this post, we will explain find command and usage with some examples.
Find a filename called test.txt on the directory /home/pheonixsolutions. The below command search all the directories and report test.txt.
find /home/pheonixsolutions/ -type f -name test.txt
Find filename starts test and extension is txt(say test1.txt, test2.txt, test3.txt).
find /home/pheonixsolutions/ -type f -name test*.txt
/home/pheonixsolutions/test1.txt /home/pheonixsolutions/test2.txt /home/pheonixsolutions/test3.txt
Find directory named called dir on the current working directory(for current working directory, we will be using .)
find . -type d -name dir
./dir1/dir ./dir
Find directory named called dir with maximum of 2 sub directories
find . -type d -name dir -maxdepth 2
./dir1/dir ./dir
Find directory named called dir with minimum of 2 directories.
find . -type d -name dir -mindepth 2
./dir1/dir ./dir1/dir2/dir
Find the file which has permission of 644.
find . -type f -perm 644
./dir1/file1.txt /dir/dir1/file2.txt file.txt
Execute commands to the find command result. This will list files with full information.
find . -type f -exec ls -la
The below example will change the permission of the find file.
find . type f -exec chmod 644 {} \;
Assign find command result using xargs.
find . type f |xargs ls -la
Use find command result on multiple places.
find . -type f |xargs -I {} mv {} {}_bak
Find the files which are older than 3 days
find . -type f -mtime +3
To delete files which are older than 3 days.
find . -type f -mtime +5 -exec rm {} \;
To find the files which are accessed before 5 days
find . -type f -atime +5
Find files which ownership is pheonixsolutions
find . -type f -user pheonixsolutions
To find files which group is pheonixsolutions
find . -type f -group pheonixsolutions