Linux common instruction learning (Part 3): file search command

Tip: after the article is written, the directory can be generated automatically. Please refer to the help document on the right for how to generate it

preface

Environment and related software: VMWare 14, CentOS 7, SecureCRT.

Tip: the following is the main content of this article. The following cases can be used for reference

1, File search command: find

Command path: / bin/find

Execution Authority: all users

Syntax: find [search scope] [matching criteria]

Function Description: File Search

1. Search by file name - name

//Precise search by file name
[root@localhost tmp]# find /etc -name init
/etc/sysconfig/init
/etc/selinux/targeted/active/modules/100/init
//Fuzzy search based on file name (* means matching any character)
[root@localhost tmp]# find /etc -name *init*
/etc/systemd/system/sysinit.target.wants
/etc/inittab
/etc/sysconfig/init
/etc/sysconfig/network-scripts/init.ipv6-global
/etc/init.d
/etc/rc.d/init.d
/etc/selinux/targeted/active/modules/100/init
/etc/selinux/targeted/contexts/initrc_context
/etc/security/namespace.init
//Fuzzy search based on file name (? Means matching a single character)
[root@localhost tmp]# find /etc -name init???
/etc/inittab
[root@localhost tmp]# touch /etc/INITTAB
[root@localhost tmp]# find /etc -name init???                /etc/inittab
//-Replace name with - iname. Is case insensitive
[root@localhost tmp]# find /etc -iname init???
/etc/inittab
/etc/INITTAB

2. Specify file size search - size

//Find files larger than 100MB
[root@localhost tmp]# find / -size +204800
/proc/kcore
find: '/proc/1589/task/1589/fd/6': There is no such file or directory
find: '/proc/1589/task/1589/fdinfo/6': There is no such file or directory
find: '/proc/1589/fd/5': There is no such file or directory
find: '/proc/1589/fdinfo/5': There is no such file or directory
/sys/devices/pci0000:00/0000:00:0f.0/resource1_wc
/sys/devices/pci0000:00/0000:00:0f.0/resource1
/usr/lib/locale/locale-archive

Note: the minimum unit in Linux is data block, and the conversion formula is:

1 data block = 512b (bytes) = 0.5KB

3. Search by owner - user

[root@localhost tmp]# find /home -user yangf
/home/yangf
/home/yangf/.bash_logout
/home/yangf/.bash_profile
/home/yangf/.bashrc

/Home /: home directory of ordinary users. When establishing each user, each user should have a default login location, which is the user's home directory. The home directory of all ordinary users is to establish a directory with the same user name under / home. For example, the home directory of user user1 is home/user1.

4. Search for - amin, - cmin, - mmin according to the time attribute

Example: $find /etc -cmin -5

    find the files and directories whose attributes have been modified in 5 minutes under / etc

   - amin access time access

    - cmin file attribute change

    - mmin file content modify

[root@localhost tmp]# find /etc -mmin -30
/etc
/etc/INITTAB

5. Use connectors to meet multiple search criteria

-a   the two conditions are met at the same time

-o   one of the two conditions can be satisfied

-Type   find according to the file type (f: file, d: directory, l: soft link file)

[root@localhost tmp]# find /etc -name init* -a -type f
/etc/inittab
/etc/sysconfig/init
/etc/sysconfig/network-scripts/init.ipv6-global
/etc/selinux/targeted/contexts/initrc_context

6. Find the specified file and display its details

-Exec command \ / -};   perform operations on search results

-inum   find according to i node

//Find the inittab file under / etc and display its details
[root@localhost tmp]# find /etc -name inittab -exec ls -l {} \;
-rw-r--r--. 1 root root 511 10 June 13, 2020 /etc/inittab
//Fuzzy search for files starting with init under / etc and display their details
[root@localhost tmp]# find /etc -name init* -a -type f -exec ls -l {} \;   
-rw-r--r--. 1 root root 511 10 June 13, 2020 /etc/inittab
-rw-r--r--. 1 root root 798 10 June 13, 2020 /etc/sysconfig/init
-rwxr-xr-x. 1 root root 5419 5 June 22, 2020 /etc/sysconfig/network-scripts/init.ipv6-global
-rw-r--r--. 1 root root 30 10 January 2020 /etc/selinux/targeted/contexts/initrc_context
[root@localhost tmp]# find /home -user yangf -ok rm {} \;      
< rm ... /home/yangf > ? n
< rm ... /home/yangf/.bash_logout > ? n
< rm ... /home/yangf/.bash_profile > ? n
< rm ... /home/yangf/.bashrc > ? n
[root@localhost tmp]# 
[root@localhost tmp]# 
[root@localhost tmp]# touch "qiangge handsome"
[root@localhost tmp]# ls -i
16777665 day1
51041787 day3
16777637 hongjiu
17395469 issue
16778156 issue.hard
16777647 issue.soft
17395463 locale.conf
33625950 ningsy
16777289 pijiu
17395467 program files
16777639 qiangge handsome
17395464 study.list
17395465 sudo.conf
  728723 systemd-private-0c4ab19897584e3f95f9f2ffddea2f98-chronyd.service-2AuwFB
    6616 systemd-private-0dd6a9e7e1024e15ba73db310090c201-chronyd.service-vyFtyd
  537725 systemd-private-86a69b4ef3cf440e9e3cd345c20f1569-chronyd.service-jVuvd3
  728713 systemd-private-d8000b3920d44f71b59a0531fec19d10-chronyd.service-n1KqK5
  728715 systemd-private-edafe6e8a1284b37be6c9a30e921f73f-chronyd.service-3z0bxa
  728724 vmware-root_663-4022243318
    6617 vmware-root_664-2722697761
  728714 vmware-root_666-2731021219
  728716 vmware-root_667-3980363901
  537726 vmware-root_673-3988556249
