Common find Files command under Linux

Source: Internet
Author: User

I. Common SEARCH commands

We often find a file in Linux, but do not know where to put it, you can use some of the following commands to search:

Which viewing the location of an executable file

Whereis viewing the location of a file

Locate to view file locations with a database

Find actual search hard disk query file name

Second, the common method of which command

Which is to find the executable file through the PATH environment variable to that route, so the basic function is to look for the executable file:

[email protected] ~]# which cat/bin/cat

The which command is usually followed by the Linux command, which can be used to find the executable file of the command according to the output results.

Iii. Common methods of Whereis command

1. Syntax:

Whereis [-BMSU] file or directory name

Parameter description:

-B: Only binary files are found

-M: Only files found under the manual path of the description file

-S: Find source files only

-u: File without document description

2. For example:

Find the files associated with the passwd file:

[Email protected] ~]# Whereis passwdpasswd:/usr/bin/passwd/etc/passwd/usr/share/man/man1/passwd.1.gz

Only binary files are found:

[Email protected] ~]# whereis-b passwdpasswd:/usr/bin/passwd/etc/passwd

Whereis looks very fast 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 files are not updated in real time, and are 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 create a file, but we can't find it.  The reason is because the database file was not updated.

Iv. Common methods of LOCATE commands

Because the Linux system does not have the locate command installed by default, it needs to be installed manually:

[Email protected] ~]# yum-y install Mlocate

As mentioned earlier, you need to rely on the original generated database when using the locate command, saying that the files you have just created and deleted may be found, and if you want to take immediate effect, you can use the updatedb command to load the database from the newly loaded build, and this load will increase the load. Therefore, it is recommended to use caution when executing updatedb commands, which can be synchronized at night.

[[email protected] ~]# Updatedb[[email protected] ~]# echo $?0[[email protected] ~]# locate Passwd/data/www/source/module /member/member_getpasswd.php/data/www/source/module/member/member_lostpasswd.php/data/www/template/default/ forum/forumdisplay_passwd.htm/data/www/template/default/member/getpasswd.htm/etc/passwd/etc/passwd- ..........................

The results of the search show that the results of using the Locate command are more detailed than those found in Whereis and which, but not exact fuzzy matching lookups, which are described using the Find command for exact matching lookups.

V. Common methods of the Find command

1. Command format

Find Pathname-options [-print-exec-ok ...]

2. Command parameters

The directory path that the Pathname:find command looks for. For example, use. To represent the current directory, and/to represent the system root directory.

The-print:find command outputs the matched file to standard output.

The-exec:find command executes the shell command given by the parameter to the matching file. The corresponding command is in the form of ' command ' {} \;, note the space between {} and \;

-ok: The same as-exec, except that the shell command given by the parameter is executed in a more secure mode, prompting the user to determine whether to execute before executing each command.

3. Common options

-name searches files by file name and supports wildcard.

-iname ignores the case-finding of file names.

-perm to find files according to file permissions.

-user Search for files according to the owner of the file.

-group finds files according to the group to which the files belong.

-nogroup finds a file that does not have a valid owning group, that is, the group to which the file belongs does not exist in/etc/groups.

-nouser finds a file without a valid owner, that is, the owner of the file does not exist in the/etc/passwd.

-newer file1! File2 look for a file that changes time than the file File1 new but older than the file file2.

-follow: If the find command encounters a symbolic link file, it tracks to the file that the link points to.

-type find a file of a certain type, such as:

B-block device files.


C-character device file.

P-Pipeline file.

L-Symbolic link file.

F-Normal file.

Combination criteria: Precede the options you want to find

-A: With. While satisfying, can omit

-O: Or, a meeting

-not,! Non -. Take counter

-size N: {The front can be positive negative numbers and the following commonly used units are K, M, G, for example, if you use +2m to represent a file larger than 2M, 2M represents a file between 1m-2m, -2m represents a file between 0-1m.

-mtime {-|+}n: In days, the file changes time to find the file,-n means that the file change time is now less than n days, + n means that the file change time is now N days ago. The Find command also has the-atime and-ctime options, but they are all similar to the-mtime option. In addition, the following three differences:

-amin N Find the last n minutes of files accessed in the system

-atime N Find the last n*24 hour Access file in the system

-cmin n Find files in the last n minutes of the system changed file status

-ctime n Find files that have changed file status in the last n*24 hours of the system

-mmin n Find files that have changed file data in the last N minutes of the system

-mtime n Find files that have changed file data for the last n*24 hours in the system

Lookup based on permissions: -perm [+|-]mode exact match +mode: Any one of a class of users can match, often used to find out whether a particular kind of user's specific permissions exist,-mode: Each class of users has a specified permission bit to check that matches, for example:

File permissions: 644

-perm 600: Mismatch

-perm + 222: match, find user has Write permission

-perm + 002: Mismatched, 0 means not viewing

-perm-444: Match

4. Use Example

Look for files in the/var directory that belong to the primary root and belong to the group Mail [[email protected] ~]# find /var -user root - Group mail Find all files that do not belong to root, bin in the/usr directory [[email protected] ~]# find /usr -not - user root -not -user bin  find the contents of the/etc directory that have been modified in the last week and are not part of the root file [[email protected]  ~]# find /etc -mtime -7 -not -user root find files that are not in a group or group on the current system and have been accessed in the last one months [ [Email protected] ~]# find -nouser -atime -30 -o -nogroup -atime  -30 find all files that are larger than 1M in the early/etc/directory and are of type normal file [[email protected] ~]# find /etc/ -type f  -size +1m Find files for/etc/directory All users do not have write permission [[email protected] ~]#  find /etc/ - not -perm +222 find at least one type of file without write permission in the/etc/directory [[Email protected] ~]# find /etc/ -perm  -222 Find the/ETC/INIT.D directory, all users have permission to execute files that their other users have write access to [[email protected] ~]# find /etc/init.d/   -perm -111 -perm -002 or  find /etc/init.d/  -perm -113 Query the modified file of the day and list the properties of the file [[email  protected] ~]# find  ./  -mtime  -1  -type f   -exec    ls -l    {} \; using the-ok parameter in the example above, You can manually enter the results after you query the results: [[email protected] ~]# find    ./    -mtime    -1    -type f    -ok     ls -l    {} \;  < ls ... ./.bash_ history > ? [[email protected] ~]# find   ./    -mtime    -1    -type f    -ok     ls -l    {} \;  < ls ... ./.bash_history &NBSP;&GT;&NBSP;?&NBSP;YES-RW-------. &NBSP;1&NBsp;root root 16294 7 Month   11 06:35 ./.bash_history 

According to the example above, we can use find when we can't find the files we need with Whereis and locate, but find is a way to traverse lookups on the hard disk, so it consumes hard disk resources and is very inefficient. It is therefore recommended that whereis and locate be preferred.  Locate is found in the database, and the database is updated as large as daily.   Whereis can find executable commands and man page find is the search for files based on criteria. which can find executable files and aliases (alias).


Marco Video



This article is from the "Bread" blog, make sure to keep this source

Common find Files command under Linux

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: 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.