Tips for using the Linuxfind command in 10 ways

Source: Internet
Author: User
This article mainly introduces 10 tips and methods for using the Linuxfind command. if you need it, refer to the following tag: LinuxFind

The find command traverses down the file hierarchy, matches the files that meet the conditions, and performs corresponding operations.

1. search by file name or regular expression

The option-name parameter specifies the string that must be matched by the file name. we can use the wildcard as the parameter. "* .txtcharacter matches all file names ending with. txt.


Copy codeThe code is as follows:
[Root @ localhost test] # touch your data,log,file,file,log=_00001, 6379_0000.txt,#,. log,. conf}
[Root @ localhost test] # find.-name "*. txt"-print

If you want to match one of multiple conditions, you can use the-o parameter.

Copy codeThe code is as follows: [root @ localhost test] # find. \ (-name "*. txt"-o-name "*. log "\)
Option-iname ignore uppercase/lowercase letters
The option-path parameter can use wildcards to match the file path or file.

2. negative parameters

Find with "!" Deny all file names that do not end with a. txt file.


Copy codeThe code is as follows:
[Root @ localhost test] # find .! -Name "*. txt"-print

3. Directory-based Deep Search

The find command traverses all subdirectories during use. we can use-maxdepth and-mindepth to limit the depth of the find command traversal.
-Maxdepth: specifies the maximum depth;
-Mindepth: specifies the minimum depth.


Copy codeThe code is as follows: [root @ localhost ~] # Find.-maxdepth 1-type f

List all common files in the current directory. These two commands must be followed by the target path.

4. search by file type


Copy codeThe code is as follows: find.-type d-print

File type Type parameter
Common File F
Symbol File L
Directory D
Character device C
Block Device B
Socket S
Fifo P

5. search by file Time

In a Linux file system, each file has three timestamps.
-Atime: The last time the user accessed the file;
Modification time (-mtime): The last modification time of the file content.
Change Time (-ctime): the time when the last change of file metadata (metadata, such as permission or ownership) occurs.
-Atime,-mtime,-ctime as the time parameter, unit is day, you can use + to indicate greater than,-to indicate less.


Copy codeThe code is as follows:
[Root @ localhost ~] # Find.-type f-atime 7
# Print out the file that was accessed 7 days ago
[Root @ localhost ~] # Find.-type f-mtime + 7
# Print files modified for more than 7 days
[Root @ localhost ~] # Find.-type f-ctime-7
# Print the files whose modification time is less than 7 days

Similar parameters include-amin (access time),-mmin (modification time), and-cmin (change time), in minutes.
Another nice feature of find is the-newer parameter. we can specify a parameter file for comparing timestamp, and then find all the files updated than the parameter file.
Find.-type f-newer file.txt
P finds all files that have been modified longer than File.txt in the current directory.
6. search by file size
Search for available units
B -- block (512 bytes); c -- byte; w -- word (2 bytes );
K -- kilobytes; M -- megabytes; G -- kilobytes.


Copy codeThe code is as follows: [root @ localhost tmp] # find.-type f-size 2 k
#2 K files
[Root @ localhost tmp] # find.-type f-size + 2 k
# Files larger than 2 k
[Root @ localhost tmp] # find.-type f-size-2 k
# Files smaller than 2 k

7. delete matching files

-Delete can be used to delete matching files found by find.

Copy codeThe code is as follows: [root @ localhost tmp] # find.-type f-name ". sWp"-delete
# Delete all. swp files in the current directory

8. matching of file permissions and ownership


Copy codeThe code is as follows: [root @ localhost tmp] # find.-type f-perm 644
# Search for files with the current directory permission of 644
[Root @ localhost tmp] # find.-type f-user reed
# Search for files whose owner is reed in the current directory

9. execute commands or actions in combination with find

The find command can be combined with other commands with option-exec.
 
Copy codeThe code is as follows:
[Root @ localhost tmp] # find.-type f-user reed-exec chown cathy {}\;
# Change the file whose owner is reed to cathy

{} Is a special string. for each matching file, {} is replaced with the corresponding file name.

Copy codeThe code is as follows: [root @ localhost test] # find.-type f-mtime + 10-name "*. log"-exec cp {}/data/bk_log \;
# Copy the log files whose current directory is greater than 10 days to the/data/bk_log Directory
[Root @ localhost test] # find/tmp/test/-type f-name "*. txt"-exec printf "Text file: % s \ n "{}\;
Text file:/tmp/test/File_6_.txt
Text file:/tmp/test/file_4_.txt
Text file:/tmp/test/data_3_.txt
Text file:/tmp/test/data_cmd.txt
# List all txt files in a directory

10. skip the specified directory

When there is time, we need to skip some subdirectories when searching.

Copy codeThe code is as follows: [root @ localhost test] # find. \ (-name "jump_dir"-prune \)-o \ (-type f-print \)
# \ (-Name "jump_dir"-prune \) specifies the name of the subdirectory to be skipped

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.