Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

File search and location command for Linux

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article gives you a detailed introduction to the commands for finding and locating files in Linux. Most of the knowledge points are often used by everyone, so I will share them for you as a reference. Let's follow the editor and have a look.

Everything is a file under Linux, but because of the different attributes of the file, we can do corresponding search for different files in order to speed up the query and reduce the consumption of resources. Note that lookups on disk are subject to permission control, but lookups in the database are not.

The most commonly used in 1 Linux is the which command, which searches for executable files in the path specified in the $PATH environment variable (the default path for saving executable files). It is generally used to determine whether software is installed, such as to see if gcc software is installed.

Example 1.1

[user1@localhost ~] $which gcc/bin/gcc # installed gcc software

Example 1.2 the general lookup is the displayed absolute path

[root@localhost ~] # which pwd/bin/pwd

Example 1.3 ordinary users cannot find commands with only root permissions:

[user1@localhost ~] $whichuserdel / usr/bin/which:nouserdelin (/ usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/user1/.local/bin:/home/user1/bin) [user1@localhost ~] $su-Password: Last login: Sun Feb 26 10:20:33CST 2017 onpts/0 [root@localhost ~] # whichuserdel / sbin/userdel

2. Type command, this command is special, it does not have any search ability from the name. It is actually a command used to determine whether a command is a built-in command for shell. If it is a non-built-in shell command and the-p argument of type is used, it is equivalent to the which command

Example 2.1 compare the difference between which and type-p

[user1@localhost ~] $which pwd # pwd is shell's built-in command / bin/pwd [user1@localhost ~] $type-p pwd # No result, no search function [user1@localhost ~] $[user1@localhost ~] $which gcc # gcc is software, non-shell built-in command / bin/gcc [user1@localhost ~] $type-p gcc # is equivalent to which/bin/gcc

The fastest command to find in 3.Linux, the whereis command, does not look for files slowly on disk, but quickly finds files in data (/ var/lib/mlocate). This mlocate database is automatically created by the Linux system, contains information about local files, and automatically executes updatedb commands every day to update the database. It is precisely because it is updated once a day, so sometimes the search is not accurate, such as just added files can not be found, just deleted files can also be searched. So when you generally use it, you first execute the updatedb command manually, and then whereis. This is a bit of a loss of efficiency. It can only find executable files, online help documentation, and source code files.

Example 3.1 shows sudo command related: command location, command configuration file, command help manual

[root@localhost ~] # whereis sudosudo: / usr/bin/sudo / etc/sudo.conf/usr/share/man/man8/sudo.8.gz

Example 3.2 find the sudo command

[user1@localhost ~] $whereis sudosudo: / usr/bin/sudo / etc/sudo.conf/usr/share/man/man8/sudo.8.gz [user1@localhost ~] $whereis-b sudo # only look for binary files sudo: / usr/bin/sudo / etc/sudo.conf [user1@localhost ~] $which sudo # and which search for comparison / bin/sudo

The 4.locate command, which is a common mlocate database with whereis, differs from whereis in that it is more detailed. It lists all the files that contain the target file! Isn't it a little complicated?

Example 4.1 find all files containing iptables

[user1@localhost] $locate iptables/etc/sysconfig/iptables-config/usr/bin/iptables-xml/usr/sbin/iptables... [user1@localhost ~] $locate-c iptables# counts the number of files containing iptables (lines) 18

Example 4.2 if a temporary file is established, and then look for this temporary file, the database must be updated manually

[user1@localhost ~] $cd data/ Mkdiraaaaaaaaa AAAAAAAAAAAA [user1@localhost data] $lsaaaaaaaaa AAAAAAAAAAAA [user1@localhost data] $locate-r "aaaaaaa*" [user1@localhost data] $locate-r "AAAAAAAAAA*" there is already an object file in the system at this time, but the database on which locate depends has not been updated, so it cannot be retrieved (- r is a parameter that matches the regular expression) Next, update the database [user1@localhost data] $updatedb updatedb: can not open a temporary filefor` / var/lib/mlocate/mlocate.db' to indicate that there are not enough permissions here! Switch to root to refresh [user1@localhost data] $su-Password: Last login: Sun Feb 26 14:29:17 CST 2017 onpts/0 [root@localhost ~] # updatedb [root@localhost ~] # su-user1Last login: Sun Feb 26 14:29:30 CST 2017 onpts/0 then you can locate the file [user1@localhost ~] $locate-r "aaaaaa*" / home/user1/data/aaaaaaaaa [user1@localhost ~] $locate-r "AAAAAAAAAAA*" / home/user1/data/ AAAAAAAAAAAA notes that: (- I represents a case-insensitive parameter) the-r parameter must be the last of the parameter! [user1@localhost] $locate-ri "aaaaaaa*" locate: non-option arguments are notallowed with-- regexp [user1@localhost ~] $locate-ir "aaaaaaa*" / home/user1/data/AAAAAAAAAAAA/home/user1/data/aaaaaaaaa [user1@localhost ~] $locate-rc "aaaaa*" locate: non-option arguments are notallowed with-- regexp [user1@localhost ~] $locate-cr "aaaaa*" 2

5.The whatis command, which looks for commands, system calls, library functions, or special file names specified by the command parameter in the database created with the catman-w command. The whatis command displays the header line in the manual section. You can then issue the man command to get additional information. The whatis command is equivalent to using the man-f command. Is to search for commands in the whatis database. The Whatis database contains a brief description of all the commands in the system.

The function of example 5.1 whatis is to output simple description information of system commands, which is generally used a little. Note that it does not have the ability to output custom commands

[user1@localhost] $whatis lsls (1)-list directory contentsls (1p)-list directory contents [user1@localhost ~] $su-Password: Last login: Sun Feb 26 12:04:01 CST2017 onpts/0 [root@localhost ~] # whatis lsls (1)-list directory contentsls (1p)-list directory contents [root@localhost ~] #

6.apropos command, which can search for strings in the whatis database and output a short description of the system commands contained in a set of database files for keywords (for example, to complete a task, but do not know which commands, you can use apropos, of course, you can also use the-k parameter of man). Note: if the system commands increase, you need to update the database (update whatis), so that you can be accurate!

Example 6.1 find system commands related to the string "mkdir"

[root@localhost] # man-k mkdirgvfs-mkdir (1)-Create directoriesmkdir (1)-makedirectoriesmkdir (1p)-makedirectoriesmkdir (2)-createa directorymkdir (3p)-make adirectorymkdirat (2)-createa directoryrelative to adirectory file descriptor [root@localhost ~] # apropos mkdirgvfs-mkdir (1)-Create directoriesmkdir (1)-makedirectoriesmkdir (1p) -makedirectoriesmkdir (2)-createa directorymkdir (3p)-make adirectorymkdirat (2)-createa directoryrelative to adirectory file descriptor

7.find command, super powerful command. It is omnipotent, but the speed is a little slow, because it is in the specified disk location meticulous search, very carefully. Its power lies in that there are many parameters, but also can be followed by the operation!

Command format: find [path] [option] [- exec |-ok command {}\;]

Command interpretation:-exec (non-interactive) is the same as-ok (interactive), {}\; means that the output is handed over to command for processing.

Parameter list

Parameters.

explain

-name

Find by file name

-perm

Find according to file permissions

-user

Find according to the user of the file

-group

Find by the group of the file

-atime n

For files that have been accessed (access_time) in the past n days, n represents the number of days,-n represents the past n days, + n represents the past n days ago, and n represents the determined date (the nth day of the past)

-ctime n

For files that have been changed (change_time) in the past n days, n represents the number of days, as above

-amin n

For files that have been accessed (access_time) in the past n minutes, n represents the number of minutes, as above

-cmin n

For files that have been changed (change_time) in the past n minutes, n represents the number of minutes, as above

-size n [ckMG]

Find by file size, n for size, + n for files greater than n,-n for files less than n, c for bytes, k for kB,M for MB,G for GB

-newer F1

Find files whose update time is closer than F1

-anewer F1

Find files whose access time is earlier than F1

-empty

Find an empty file, which can be a directory or file

-type

Find by file type

-fstype

Find by file system

-nogroup

Find files that do not belong to a group

-nouser

Find files without users

-uid

Search according to the UID of the user to which the file belongs

-gid

Search by GID of the group to which the file belongs

-inum

Search by the inode node number of the file

-readable

Only match readable files

-samefile

Find files with the same indode

-writale

Match writable files

-links

Find by the number of file links

-depth

Cause the lookup to look up this directory before entering the subdirectory.

-mount

Find files without crossing the file system mount point

-cpio

Back up the files found

-cpio%

Look for files according to the file system, which is generally recorded in the / etc/fstab file

-prune

Ignore a directory

File type:

Character

explain

B

Block device file

C

Character device file

D

Catalog file

L

Link file

F

Ordinary document

P

Pipe file

S

Socket file

Example 7.1 find the file with a character in the / home/user1/data directory and delete it

[user1@localhost data] $lsaaaaaaaaa AAAAAAAAAAAA sssss [user1@localhost data] $lsaaaaaaaaa / file11 file14 [user1@localhost data] $find. /-name "* a *" | rm-rrm: missing operandTry'rm-- help' for more information. The error message says that rm does not have an Operand because rm cannot receive data from standard input! (there are also those who cannot receive standard input data:) [user1@localhost data] $find. /-name "* a *" | xargs rm-r [user1@localhost data] $lsAAAAAAAAAAAA sssss [user1@localhost data] $method 2: note that there will be a prompt after deletion! [user1@localhost data] $lsaaaaa AAAAAAAAAAAA sssss [user1@localhost data] $lsaaaaa / file1 file2 [user1@localhost data] $find. /-name "* a *"-exec rm-r {}\; find:'. / aaaaa': No such file or directory [user1@localhost data] $lsAAAAAAAAAAAA sssss#### here to talk about-exec. -exec is the subsequent action of find, that is, the interface for executing commands on the result set of find. # "{}" is a placeholder that represents the result set of the find operation. # "\;" indicates the end of-exec. It's strange to say in the man document that the end of the-exec command is ";", but in fact # is "\;" what does that mean? is the man document wrong? No, it's shell's fault, # ";" it has a special meaning in the environment of bash (the default shell in Linux, almost all distributions do), so we use "\" to escape.

7.2 find files in a mixed way, looking for files larger than 1m in the current directory and accessed one day ago (- and is related to;-or is or related)

[root@localhost ~] # find / etc/-size + 1M-and-atime + 1/etc/selinux/targeted/active/policy.kern/etc/selinux/targeted/contexts/files/file_contexts.bin/etc/selinux/targeted/policy/policy.30/etc/gconf/schemas/ekiga.schemas/etc/brltty/zh-tw.ctb#### Note: find is restricted by the permissions of the target file. When ordinary users look for files that only root has access to, Will report an error: [user1@localhost ~] $find / etc/-size + 10M-and-atime + 1find:'/ etc/pki/CA/private': Permissiondeniedfind:'/ etc/pki/rsyslog': Permissiondeniedfind:'/ etc/dhcp': Permissiondeniedfind:'/ etc/lvm/archive': Permissiondeniedfind:'/ etc/lvm/backup': Permissiondeniedfind:'/ etc/lvm/cache': Permissiondeniedfind:'/ etc/selinux/targeted/active':Permission denied

7.3 permissions-Note when using the perm parameter:-007 represents a file with a minimum permission of 007, while 007 represents a file with a permission of 007 (that is, a file that all users can read and write to execute);-100 represents a file with a minimum permission of 100 (that is, the user executable file to which it belongs).

[user1@localhost data] $lltotal 0-rwx 1 user1 user1 0 Feb 26 16V 28file1MUM x-1 user1 user1 0 Feb 26 16VZ 28file2 RWMUR RWMURAR-1 user1 user1 0 Feb 26 16:29file3 [user1@localhost data] $find. /-perm 007./file1 [user1@localhost data] $find. /-perm-007./file1 [user1@localhost data] $find. /-perm 100./file2 [user1@localhost data] $find. /-perm-100././file2 [user1@localhost data] $

7.4 search without error prompt: if you find an error, throw away the error message (/ dev/null file is equivalent to a dustbin in Windows, except that any file placed in this directory will be emptied instantly, so this directory is also empty! ), and if it is found correctly, the result is printed

[user1@localhost data] $find. /-nam "aaa" find: unknown predicate `- nam' [user1@localhost data] $find. /-nam "aaa" 2 > / dev/null

Summary:

Command

Retrieve files

Retrieval object

Display content

Efficiency.

Which

File / bin,/sbin in $PATH

Command, binary file

Absolute path

Root users are the most convenient to use, and ordinary users sometimes have permission problems when using them.

Whereis

Mlocate database

Executable files, help documentation, source code files

Absolute path, profile path, help document path

Any user can use it. If there is no permission problem, it is necessary to update.

Whatis

Whatis database

System command

Brief introduction of target command

Fast, get a set of profiles of commands

Apropos

Whatis database

System commands containing keywords

A brief introduction to a set of related commands

Fast, get a set of profiles of keywords

Locate

Mlocate database

File

Target file list

Fast, but the index database is updated automatically every day, and sometimes it is not timely.

Find

File system

File

Target file list

Powerful, often used as a prerequisite to pave the way for follow-up operations!

These are the details of the file search and location commands in Linux. Do you have anything to gain after reading them? If you want to know more about it, you are welcome to follow the industry information!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report