locate command and find

locate 

It allows users to quickly search whether there is a specified file in the file system. The method is to first establish a database including the names and paths of all files in the system, and then only need to query this database when searching, without actually going deep into the file system. In general distribution, the establishment of the database is automatically executed in crontab.

1. Command format:

Locate [choose parameter] [style]

2. Command function:

The locate command can quickly find files when searching the database. The database is updated by the updatedb program. The updatedb is periodically created by the cron daemon. The locate command is faster than searching for data from the entire hard disk data when searching the database, but it is worse If the file found by locate is newly created or just renamed, it may not be found. Among the default values, updatedb will run once a day, and the setting value can be updated by modifying crontab. (etc/crontab)

locate is used to search for qualified files. It will go to the database storing file and directory names to find files or directories that meet the template style conditions. Special characters (such as "*" or "?", etc.) can be used to Specify the template style. If the specified template is kcpa*ner, locate will find all the files or directories whose starting string is kcpa and ending with ner. For example, if the name is kcpartner, if the directory name is kcpa_ner, it will list the files or directories included in the directory. All files including subdirectories.

The locate command is similar to the function of find to find files, but locate uses the update program to first create an index database for all files and directory data in the hard disk, and directly finds the index when executing loacte. The query speed will be faster, and the index database is generally It is managed by the operating system, but you can also directly issue an update to force the system to modify the index database immediately.

3. Command parameters:

-e will exclude from the search.

-1 if is 1.Then start safe mode. In safe mode, users will not see files that they cannot see. This will start to slow down, because locate has to go to the actual file system to get the file's permission data.

-f excludes specific file systems, for example we don't have reason to put files in the proc file system in the database.

-q Quiet mode, no error messages will be displayed.

-n Display at most n outputs.

-r Use regular expressions as search conditions.

-o Specifies the name of the repository.

-d specifies the path to the database

-h display auxiliary information

-V Display the version information of the program

4. Example of use:

Example 1: Find all files related to pwd

Order:

locate pwd

output:

peida-VirtualBox ~ # locate pwd

/bin/pwd

/etc/.pwd.lock

/sbin/unix_chkpwd

/usr/bin/pwdx

/usr/include/pwd.h

/usr/lib/python2.7/dist-packages/twisted/python/fakepwd.py

/usr/lib/python2.7/dist-packages/twisted/python/fakepwd.pyc

/usr/lib/python2.7/dist-packages/twisted/python/test/test_fakepwd.py

/usr/lib/python2.7/dist-packages/twisted/python/test/test_fakepwd.pyc

/usr/lib/syslinux/pwd.c32

/usr/share/help/C/empathy/irc-join-pwd.page

/usr/share/help/ca/empathy/irc-join-pwd.page

/usr/share/help/cs/empathy/irc-join-pwd.page

/usr/share/help/de/empathy/irc-join-pwd.page

/usr/share/help/el/empathy/irc-join-pwd.page

Example 2: Search for all files starting with sh in the etc directory 

Order:

locate /etc/sh

output:

peida-VirtualBox ~ # locate /etc/sh

/etc/shadow

/etc/shadow-

/etc/shells

peida-VirtualBox ~ #

Example 3: Search for all files starting with m in the etc directory

Order:

locate /etc/m

output:

peida-VirtualBox ~ # locate /etc/m

/etc/magic

/etc/magic.mime

/etc/mailcap

/etc/mailcap.order

/etc/manpath.config

/etc/mate-settings-daemon

find

The command searches the directory structure for files and performs the specified action. The find command under Linux provides quite a lot of search conditions, and the function is very powerful. Because find has powerful functions, it has many options, most of which are worth our time to understand. Even if the system contains a network file system (NFS), the find command is also valid in the file system, as long as you have the corresponding permissions. When running a very resource-consuming find command, many people tend to put it in the background, because traversing a large file system may take a long time (here refers to the file system above 30G bytes).

1. Command format:

find pathname -options [-print -exec -ok ...]

2. Command function:

Used to find files in the file tree and make corresponding processing 

3. Command parameters:

pathname: The directory path that the find command is looking for. For example, use . to represent the current directory, and use / to represent the system root directory. 

-print: The find command outputs matching files to standard output. 

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

-ok: It has the same function as -exec, except that the shell command given by this parameter is executed in a safer mode. Before executing each command, a prompt will be given to let the user determine whether to execute it.

4. Command options:

-name Find files by filename.

