Linux learning notes (5) file search commands for common Linux commands and Common commands for learning notes
(1) find
The find command is used for file search. Its path is/bin/find. Its Syntax format is:
Find [search range] [matching condition]
1)-name: search by file name, such as searching for the init file under/etc.
[root@localhost ~]# find /etc/ -name init/etc/sysconfig/init/etc/kdump-adv-conf/kdump_initscripts/init/etc/init[root@localhost ~]#
Note:-iname is case-insensitive. Unlike Windows, files searched by-name are searched only when they are exactly the same.
2)-size: search by file size. For example, search for files larger than MB in the root directory:
[Root @ localhost ~] # Find/-size + 204800 find: "/proc/1628/task/1628/fd/5": find: "/proc/1628/task/1628/fdinfo/5": No file or directory find: "/proc/1628/fd/5": No file or directory find: "/proc/1628/fdinfo/5": No file or directory [root @ localhost ~] #
Note: "+" before num indicates greater than, "-" indicates smaller than, and the second file size needs to be converted. Linux uses data blocks as the unit, one data block is 512 bytes (0.5 kb ).
3)-user: search by owner and-group: search by group. For example, find the file "find/home-user username" with username in the/home directory.
4)-amin: access by access time,-cmin: change by File Attribute Modification time,-mmin: search by the time when the file content is modified (modify ). For example, in/etc, find the file whose attributes have been modified within 5 minutes:
Find/etc-cmin-5
Note: + 5 indicates that the time exceeds 5 minutes, and-5 indicates that the time is within 5 minutes.
5) connection options:-a and-o
-A indicates the relationship with (and), and-o indicates or (or). For example, in/etc, find files larger than 80 M and smaller than M: find/etc-size + 163840-a-size-204800.
6) connection options:-exec and-OK
Format:-exec/-OK [command] {}\;
"{}" Contains the search result. "\" indicates the transfer character, semicolon ";" indicates the end of the command. -Exec and-OK are used to search for files and execute specific commands. The difference between the two is that the former runs directly, and the latter needs to ask for confirmation. For example, search for the inittab file in/etc and display its details:
[Root @ localhost ~] # Find/etc/-name inittab-exec ls-l {}\;-rw-r --. 1 root 884 January 7 22:27/etc/inittab [root @ localhost ~] #
7)-type and-inum
-The type option is based on the file type. "f" indicates the file, "d" indicates the directory, and "l" indicates the soft link file. For example, search for files starting with init in/etc:
[root@localhost ~]# find /etc/ -name init* -a -type f/etc/sysconfig/init/etc/sysconfig/network-scripts/init.ipv6-global/etc/kdump-adv-conf/kdump_initscripts/init/etc/init/init-system-dbus.conf/etc/selinux/targeted/contexts/initrc_context/etc/inittab[root@localhost ~]#
-The inum option is based on the I node search. in Linux, each file has an I node number. It can be displayed through the ls-l command. The I node number is used: when you perform operations on a file with complex file names, you can use the I node number to perform operations. At the same time, you can find the hard link information.
(2) locate
The locate command is used to search for files in the file database. It is a quick search tool. Its Syntax format is:
Locate [command name]
The-I option is not case sensitive for search.
Note: The newly created file cannot be searched using locate, because the file has not been updated to the file database. If you need to find it successfully, you need to use updatedb to update it. In addition, files created under/tmp are not included in the file database, so they cannot be searched for files in this folder.
Search for the inittab File
[root@localhost ~]# locate inittab/etc/inittab/usr/share/man/man5/inittab.5.gz/usr/share/vim/vim72/syntax/inittab.vim[root@localhost ~]#
Create a new file in the/root directory, update it using updatedb, and search for it:
[root@localhost ~]# touch text.txt[root@localhost ~]# locate text.txt[root@localhost ~]# updatedb[root@localhost ~]# locate text.txt/root/text.txt
(3) which
The which command is used to search for the Directory and alias of a command. The path is/usr/bin/which. The syntax format is:
Which [command name]
For example, search for information about the rm command:
[root@localhost ~]# which rmalias rm='rm -i' /bin/rm
Alias is the alias.
(4) whereis
The whereis command is used to search for the directory where the command is located and the path of the help document. The path is/usr/bin/whereis. Its Syntax format is:
Whereis [command name]
For example, find the ls command information:
[root@localhost ~]# whereis lsls: /bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
(5) grep
The grep command is used to search for matched lines in the file and output them. The path is/bin/grep. Its Syntax format is:
Grep-iv [specified string] [file]
The-I option indicates case insensitive, and the-v option indicates that the specified string is excluded for display.
For example, search for the mysql row in install. log under/root:
[Root @ localhost ~] # Grep mysql install. log install mysql-libs-5.1.71-1.el6.i686
For example, view the lines starting with # In the inittab file under/etc.
[root@localhost ~]# grep -v ^# /etc/inittabid:3:initdefault: