Nội dung
Some commonly used Find commands on Linux that you need to know…
Introduction
The find command is a powerful search utility used to search or locate files in operating systems such as Linux and Unix. It can find files and folders based on criteria like name, extension, type, modification and creation date, permissions, size, owner…
And in this article, I will focus on explaining how to use the find command and giving specific examples for you to visualize easily.
Structure of the Find command
The basic structure of the find command would be:
find <directory-to-search> [OPTIONS] <find name,file,extension>
Here are some options that come with the find command:
-H
and-L :
: Used to manage symlinks when searching.- -P: Don’t follow symlinks.
- -L: Follow symbolic links.
For example
Note: When using the find command, the structure of the command will be: find <path to search>. For example, find /home means that it will search at the path Home. Otherwise, if it is “find .” it will find it at the path you are standing at.
- Find files by name
To find a file by its name, use the command with the ‘-name
‘ option.
Example 1: Find a file named info.php in the Home folder, the command structure will be as follows:
find /home -name azdigi.php
Example 2: Find a file named info.php in the existing folder (ie find the file where you are)
find . -name azdigi.php
- Find files by extension
If you want to search all files by extension, you can use find command with*. <file-extension>
For example: Searching for all files ending in ‘.png‘ in the Home folder, the command structure will be as follows:
find /home -name *.png
- Find only folders
If you only want to search folders, use the find command with the ‘-type d’ option.
For example: Find all files in the Home folder with the command structure will be as follows:
find /home -type d
- Find only files
If you just want to search for files, use the command with the ‘-type f’ option.
For example: Find all files in the Home folder with the command structure will be as follows:
find /home -type f
- Find and delete
In some cases where you want to find and delete files/folders that are no longer needed, you can run the find command with the ‘-delete’ option.
For example: Find all *.png images in the Home folder and delete:
find /home -name *.png -delete
- Find File/Folder with specific permissions
If you want to search all files/folders with specific permissions, use the command with the ‘-perm’ option.
For example: To find files with ‘644’ permissions and folders with ‘755’ permissions, the command structure would look like this:
find . -type f -perm 644 -exec ls -l {} \;
find . -type d -perm 775 -exec ls -l {} \;
- Find the File/Folder by date/time
You can use the find command with the ‘-mtime -N’ option to find files last modified ‘N’ days ago.
Example 1: To search all files modified in the last 3 days in the Home folder, the command structure would be as follows:
find /home -type f -mtime -3 -exec ls -l {} \;
Another way is to use the find command to search for files/folders by specific date/time with the ‘-newerXY’ option.
In which, the case X and Y can be:
a- file access time
B- file creation time
c- time to change the inode state of the file
m- file modification time
t- reference is directly interpreted as time
Example 2: To search all files modified on April 10, 2022 in the Home folder, the command structure would be as below:
find /home -type f -newermt 2022-04-10 -ls
- Find File/Folder by size
If you want to search File/Folder by size, use the command with the ‘-size’ option.
For example: To search for a File/Folder larger than 1GB in the Home folder, the command structure would be as follows:
find /home -type f -size +1G
find /home -type d -size +1G
Above are the most commonly used basic find commands that you can refer to. So with the find command, we can easily find or view the full range of File/Folder options so that administrators can easily manage many Files/Folders on their server. Hope this article will help you, wish you success!
You can refer to other instructions at the link below:
If you need assistance, you can contact support in the ways below:
- Hotline 247: 028 888 24768 (Ext 0)
- Ticket/Email: You use the email to register for the service and send it directly to: support@azdigi.com .