Hello everyone In this Article we are going to learn how we can find files/directories in Linux in a very simple and easy step-by-step approach.
Linux find command With Examples
The find command is used to search any files and directory in the Linux kernel. When we forget the location of a file & directory then we can use the find command to search them
Before Jumping on the command we should know the syntax of the find command
find location option argument
Let’s make file using touch command in /etc/ directory .
# touch /etc/File1.txt
Let’s make directory using mkdir command in /etc/ directory .
# mkdir /etc/Directory
If we forget the location where we created File1.txt & Directory then we can use the find command to search for files and directory
1- How to search a File on the base of their Name
When we add the ( -name )option in the find command then we can search the File on the basics of their name. In this example, we are searching from the / directory in the Linux
# find / -name FileName
2 – How to search a Directory on the base of their Name
When we add the ( -name )option in the find command then we can search the Directory on the basics of their name. In this example, we are searching from the / directory in Linux.
# find -name / DirectoryName
3 – How to search Files and Directories that had Readable Permission
To search the Files and Directories that had only readable permission then we can add ( -readable )option in the find command
# find / -readable
4 – How to search Files and Directories that had Writable Permission
To search the Files and Directories that had only writable permission then we can add ( -writable ) option in the find command
# find / -writable
5 – How to search Files and Directories that had Executable Permission
To search the Files and Directories that had only executable permission then we can add ( -executable ) option in the find command
# find / -executable
6 – How to search empty Files & Directory
When we add a ( -empty ) option in the find command then this will search the Files & Directories that are empty.
# find / -empty
7 – How to search Files & Directory with exact permission
To search the files & directories with the exact permission then we can add ( -perm ) option in the find command with permission
# find / -perm 777
8 – How to search Directories & Files of a specific size
To search files & directories with a specific size then we can add ( -size ) option in the find command
# find / -size 1M
9 – How to Search a File & Directory with More Size
To search for Files & Directories whose size is greater then 10MB then we can add + 10M argument in the find command, With the ( -size ) argument
# find / -size +10M
10 – How to Search a File & Directory with Less Size
To search for Files & Directories whose size is lesser than 10MB then we can add the -10M argument in the find command., With the ( -size ) argument
# find / -size -10M
11 – How to search Files & Directories with specific range
To search the Files & Directories with a specific size then we can specify the size in the find command. Here we are searching for files & directories between the range 10 – 12MB
# find / -size +10M -size -12M
12 – How to search specific user data
To search for specific user data in the system then we can add (-user ) option with username in the find command
# find / -user devopstut
13 – How to execute another command in find command
We can use the ( -exec )option when we want to execute some other command in the find command.
Here we want to put all data of devopstut user inside the devopstutdata directory.
To terminate from -exec option we have to add \;
# find / -user devopstut -exec cp -rf {} /devopstutdata \;
14 – How to search only files in find command
To search only files then we can add ( -f ) option in the find command.
Here we are searching for the file name as abc from the /data/ directory
# find /data/* -type f -name abc*
15 – How to search only directories in find command
To search only files then we can add ( -d ) option in the find command.
Here we are searching for the directory name as xyz from the /data/ directory.
#find /data/* -type d -name xyz
16 – How to Find specific permission and replace with other permission
To search for the specific permission and we want to change to some other permission then we can use the ( – exec ) option in find command with permission. Here we are searching for permission 755 and we are replacing it with 777 permission
# find /* -perm 755 -exec chmod 777 {} \;
17 – How to check version Of the find command
To check what is the version of the find command then we can execute the below command.
# find --version
18 – How to View the Most useful Options in the find command
To know the most useful options with the find command then we can execute below command.
# find --help
Conclusion
If you want to learn Linux in the most simple language then please click on this link.
If you want to see the man page of find command online then click on this link
This is the complete knowledge of how we can search files & Directories in the Linux kernel using the rm command if you have any doubts please feel free to comment below. Please don’t forget to join our email subscription to get the latest updates on DevOps Articles.