Search for files in Linux

Source: Internet
Author: User
I was asked about how to find specific documents during the recent interview. I would like to summarize this question. I. problem description: find the file with the specified extension in the specified directory. first, Introduction 2,

I was asked about how to find specific documents during the recent interview. I would like to summarize this question.

I. problem description:

First, we will introduce

2. common file search commands in Linux: 1. find

Find is the most common and powerful search command. you can use it to find any file you want.

The format of find is as follows:

$ Find <指定目录> <指定条件> <指定动作>

- <指定目录> : The directory to be searched and all its subdirectories. The current directory is used by default.

- <指定条件> : The features of the file to be searched.

- <指定动作> : Specific processing of search results.

If no parameters are added, find searches for the current directory and its subdirectories by default, does not filter any results (that is, returns all files), and displays them all on the screen.

========================================================== ================

-Name filename # search for a file named filename
-Perm # search by execution permission
-User username # search by file owner
-Group groupname # search by group
-Mtime-n + n # find the file based on the file change time.-n indicates that the file is less than n days, and + n indicates that the file is earlier than n days.
-Atime-n + n # Query GIN by file access time: 0px ">

-Ctime-n + n # find the file based on the file creation time.-n indicates that the file is less than n days, and + n indicates that the file is earlier than n days.

-Nogroup # Check for files with no valid group, that is, the file group does not exist in/etc/groups.
-Nouser # check files with no valid owner, that is, the file owner is not stored in/etc/passwd.
-Newer f1! F2 file search,-n refers to n days or less, + n refers to n days ago
-Ctime-n + n # find the file based on the file creation time.-n indicates that the file is less than n days, and + n indicates that the file is earlier than n days.
-Nogroup # Check for files with no valid group, that is, the file group does not exist in/etc/groups.
-Nouser # check files with no valid owner, that is, the file owner is not stored in/etc/passwd.
-Newer f1! F2 # Query files whose modification time is earlier than f1 but earlier than f2
-Type B/d/c/p/l/f # Check block devices, directories, character devices, pipelines, symbolic links, and common files.
-Size n [c] # Query objects with a length of n [or n bytes]
-Depth # search for the local directory before entering the subdirectory
-Fstype # Query files whose change time is newer than f1 but earlier than f2
-Type B/d/c/p/l/f # Check block devices, directories, character devices, pipelines, symbolic links, and common files.
-Size n [c] # Query objects with a length of n [or n bytes]
-Depth # search for the local directory before entering the subdirectory
-Fstype # Query files in a certain type of file system. These file system types can be found in/etc/fstab.
-Mount # The mount point of the file system is not crossed during File query.
-Follow # if a symbolic link file is encountered, the file indicated by the link will be tracked.
-Cpio %; # Query files in a certain type of file system. These file system types can be found in/etc/fstab.
-Mount # The mount point of the file system is not crossed during File query.
-Follow # if a symbolic link file is encountered, the file indicated by the link will be tracked.
-Cpio # Use the cpio command to back up the matching files to the tape device.
-Prune # ignore a directory

========================================================== ==================

Some examples:

$ Find ~ -Name "*. txt"-print example: check the. txt file and display it in home ‑.
$ Find.-name "commana-z00000000a-z000000000-9000000000-90000.txt"-print # Query txt files starting with two lower-case letters and two numbers
$ Find.-perm-007-exec ls-l {}\; # Query all files that can be read and written by users.

$ Find.-size + 000000c-print # Query files with a length greater than 1 MB

$ Find.-size 100c-print # Query files with a length of C

Find/home-mmin + 60 check files modified 60 minutes ago under/home
Find/home-name tmp.txt-maxdepth 4 list/The tmp.txt in homecan be found at a maximum of three layers.
Find/home-name tmp.txt-mindepth 3 query from Layer 1

2. locate

The locate command is actually another method of writing "find-name", but it is much faster than the latter because it does not search for a specific directory, instead, search for a database (/var/lib/locatedb) that contains information about all local files. The Linux system automatically creates the database and updates the database once a day. Therefore, the latest changed files cannot be found using the locate command. To avoid this problem, you can useUpdatedbCommand to manually update the database.

Use instance of the locate command:

  $ Locate/etc/sh

Search for all files starting with sh in The etc directory.

3. whereis

The whereis command can only be used for searching program names, and only binary files (parameter-B), man description files (parameter-m), and source code files (parameter-s) can be searched ). If the parameter is omitted, all information is returned.

Examples of using the whereis command:

  $ Whereis grep

4. which

The which command searches for the location of a system command in the PATH specified by the PATH variable and returns the first search result. That is to say, by using the which command, you can see whether a system command exists and where the command is executed.

Use instance of the which command:

  $ Which grep

III. The following describes grep:

$ Grep "searched string" file name

Example: Find the. in file containing the specified string in the first-level folder in the current directory.

Grep "thermcontact" */*. in

Find the row matching the regular expression from the file content:

$ Grep-e "regular expression" file name

Case insensitive when searching:

$ Grep-I "searched string" file name

Find the number of matched rows:

$ Grep-c "searched string" file name

Find rows that do not match the specified string from the file content:

$ Grep-v "searched string" file name

4. The combination of the two can be as desired:

Search for all text files with the extension. log in the root directory and find the rows containing "ERROR ".

Find/-type f-name "*. log" | xargs grep "ERROR"

Example: search for all text files with the extension. in from the current directory and find the rows containing "thermcontact"

Find.-name "*. in" | xargs grep "thermcontact"

(Xargs is a common command for Unix and Unix-like operating systems. It is used to convert the parameter list into small segments and pass it to other commands to avoid the problem that the parameter list is too long .) Bytes

Related Article

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.