Find : Find command is one of the most important and much used command in Linux sytems. Find command used to search and locate list of files and directories based on conditions you specify for files that match the arguments. Find can be used in variety of conditions like you can find files by permissions, users, groups, file type, date, size and other possible criteria.
Syntax: $find <pathname> <condition>
a)To find a single file by name pass the -name option to find along with the name of the file.
Ex: $find -name cities
It will show the every path where file cities exist in the system.Here ABC,Demo and pqr are directories.
b)To find for files using name and ignoring case.
Ex: $find -iname cities
c)To find a file in the current directory only.
Ex: $find -maxdepth 1 -name cities
d)To find for files in a specific directory.
Ex: $find ./Demo -name cities
e)To find for files containing a specific word in its name.
Ex: $find -name "*cities*"
Ex: $find -iname "*cities*"
Ex: $find -iname "*cit*"
f)To run the last executed find command.
Ex: $!find
g)To find the files whose name are not same cities.
Ex: $find -not -name cities
It prints all the files except cities.
h)To print the files in the current directory and one level down to the current directory.
Ex: $find -maxdepth 2 -name cities
i)To print the files in the current directory and two level down to the current directory.
Ex: $find -maxdepth 3 -name cities
j)To print the files in the subdirectries between level 1 and 4.
Ex: $find -mindepth 2 -maxdepth 5 -name cities
k)To find the empty files in a directory.
2)To find files based on the file type.
a) Finding directories
Ex : $find . -type d
b) Finding regular files
Ex : $find . -type f
c)Finding hidden directories
Ex : $find -type d -name ".*"
d)Finding hidden files
Ex : $find -type f -name ".*"
e)Finding all exe files
Ex : $find
f)Finding socket files
Ex : $find . -type s
3)To find files based on the size
a)Finding files whose size is exactly 5M
Ex : $find . -size 5M
b)Finding files whose size is more than 5M
c)Finding files whose size is less than 5M
Note:M should be in capital.
Note : 777 specifies read,write and execute permissions.To know the permissions of the files and directories we use ls -l.
b)Find all the files without having permissions are 777
Ex : $find -type f ! -perm 777
c)Find all the SGID bit files whose permissions set to 644
Ex : $find -perm 644
d)Find all SUID set files
Ex : $find / -perm /u=s
e)Find all SGID set files
Ex : $find / -perm /g=s
f)To find all read only files
Ex : $find / -perm /u=r
g)To find all executable files
Ex : $find / -perm /a=x
h)To find the files which are modified after the modification of a give file.
Ex : $find -newer "customers.txt"
This will display all the files which are modified after the file customers.txt
i)Display the files which are accessed after the modification of a give file
Ex :$find -anewer "department"
j)Files which are changed after the modification of a give file.
Syntax: $find <pathname> <condition>
a)To find a single file by name pass the -name option to find along with the name of the file.
Ex: $find -name cities
It will show the every path where file cities exist in the system.Here ABC,Demo and pqr are directories.
b)To find for files using name and ignoring case.
Ex: $find -iname cities
c)To find a file in the current directory only.
Ex: $find -maxdepth 1 -name cities
d)To find for files in a specific directory.
Ex: $find ./Demo -name cities
e)To find for files containing a specific word in its name.
Ex: $find -name "*cities*"
Ex: $find -iname "*cities*"
Ex: $find -iname "*cit*"
f)To run the last executed find command.
Ex: $!find
g)To find the files whose name are not same cities.
Ex: $find -not -name cities
It prints all the files except cities.
h)To print the files in the current directory and one level down to the current directory.
Ex: $find -maxdepth 2 -name cities
i)To print the files in the current directory and two level down to the current directory.
Ex: $find -mindepth 2 -maxdepth 5 -name cities
k)To find the empty files in a directory.
Ex: $find -maxdepth 1 -empty
2)To find files based on the file type.
a) Finding directories
Ex : $find . -type d
b) Finding regular files
Ex : $find . -type f
c)Finding hidden directories
Ex : $find -type d -name ".*"
d)Finding hidden files
Ex : $find -type f -name ".*"
e)Finding all exe files
Ex : $find
f)Finding socket files
Ex : $find . -type s
3)To find files based on the size
a)Finding files whose size is exactly 5M
Ex : $find . -size 5M
b)Finding files whose size is more than 5M
Ex : $find . -size +5M
Ex : $find . -size -5M
Note:M should be in capital.
4)To find files based on their permission
a)Find all the files whose permissions are 777
Ex : $find . -perm 777
or
Ex : $find . -perm 0777 -print
or
Ex : $find . -type f -perm 0777 -print
Note : 777 specifies read,write and execute permissions.To know the permissions of the files and directories we use ls -l.
b)Find all the files without having permissions are 777
Ex : $find -type f ! -perm 777
c)Find all the SGID bit files whose permissions set to 644
Ex : $find -perm 644
d)Find all SUID set files
Ex : $find / -perm /u=s
e)Find all SGID set files
Ex : $find / -perm /g=s
f)To find all read only files
Ex : $find / -perm /u=r
g)To find all executable files
Ex : $find / -perm /a=x
h)To find the files which are modified after the modification of a give file.
Ex : $find -newer "customers.txt"
This will display all the files which are modified after the file customers.txt
i)Display the files which are accessed after the modification of a give file
Ex :$find -anewer "department"
j)Files which are changed after the modification of a give file.
Ex :$find -cnewer "employees"
k)Find the files which are modified within 10 minutes.
Ex : $find . -mmin -10
l)Find the files which are modified within 2 day
Ex : $find . -mtime 2
m)Find the files which are modified 10 minutes back
Ex :$find . -not -mmin -10
n)Find the files which are modified 1 day back.
Ex : $find . -not -mtime -1
o)Print the files which are accessed within 1 hour.
Ex :$find . -amin -60
p)Print the files which are accessed within 1 day
Ex :$find . -atime -1
q)Files which are changed within 2 hours
Ex :$find . -cmin -120
r)Files which are changed within 3 days
Ex :$find . -ctime -3
s)Find the files which are created between two files
Ex :$find . -cnewer f1 -and ! -cnewer f2
5)Find the permissions of the files which contain the name cities
Ex :$find -name "*cities*"|xargs ls -l
b)To remove files which contain the name "cities"
Ex :$find -name "*cities*" -exec rm -r {} \;
Above command delete all cities file from all the directories.
No comments:
Post a Comment