Linux command: Find command finds files

Source: Internet
Author: User

The Find command is one of the most commonly used commands under Linux, with the flexibility to use the Find command, and you'll find it easy to find files.

Command format

Find [Specify Find directory] [find rules (options)] [actions performed after finding]

Parameter rules

-name finding files by file name
-iname Search by file name, but not case-sensitive
-perm follow file permissions to find files
-prune Use this option to enable the Find command to not find in the currently specified directory

-user search for files based on owner
-group to find files based on genus Group

-uid to find files based on UID
-gid searching for files based on GID

Use of association conditions
-A connection two different conditions (two conditions must be met simultaneously)
-O Connection two different conditions (two conditions to satisfy one)
-not on the condition inversion

Find files based on the relevant properties of the file timestamp
-mmin n files were last modified within n minutes
-mtime n files were last modified within n*24 hours
Find/tmp–atime +5 to find files that have not been accessed within five days

-amin N File The last access is within n minutes
-atime n file last accessed within n*24 hours

The state of the-cmin n file is changed within n minutes
-ctime N file status changed within n*24 hours (i.e. n days)

-type to find files based on file type

// Normal file // Catalog Files // Link File // Block device Files // Character device Files // Pipeline Files // Socket File

-size to find files based on size

-perm finding files based on file permissions

-newer file1! File2 find changed time than file File1 new but older file file2 than file

Find out execution
-print actions by default
-ls find and then use LS to display it
-ok [commend] asks the user if they want to execute the command after the lookup
-exec [commend] does not ask the user when executing the command after the lookup, directly executes

-nouser And-nogroup Search the entire system for files that are neither owner nor group (such files are often dangerous and should be purged in a timely manner)

Related commands

Locate command,the locate command is actually "Find-name" another way of writing, but much faster than the latter, because it does not search the specific directory, but instead of searching a database (/var/lib/locatedb), This database contains all the local file information. The Linux system automatically creates this database and updates it automatically once a day, so you can't find the latest changed files using the Locate command. To avoid this situation, you can manually update the database by using the UpdateDB command before using locate.
whereis command to find eligible files in a specific directory. Whereis looks very fast when compared to find, because the Linux system records all the files in the system in a single database file, and when you use Whereis and the locate described below, the data is looked up from the database, not like the Find command. By traversing the hard drive to find, the efficiency will naturally be very high. However, the database file is not updated in real time and is updated once a week by default, so when we use Whereis and locate to find files, we sometimes find data that has been deleted, or just created the file, but cannot find it because the database file is not updated.

The which command,which, is to find the executable file through the PATH environment variable to that route, and the basic function is to look for the executable file.
The type command, which is used to distinguish whether a command is brought by the shell or by a standalone binary file outside the shell. If a command is an external command, then using the-p parameter displays the path to the command, which is equivalent to the which command.

Scenario Example

1. Find files by file name

This is a basic use of the Find command.
# find-name "Nginx.conf"

2. Find files by file name, ignoring case
# find-iname "Nginx.conf"

3. Use mindepth and maxdepth to limit the depth of the search to a specified directory
Look for the passwd file under the root directory and its subdirectories.

# Find/-name passwd
Find passwd in the root directory and its 1-deep subdirectory. (e.g. Root-level 1, and one Sub-directory-level 2)

# find-maxdepth 2-name passwd
Locate the passwd file in the root directory and in the subdirectories of its maximum two-level depth. (e.g. Root-level 1, and Sub-directories-level 2 and 3)

# Find/-maxdepth 3-name passwd
Find the passwd file between the second-level subdirectory and the fourth subdirectory.

# find-mindepth 3-maxdepth 5-name passwd

4. After the search command, execute the command on the file found on the find

The following example shows the Find command to calculate the MD5 validation and for all files that are not case-sensitive with the file name "MYCPROGRAM.C". {} will be replaced by the current file name.

Find-iname "MYCPROGRAM.C"-exec md5sum {} \;
5. The opposite match

Displays all names that are not MYCPROGRAM.C files or directories. Because MaxDepth is 1, only the files and directories in the current directory are displayed.

Find-maxdepth 1-not-iname "MYCPROGRAM.C"

6. Finding files using inode numbers

Each file has a unique inode number, which allows us to differentiate the file. Create two names of similar files, such as one with a space ending, one without.

The output from LS cannot distinguish which file has a space ending. With option-I, you can see the inode number of the file, which allows you to differentiate between the two files.

You can specify the inode number in the Find command as shown below. Here, the Find command renames a file with an inode number.

Find-inum 16187430-exec mv {} new-test-file-name \;

7. Find files based on file permissions

The following actions are reasonable:
Locate a file with the specified permissions
Ignore other permission bits to check whether the specified permissions match
Search according to the permission given by the octal/symbolic representation
In this example, assume that the directory contains the following files. Note The permissions for these files are different.

