Linux Learning Notes (5) file Search command for Linux common commands

Source: Internet
Author: User

(1) Find

The Find command is used for file searches, where the path is/bin/find, with the syntax in the following format:

Find [Search Range] [match condition]

1)-name: Search by file name, such as searching for the init file under/etc

[Email protected] ~]# find/etc/-name init/etc/sysconfig/init/etc/kdump-adv-conf/kdump_initscripts/init/etc/init[[ Email protected] ~]#

Note:-iname is not case-sensitive, and unlike windows, files that are searched by-name are searched only if they are identical.

2)-size: Search by file size, such as searching for files larger than 100M in the root directory:

[[email protected] ~]# Find/-size +204800find: "/PROC/1628/TASK/1628/FD/5": No file or directory 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 [[email protected] ~]#

Note: num front "+" means greater than, "-" means less than, second file size needs to be converted, Linux in the data block, 1 data block is 512 bytes (0.5k).

3)-user: Find by Owner,-group: Find by owning group, such as find files with owner username in the/home directory: Find/home–user username.

4)-amin: Search by Access time (Access),-cmin: Search by the time the file property was modified (change),-mmin: Search by the time the file content was modified (modify). If you are looking for files that have been modified within 5 minutes in/etc:

Find/etc–cmin-5

Note: +5 means more than 5 minutes,-5 means within 5 minutes.

5) connection options:-A and-O

-A represents a relationship with (and), O-means or (or), such as looking for files larger than 80M and less than 100M in/etc: Find/etc–size +163840–a–size-204800.

6) connection options:-exec and-ok

The format is:-exec/-ok [command] {} \;

where "{}" contains the result of the search, "\" means the transfer character, semicolon ";" Indicates the end of the command. -exec and-ok are used to search after a file after a specific command is executed. The difference between the two is that the former is executed directly, and the latter requires confirmation. If you look up the Inittab file in/etc and display its details:

[Email protected] ~]# find/etc/-name inittab-exec ls-l {} \;-rw-r--r--. 1 root root 884 January   7 22:27/etc/inittab[[email protected] ~]#

7)-type and-inum

The-type option is found based on the file type, where "F" means the file, "D" for the Directory, and "L" for the soft-link file. To find files that begin with/etc under init:

[Email protected] ~]# 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[[email protected] ~]#

The-inum option is based on the I-node lookup, each file in Linux has an I node number, through the ls–l command can be displayed, the function of the I node number is: When a file name is very complex operation, you can use the I node number to operate, while the I node number can find hard link information.

(2) Locate

The locate command is used to find files in a file repository, and is a quick Find tool in the syntax format:

Locate [file name]

The Join-I option indicates a case-insensitive lookup.

Note: The newly created file cannot be found using locate because the file has not been updated to the file repository and will need to be updated with the UpdateDB command to find it successfully. In addition, files created in/TMP are not included in the file repository, and therefore cannot be found in files under that folder.

Find Inittab files

[Email protected] ~]# Locate inittab/etc/inittab/usr/share/man/man5/inittab.5.gz/usr/share/vim/vim72/syntax/ Inittab.vim[[email protected] ~]#

Create a new file under the/root directory, update it with UpdateDB, and then look for it:

[[email protected] ~]# Touch Text.txt[[email protected] ~]# locate Text.txt[[email protected] ~]# updatedb[[email protecte D] ~]# Locate Text.txt/root/text.txt
(3) which

The which command is used to search the directory where the command resides and the alias information, where the path is/usr/bin/which and the syntax format is:

which [command name]

For information such as finding the RM command:

[email protected] ~]# which Rmalias rm= ' rm-i '        /bin/rm

Where alias is the defined alias.

(4) Whereis

The Whereis command is used to search the directory where the command is located and the Help document path, where the path is/usr/bin/whereis, in the syntax format:

Whereis [command name]

To find the command information for LS:

[Email protected] ~]# 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 the file for string-matched rows and outputs, where the path is/bin/grep, with the syntax in the following format:

Grep–iv [Specify string] [file]

Where the-I option indicates case-insensitive, the-V option indicates that the specified string is excluded from display.

Example: Search for the MySQL line in/root under Install.log:

[[email protected] ~]# grep mysql install.log installation mysql-libs-5.1.71-1.el6.i686

Example: View the lines of the Inittab file except # at the beginning of/etc

[Email protected] ~]# grep-v ^#/etc/inittabid:3:initdefault:

Linux Learning Notes (5) file Search command for Linux common commands

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.