//Find the file with i node 16777639 in the current directory and delete it
[root@localhost tmp]# find . -inum 16777639 -exec rm {} \;
[root@localhost tmp]# ls
day1           sudo.conf
day3           systemd-private-0c4ab19897584e3f95f9f2ffddea2f98-chronyd.service-2AuwFB
hongjiu        systemd-private-0dd6a9e7e1024e15ba73db310090c201-chronyd.service-vyFtyd
issue          systemd-private-86a69b4ef3cf440e9e3cd345c20f1569-chronyd.service-jVuvd3
issue.hard     systemd-private-d8000b3920d44f71b59a0531fec19d10-chronyd.service-n1KqK5
issue.soft     systemd-private-edafe6e8a1284b37be6c9a30e921f73f-chronyd.service-3z0bxa
locale.conf    vmware-root_663-4022243318
ningsy         vmware-root_664-2722697761
pijiu          vmware-root_666-2731021219
program files  vmware-root_667-3980363901
study.list     vmware-root_673-3988556249

2, Find a file in the file database: locate

   unlike the find command, the find command is powerful and has a wide search range, but the search speed is relatively slow. It is best not to use it when the system load is high; The locate command can only search the file name. The search function is simple, but the search speed is fast, similar to the search of everything software under Windows.

Command path: / usr/bin/locate

Execution Authority: all users

Syntax: locate file name

Function Description: find documents in the document database

Example: $locate inittab

1. If the CentOS system cannot find the locate command, follow the steps below:

[root@localhost tmp]# locate inittab
-bash: locate: Command not found
//1. Install mlocate package
[root@localhost tmp]# yum -y install mlocate
 Plug in loaded: fastestmirror
Determining fastest mirrors
 * base: mirrors.njupt.edu.cn
 * extras: mirrors.ustc.edu.cn
 * updates: mirrors.nju.edu.cn
base                                  | 3.6 kB     00:00     
extras                                | 2.9 kB     00:00     
updates                               | 2.9 kB     00:00     
updates/7/x86_64/primary_db             |  13 MB   00:06     
Resolving dependencies
--> Checking transactions
---> software package mlocate.x86_64.0.0.26-8.el7 Will be installed
--> Resolve dependency complete

Dependency resolution

=============================================================
 Package      framework        edition               source         size
=============================================================
Installing:
 mlocate      x86_64      0.26-8.el7         base      113 k

Transaction summary
=============================================================
Install 1 package

Total downloads: 113 k
 Installation size: 379 k
Downloading packages:
mlocate-0.26-8.el7.x86_64.rpm           | 113 kB   00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing    : mlocate-0.26-8.el7.x86_64                1/1 
  Verification in progress      : mlocate-0.26-8.el7.x86_64                1/1 

already installed:
  mlocate.x86_64 0:0.26-8.el7                                

complete!
//2. Update database
[root@localhost tmp]# updatedb
//3. locate search
[root@localhost tmp]# locate inittab
/etc/inittab

2. If a new file is created

[root@localhost tmp]# touch /root/fuwuqi
//If you create a new document, the document database may not be updated in time. You need to update the database and look it up again
[root@localhost tmp]# locate fuwuqi
[root@localhost tmp]# updatedb          
[root@localhost tmp]# locate fuwuqi
/root/fuwuqi
//If you create a new file in the tmp directory, because the tmp directory does not belong to the search scope of the file database. So we can't find it
[root@localhost tmp]# touch /tmp/kehuduan
[root@localhost tmp]# updatedb           
[root@localhost tmp]# locate kehuduan