ls-l
total 0
-rwxrwxrwx 1 root root 0 2009-02-19 20:31 all_for_all
-rw-r--r--1 root root 0 20 09-02-19 20:30 everybody_read
----------1 root root 0 2009-02-19 20:31 no_for_ All
-rw-------1 root root 0 2009-02-19 20:29 ordinary_file
-rw-r-----1 root root 0 2009-02-19 20:27 others_can_also_read
----r-----1 Root root 0 2009-02-19 20:27 others_can_only_read
Find a file with group Read permission. Use the following command to locate a file in the current directory that has read permissions for the same group of users, ignoring other permissions for the file.

Find. -perm-g=r-type f-exec ls-l {} \;
-rw-r--r--1 root root 0 2009-02-19 20:30./everybody_read
-rwxrwxrwx 1 root root 0 2009-02-19 20:31./all_for_all
----r-----1 root root 0 2009-02-19 20:27./others_can_only_read
-rw-r-----1 root root 0 2009-02-19 20:27./others_can_also_read
Locate the file that has read-only permissions for the group user.

Find. -perm g=r-type f-exec ls-l {} \;
----r-----1 root root 0 2009-02-19 20:27./others_can_only_read
Locate the file that has read-only permissions for the group user (in the form of octal permissions).

Find. -perm 040-type f-exec ls-l {} \;
----r-----1 root root 0 2009-02-19 20:27./others_can_only_read

8. Find all the empty files (0 bytes files) in the home directory and sub-directory

Most of the output files for the following command are locked file boxes created by other programs place Hoders

Find ~-empty
List only the empty files in your home directory.

Find. -maxdepth 1-empty

Only non-hidden empty files under the current year directory are listed.

Find. -maxdepth 1-empty-not-name ". *"
9. Find the 5 largest files

The following command lists the 5 largest files in the current directory and subdirectories. This will take a little time, depending on the number of files the command needs to process.

Find. -type f-exec ls-s {} \; | Sort-n-R | Head-5
10. Find 5 Smallest files

method is similar to finding the 5 largest files, except that the sort order is descending.

Find. -type f-exec ls-s {} \; | Sort-n | Head-5
In the above command, most likely you see only empty files (0-byte files). So, you can use the following command to list the smallest file instead of the 0-byte file.

Find. -not-empty-type f-exec ls-s {} \; | Sort-n | Head-5

11. Use-type to find files of the specified file type

Find only socket files
Find. -type s
Find all the Directories
Find. -type D
Find all the general files
Find. -type F
Find all hidden files

Find. -type f-name ". *"
Find all hidden directories

Find-type d-name ". *"
12. Find files by comparing modification times with other files

Displays files that have been modified after the specified file. The Find command below shows all the files that were created after ordinary_file.
# Find-newer Ordinary_file

13. Find files by file size
Use the-size option to find files by file size.
Find files larger than the specified file

Find ~-size +100m
Find files that are smaller than the specified file

Find ~-size-100m
Find files that match a given size

Find ~-size 100M
Note: – refers to smaller than a given size, + refers to larger than a given size. No symbol represents exactly as large as the given size.

14. Alias for common find operations

If you find something useful, you can give him an alias. and execute it wherever you want.
Commonly used to delete a.out files.

Alias rmao= "Find. -iname a.out-exec rm {} \; "
# Rmao
Remove the core file generated by the C program.

Alias rmc= "Find. -iname core-exec rm {} \; "
# RMC

15. Delete large packaged files with the Find command
The following command deletes a *.zip file larger than 100M.
Find/-type f-name *.zip-size +100m-exec rm-i {} \; "

16. Find files that have been changed within 1 hours
Find-mmin-60


Or
Find-mtime-1
17. Find the files that have been accessed within 1 hours
# Find. -amin-60

18. Find the file with state changed within one hours
# Find. -cmin-60

19. Search is only limited to files, do not display folders
# Find/-name "*.log"
# Find/-xdev-name "*.log"

20. Find only non-hidden files (hidden files are not shown)
if we don't want to hide the files when we look for them, we can find them using the following regular expressions:
The following command displays files that have been modified in the current directory and its subdirectories for 15 minutes, and lists only non-hidden files. That is, the files that begin with. are not displayed.
# Find/-xdev-name "*.log"
21. Find file modification time in a file after a modified file
# find-newer/etc/passwd
22. Search only in the current file system
The following command searches for the file names at the end of all. log directories in the root directory/its subdirectories. If you have multiple partitions under/below, then this search will search for all mounted partitions:
# Find/-name "*.log"
If we use the-xdev option, we will only search the current file system, and here is a definition of-xdev found on the Xdev man page:
-xdev Don ' t descend directories on other filesystems.
The following command searches the/directory and its subdirectories for all files ending in. Log in the current file system (i.e./mounted file system), meaning that if you have multiple partitions attached to/below, the following search will not search for other partitions (such as/home/)
# Find/-xdev-name "*.log"

Refer to Linux five find commands Ruan Yi Feng

Linux command: Find command finds files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.