Linux command of the popular file search command Detailed (find, locate, which, whereis, grep, wc)

Linux common commands - file search command

A, find command: find the specified file in the directory

The find command is to search directly on the hard disk, if the specified search criteria are too large, the find command will consume more system resources, resulting in excessive pressure service. Therefore, when using the find command to search, try to find more accurate, so consume less system resources, fast lookups.

Basic information of the find command is as follows:

  • Command name: find
  • The path: / bin / find
  • Format: find the search path to search for content
  • Execute permissions: All users
  • Description: Find files in the directory
(1) Search by file name

-name: Search by file name; in Windows search, as long as the file name contains init will be searched out, but in Linux, find command is an exact match, will have exactly the same list and search keywords, it is a Refine search;

[root@root 桌面]# find /etc -name init
/etc/kdump-adv-conf/kdump_initscripts/init
/etc/init
/etc/sysconfig/init

Use wildcards: * indicates a match at all,? It represents a single character match;

[root@root 桌面]# find /etc -name *init*
/etc/festival/siteinit.scm
/etc/X11/xinit
/etc/X11/xinit/xinitrc-common
/etc/X11/xinit/xinitrc.d
/etc/X11/xinit/xinitrc
/etc/pam.d/run_init
/etc/kdump-adv-conf/kdump_initscripts
......
[root@root 桌面]# find /etc -name init???
/etc/inittab
[root@root 桌面]# find /etc -name init??
/etc/init.d
/etc/rc.d/init.d

In Linux, strictly case-sensitive and case-insensitive if you want, you can use -iname: search by file name, but does not distinguish between file names;

[root@root 桌面]# find /etc -name init???
/etc/inittab
[root@root 桌面]# touch /etc/INITTAB
[root@root 桌面]# find /etc -name init???
/etc/inittab
[root@root 桌面]# find /etc -iname init???
/etc/INITTAB
/etc/inittab
(2) in accordance with the file size of the search: -size Size: + n greater than equal to less than n -n

1 = 512k, 2048 block = 10M;

[root@root 桌面]# find /etc -size +2048
/etc/selinux/targeted/policy/policy.24
/etc/selinux/targeted/modules/active/policy.kern
/etc/gconf/gconf.xml.defaults/%gconf-tree.xml
(3) According to the owner of the query: -user owner

Note: the owner, referring to the creator; an owner, referring to the creator of the group is located.

[root@redhat 桌面]# find /home -user root
/home
/home/zhangsan/zhang
/home/vftpusers/guest
(4) a query based on an owner
[root@redhat 桌面]# find /home -user root
/home
/home/zhangsan/zhang
/home/vftpusers/guest
(5) based on the time attribute query:
  • -amin: Access Time
  • -cmin: change file attributes (ls -l to see the use of information)
  • -mmin: changing the contents of the file
[root@root 桌面]# find /etc -mmin -30
/etc
/etc/vmware-tools
/etc/gconf/gconf.xml.defaults
/etc/gconf/gconf.xml.mandatory
/etc/gconf/gconf.xml.system
/etc/resolv.conf
/etc/mtab
/etc/tpvmlp.conf
(6) -a: two conditions are met; -o: can be any one of two conditions are satisfied; -type: file type. f -> File, d -> Directory, l -> soft link file
[root@root 桌面]# find /etc -name init* -a -type f
/etc/kdump-adv-conf/kdump_initscripts/init
/etc/inittab
/etc/selinux/targeted/contexts/initrc_context
/etc/init/init-system-dbus.conf
/etc/sysconfig/network-scripts/init.ipv6-global
/etc/sysconfig/init

Here Insert Picture Description

(7) -exec / -ok: the results of the find command to the command invoked by -exec / -ok 2 treated. "{}" On behalf of the find command to query results.
[root@root 桌面]# find /etc -name inittab
/etc/inittab
[root@root 桌面]# find /etc -name inittab -exec ls -l {} \;
-rw-r--r--. 1 root root 884 6月  22 19:26 /etc/inittab
[root@root 桌面]# find /etc -name inittab -ok ls -l {} \;
< ls ... /etc/inittab > ? y
-rw-r--r--. 1 root root 884 6月  22 19:26 /etc/inittab

Two, locate command: Find files in the file library

locate command to find command In contrast, it is very fast search, and consume very little system resources. This is because the locate command does not directly search the hard disk space, but will first establish locate database, and then search by file name in the database, it is to quickly find the search command.

Basic information of this locate command is as follows:

  • Command name: locate
  • The path: / usr / bin / locate
  • Execute permissions: All users
  • Description: File search files by name

1, locate the file name

Note: This experiment did not succeed!