Note: the file database is located at / var / lib / mlocate / mlocate DB file. In addition, the system sets some search restrictions in the configuration file of updating the database, so the locate command cannot search the files in the tmp directory. The search restrictions can be found in / etc / updatedb View in conf configuration file:

[root@localhost tmp]# vi /etc/updatedb.conf

PRUNE_BIND_MOUNTS = "yes"
PRUNEFS = "9p afs anon_inodefs auto autofs bdev binfmt_misc cgroup cifs coda configfs cpuset debugfs devpts ecryptfs exofs fuse fuse.sshfs fusectl gfs gfs2 gpfs hugetlbfs inotifyfs iso9660 jffs2 lustre mqueue ncpfs nfs nfs4 nfsd pipefs proc ramfs roo
tfs rpc_pipefs securityfs selinuxfs sfs sockfs sysfs tmpfs ubifs udf usbfs fuse.glusterfs ceph fuse.ceph"
PRUNENAMES = ".git .hg .svn"
PRUNEPATHS = "/afs /media /mnt /net /sfs /tmp /udev /var/cache/ccache /var/lib/yum/yumdb /var/spool/cups /var/spool/squid /va
r/tmp /var/lib/ceph"
  1. PRUNE_BIND_MOUNTS indicates whether search restriction is enabled;
  2. Prenefs refers to the file system that will not be searched during search;
  3. Prunnames indicates the file type that will not be searched during search;
  4. Prenepaths indicates the path that will not be searched during search;

3. Search case insensitive locate -i

[root@localhost tmp]# touch /root/fuwuqI
[root@localhost tmp]# updatedb 
[root@localhost tmp]# locate fuwuqi        
/root/fuwuqi
[root@localhost tmp]# locate -i fuwuqi
/root/fuwuqI
/root/fuwuqi

3, Directory and alias of search command: which

Command path: / usr/bin/which

Execution Authority: all users

Syntax: which command

Function Description: example of directory and alias information of search command: $which ls

[root@localhost tmp]# which rm
alias rm='rm -i'
        /usr/bin/rm
[root@localhost tmp]# ls /root
anaconda-ks.cfg  day2    fuwuqI  test1
day1             fuwuqi  learn2  tmp.soft
[root@localhost tmp]# rm /root/fuwuqI
rm: Delete normal empty file "/root/fuwuqI"?y
//It can be seen that the rm instruction knocked before is the alias rm -i instruction, and - i indicates query confirmation
[root@localhost tmp]# /bin/rm /root/fuwuqi
[root@localhost tmp]# ls /root            
anaconda-ks.cfg  day1  day2  learn2  test1  tmp.soft
[root@localhost tmp]# which cp
alias cp='cp -i'
        /usr/bin/cp
[root@localhost tmp]# which ifconfig
/usr/sbin/ifconfig

4, Directory of search command and help document path: where is

Command path: / usr/bin/whereis

Execution Authority: all users

Syntax: whereis [command name]

Function Description: search the directory where the command is located and the path of the help document

Example: $whereis ls

[root@localhost tmp]# whereis useradd
useradd: /usr/sbin/useradd /usr/share/man/man8/useradd.8.gz
[root@localhost tmp]# whereis rm
rm: /usr/bin/rm /usr/share/man/man1/rm.1.gz

5, Search the file for lines that match the string: grep

Command path: / bin/grep

Execution Authority: all users

Syntax: grep -iv [specify string] [file]

Function Description: search for the line matching the string in the file and output it

    - i   case insensitive

    - v   exclude specified string

Example: # grep MySQL / root / install log

//View inittab file
[root@localhost tmp]# more /etc/inittab
# inittab is no longer used when using systemd.
#
# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
#
# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
#
# multi-user.target: analogous to runlevel 3
# graphical.target: analogous to runlevel 5
#
# To view current default target, run:
# systemctl get-default
#
# To set a default target, run:
# systemctl set-default TARGET.target
[root@localhost tmp]# grep system /etc/inittab
# inittab is no longer used when using systemd.
# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
# systemctl get-default
# systemctl set-default TARGET.target
[root@localhost tmp]# grep -i system /etc/inittab
# inittab is no longer used when using systemd.
# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
# systemctl get-default
# systemctl set-default TARGET.target
[root@localhost tmp]# grep -v ^# /etc/inittab        
[root@localhost tmp]# 

Note: ^ # indicates that lines starting with # are excluded. Since all lines start with #, there is no result.

summary

To be improved...

Keywords: Linux Operation & Maintenance server

Added by daedlus on Mon, 31 Jan 2022 16:40:56 +0200