-perm Find files by file permissions.

-prune Use this option to make the find command not search in the currently specified directory. If you use the -depth option at the same time, -prune will be ignored by the find command.

-user Find files by their owner.

-group Find files by the group they belong to.

-mtime -n +n Find files according to the file change time, - n means that the file change time is within n days from now, + n means that the file change time is n days ago from now. The find command also has -atime and -ctime options, but they are both compatible with the -m time option.

-nogroup Finds files that have no effective group, that is, the group to which the file belongs does not exist in /etc/groups.

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

-newer file1 ! file2 Find files with a change time newer than file1 but older than file2.

-type finds files of a certain type, such as:

b - block device file.

d - directory.

c - A character device file.

p - the pipeline file.

l - symbolic link file.

f - normal file.

-size n: [c] Find files with a file length of n blocks, with c indicating that the file length is in bytes. -depth: When looking for files, first look for files in the current directory, and then look in its subdirectories.

-fstype: Find files located in a certain type of file system. These file system types can usually be found in the configuration file /etc/fstab, which contains information about the file system in this system.

-mount: Do not cross the file system mount point when looking for files.

-follow: If the find command encounters a symbolic link file, it will follow the file pointed to by the link.

-cpio: Use the cpio command on matching files to back them up to tape devices.

In addition, the difference between the following three:

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

-atime n Find files accessed in the last n*24 hours on the system

-cmin n Find files in the system whose file status has been changed in the last N minutes

-ctime n Find files whose file status has been changed in the last n*24 hours in the system

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

-mtime n Find files with changed file data in the last n*24 hours in the system

5. Example of use:

Example 1: Find files modified within a specified time 

Order:

           find -atime -2

output:

[root@peidachang ~]# find -atime -2

.

./logs/monitor

./.bashrc

./.bash_profile

./.bash_history

illustrate:

Find files modified within 48 hours 

Example 2: Search by keyword 

Order:

find . -name "*.log"

output:

[root@localhost test]# find . -name "*.log" 

./log_link.log

./log2014.log

./test4/log3-2.log

./test4/log3-3.log

./test4/log3-1.log

./log2013.log

./log2012.log

./log.log

./test5/log5-2.log

./test5/log5-3.log

./test5/log.log

./test5/log5-1.log

./test5/test3/log3-2.log

./test5/test3/log3-3.log

./test5/test3/log3-1.log

./test3/log3-2.log

./test3/log3-3.log

./test3/log3-1.log

illustrate:

Find files ending in .log in the current directory. "." represents the current directory 

Example 3: Find files according to the permissions of directories or files

Order:

find /opt/soft/test/ -perm 777

output:

[root@localhost test]# find /opt/soft/test/ -perm 777

/opt/soft/test/log_link.log

/opt/soft/test/test4

/opt/soft/test/test5/test3

/opt/soft/test/test3

illustrate: 

Find files with permissions of 777 in the /opt/soft/test/ directory

Example 4: Find by type 

Order:

find . -type f -name "*.log"

output:

[root@localhost test]# find . -type f -name "*.log"

./log2014.log

./test4/log3-2.log

./test4/log3-3.log

./test4/log3-1.log

./log2013.log

./log2012.log

./log.log

./test5/log5-2.log

./test5/log5-3.log

./test5/log.log

./test5/log5-1.log

./test5/test3/log3-2.log

./test5/test3/log3-3.log

./test5/test3/log3-1.log

./test3/log3-2.log

./test3/log3-3.log

./test3/log3-1.log

[root@localhost test]#

illustrate:

Find common files ending with .log in the current directory 

Example 5: Find and sort all current directories

Order:

find . -type d | sort

output:

[root@localhost test]# find . -type d | sort

.

./scf

./scf/bin

./scf/doc

./scf/lib

./scf/service

./scf/service/deploy

./scf/service/deploy/info

./scf/service/deploy/product

./test3

./test4

./test5

./test5/test3

[root@localhost test]#

Example 6: Find files by size

Order:

find . -size +1000c -print

output:

[root@localhost test]#  find . -size +1000c -print

.

./test4

./scf

./scf/lib

./scf/service

./scf/service/deploy

./scf/service/deploy/product

./scf/service/deploy/info

./scf/doc

./scf/bin

./log2012.log

./test5

./test5/test3

./test3

illustrate:

Find files larger than 1K in the current directory 

Guess you like

Origin blog.csdn.net/a1058926697/article/details/131174405