[root@redhat 桌面]# locate inittab
/etc/inittab
/usr/share/man/man5/inittab.5.gz
/usr/share/vim/vim72/syntax/inittab.vim

2, locate Finding a drawback, it is that if we build a new document, then locate command can not find the file; because the locate database can not be updated in real time; but we can updatedb manually query.

[root@redhat 桌面]# locate inittab
/etc/inittab
/usr/share/man/man5/inittab.5.gz
/usr/share/vim/vim72/syntax/inittab.vim

3, document repository locate command is not included in / tmp this temporary file;

[root@root ~]# touch /tmp/zhaoliying
[root@root ~]# updatedb
[root@root ~]# locate zhaoliying
[root@root ~]# 

4, when using the locate command to find the file, if you want case-insensitive, you can add the -i option;

[root@root ~]# touch TANGYAN
[root@root ~]# updatedb
[root@root ~]# locate tangyan
[root@root ~]# locate -i tangyan
/root/TANGYAN
[root@root ~]# 

Three, which commands: command lists the path where

Basic information of which command is as follows:

  • Command name: which
  • The path: / usr / bin / which
  • Execute permissions: All users
  • Functional Description: Lists all the command path

which command to query binary Meanwhile, if the command has an alias, you can also find a command aliases;

[root@root ~]# which cp
alias cp='cp -i'
	/bin/cp
[root@root ~]# which useradd
/usr/sbin/useradd
[root@root ~]# which rm
alias rm='rm -i'
	/bin/rm
[root@root ~]# which ifconfig
/sbin/ifconfig
[root@root ~]# 

Behind the alias field is the alias command, for example: when we execute the rm command itself is no message, but the Linux system, rm is an alias for rm -i, why we have a message, whether to delete the file?

Note: Defining aliases

Format: alias = alias' command the path '
for example:
Alias the dir = "/ usr / bin / LS'
Alias RM = '/ usr / bin / RM -i'

[root@root ~]# cd /tmp
[root@root tmp]# touch tangyan
[root@root tmp]# rm tangyan
rm:是否删除普通空文件 "tangyan"?

Four, whereis command: command search directory where the file path and help

whereis command is a command search system, that is, whereis command can not search for regular files, but can only search system commands. Basic information whereis command is as follows:

  • Command name: whereis
  • The path: / usr / bin / whereis
  • Execute permissions: All users
  • Description: Find a binary command, the command source files, and help documentation

whereis command not only searches the directory where the command can also help document search path, the path for the search command respect, whereis command and which command is no essential difference;

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

Five, grep command: search for a match in a row in the output file

Basic information of grep command is as follows:

  • Command name: grep
  • The path: / bin / grep
  • Format: grep searches search path
  • Execute permissions: All users
  • Description: The search for matches in the document and output lines

1, Options: -i (ignore upper and lower case) -v (reverse lookup I)

[root@root tmp]# grep multiuser /etc/inittab
#   3 - Full multiuser mode
[root@root tmp]# grep -i multiuser /etc/inittab
#   2 - Multiuser, without NFS (The same as 3, if you do not have networking)
#   3 - Full multiuser mode
[root@root tmp]# grep -v "#"  /etc/inittab
id:5:initdefault:

2. Search Terms:

Look for a string beginning with: "^ a string" example: grep "^ #" / etc / inittab

[root@root tmp]# grep "^#" /etc/inittab
# inittab is only used by upstart for the default runlevel.
#
# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# System initialization is started by /etc/init/rcS.conf
#
# Individual runlevels are started by /etc/init/rc.conf
#.......

Look for a string at the end: "A String $" For example: grep ") $" / etc / inittab

[root@root tmp]# grep ")$" /etc/inittab
#   0 - halt (Do NOT set initdefault to this)
#   2 - Multiuser, without NFS (The same as 3, if you do not have networking)
#   6 - reboot (Do NOT set initdefault to this)

Find a blank line: "^ $" For example: grep ") $" / etc / inittab

[root@root tmp]# grep "^$" /etc/yum.conf


[root@root tmp]# 

Six, wc command: Statistics command

Basic information of wc command is as follows:

  • Command name: wc
  • The path: / usr / bin / bin
  • Execute permissions: All users
  • Statistical information file: Functional Description

Wc command parameters are as follows:

  • -l: file wc -l need statistics: statistics such as the number of lines
  • -w: count the number of words such as: wc -w need statistics file
  • -c: File wc -c need statistics: statistics such as the number of bytes
[root@root tmp]# wc -l /etc/services
10774 /etc/services
[root@root tmp]# wc -w /etc/services
58108 /etc/services
[root@root tmp]# wc -c /etc/services
641020 /etc/services
[root@root tmp]# 

Guess you like

Origin blog.csdn.net/weixin_45116657/article/details/94434716