Linux File Search-find command, linux search-find

Source: Internet
Author: User

Linux File Search-find command, linux search-find
Find command: used to search for and process files in the file tree

1. find command format;

Find pathname-options [-print-exec-OK...] {} \

2. Parameters of the find command;

Pathname: directory path searched by the find command. For example, "." is used to represent the current directory, and "/" is used to represent the system root directory.

-Print: The find command outputs matching files to the standard output.

-Exec: The find command executes the shell command given by this parameter on the matching file. The corresponding command is in the form of 'command' {};. Note the space between {} And.

-OK: The Role of-exec is the same, but the shell command given by this parameter is executed in a safer mode. A prompt is displayed before each command is executed, let the user determine whether to execute.

3. find Command Options:

-Depth: when searching for a file, first find the file in the current directory and then find it in its subdirectory.

-Name: search by file name

-Perm: search by File Permission

-Prune: The find command can be used to not search in the current directory. If the-depth option is used at the same time,-prune will be ignored by find.

-User: search by file owner

-Group: search by file group

-Nouser: Search for files without a valid owner

-Nogroup: searches for files without a valid group.

-Newer file1 file2: Find Files whose change time is earlier than file1 but earlier than file2

-Type: searches for a certain type of files, such as f-common files, d-directories, c-character files, B-Block devices, p-pipeline files, and l-symbol connections.

-Size: [c] searches for files with a length of n blocks. If a file contains c, the file length is measured in bytes.

-Fstype: searches for files belonging to a certain type of file system.

-Mount: the mount point of the file system is not crossed during file search.

-Follow: If the find command encounters a symbolic connection file, it traces the file to which the connection points.

-Cpio: Use the cpio command to back up the file to the tape device.

-Mtime-n + n: query by the file modification time.-n file modification time is earlier than n days, and + n file modification time is earlier than n days.

-Amin n: searches for files accessed in the last n minutes in the system.

-Atime n: searches for the last n * 24 hours of files in the system.

-Cmin n: Find the file whose status is changed in the last n minutes in the system.

-Ctime n: Find the file whose status is changed in the last n * 24 minutes in the system.

-Mmin n: Find the file whose data is changed in the last n minutes in the system.

-Mtime n: Find the file whose data has been changed for the last n * 24 hours in the system.

4. Example:

1) Search for files modified within the specified time: (search for files modified within 48 hours)

[root@bogon ~]# find -atime -2../.bash_profile

2) search by Keyword: (find the file ending with. log in the current directory)

[root@bogon log]# find . -name "*.log"./audit/audit.log./tuned/tuned.log

3) search by File Permission: (find the file with 755 permission under the/tmp directory)

[root@bogon ~]# find /tmp -perm 755 

4) search by file type:

[Root @ bogon ~] # Find.-type d-print # find the directory under the current directory ../data [root @ bogon ~] # Find .! -Type d-print # Find the non-directory./. bash_logout./. bash_profile in the current directory

5) search by file size: (find files larger than 1 kb in the current directory)

[root@bogon ~]# find -size +1000c -print../anaconda-ks.cfg./.bash_history

6) search by file name

[root@bogon ~]# find . -name "*.txt"./test.txt 

5. Execute the shell command using exec or OK:

As long as you write the desired operation in a file, you can use exec to search with the find command

1) display all files larger than 10 kb in the current directory. Execute a command after exec, and {} indicates the file name.

[root@bogon ~]# find . -type f -size +10k -exec ls -l {} \;-rw-r--r--. 1 root root 12288 May  4 17:22 ./.test.txt.swp-rw-------. 1 root root 40960 May  4 17:25 ./.swp

2) Find and delete the files 10 days ago under/tmp.

[root@bogon ~]# find /tmp -type f -mtime +10 -ok rm {} \;

3) backup the configuration file and add the. bak extension.

[root@bogon ~]# find . -name "*.conf" -exec cp {} {}.bak \;-rw-r--r--. 1 root root 11753 May  6 18:03 httpd.conf-rw-r--r--. 1 root root 11753 May  6 18:04 httpd.conf.bak

4) Find the files written by other users in the main directory.

[root@bogon ~]# find ~ -perm -002 -exec chmod o-w {} \;

6.-print matching conditions:

The abbreviated expressions are evaluated in order, and-a and-o are both short-circuit values.

1) Ignore a directory (search for files in the httpd directory but do not want to search for files in the/httpd/conf directory)

[root@bogon etc]# find httpd -path "httpd/conf" -prune -o -printhttpdhttpd/conf.dhttpd/conf.d/READMEhttpd/conf.d/autoindex.conf

2) Match multiple conditions and use the OR condition operation.

[root@bogon ~]# find . \( -name "*.txt" -o -name "*.conf" \) -print./httpd.conf./test.txt

7. xargs command:

Some commands cannot accept too many parameters. In this case, the command execution may fail. The error message is usually "the parameter column is too long" or "the parameter column overflows". This is the use of xargs commands.
Find | xargs COMMAND

Example: filter the word "nologin" in a common file in the current directory.

[root@bogon ~]# find . -type f -print |xargs grep "nologin"Binary file ./.test.txt.swp matches./test.txt:x:1:1:bin:/bin:/sbin/nologin./test.txt:on:x:2:2:daemon:/sbin:/sbin/nologin

8. locate command:

The pre-built file index database on the query system relies on the first index construction. The construction process requires traversing the entire root file system, which consumes a lot of resources.

Features: fast search, fuzzy search, and non-real-time search.

-I: perform case-sensitive searches.

-N x: only lists the first x matching options.

 

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.