File lookup commands under Linux

Source: Internet
Author: User

File lookup: Search for files by filename or file property;

A) Locate

Fuzzy Lookup:
Based on a dedicated database to find, the database should be created in advance, and regularly updated;
You can manually update the locate database using the UpdateDB command;
Find very fast, find accurate to very limited;


=============================================================
II) find

Exact search:
The search precision is high, the speed is slightly slow;
A traversal scan of a filename or file property at the specified location; It is strongly not recommended to find the root directory;
Real-time search;
Use the Find command to search only the directory where the current user has read and execute permissions;

Find command:
Find-search for files in a directory hierarchy
Find [OPTIONS ...] [Find Path] [Search Criteria] [Handling Action]
Find path: Default to the current working directory, you can specify a specific directory path;
Search criteria: The criteria for the searches can be file name, file size, file type, file permissions, etc.; By default, all files in the specified directory;
Handling actions: Perform a processing operation on a file that matches the criteria, and output the lookup results to the display by default;

Search by file name:
-name file name, support for using globbing, (*,?, [], [^])
-iname file name, ignoring letter case, support using globbing, (*,?, [], [^])

Search by inode number of the file:
-inum inode Number: Find the corresponding file name and path by the given inode number;
-samefile name: Finds the corresponding inode number by the given file name, and then determines all filenames and paths with that inode number;
-links N: Finds all files with the number of links n;

Find by Regular expression:
-regex pattern: Matches the entire file path string with pattern, not just the name of the given file;

Search according to the file's owner and owner group:
-user uname: Search According to the user name of the owner for the specified user
-uid UID: Search by Owner for a UID
-group Gname:
-gid GID:

-nogroup: There is no group name on the genus Group of the file;
-nouser: The owner of the document has no user name;

Find by File type:
-type file Type: "
B: Block device
C: Character device
D: Catalog file
F: Normal file
L: Symbolic Link file
P: Pipeline File
S: Socket file
-xtype file type: The matching of the symbolic link file needs to be matched with other options;

To find based on a timestamp:
In days:
-atime [+|-]n: Find based on access time
-ctime [+|-]n: Search by change time
-mtime [+|-]n: Find based on modified time
N:[N,N+1)
+n:[n+1,+∞)
-n:[now,n)
In minutes:
-amin [+|-]n
-cmin [+|-]n
-mmin [+|-]n

Example:
5-28-11-18
-mtime-3
5-25-11-18
-mtime 3
5-24-11-18
-mtime +3

To find the file size:
-size [+|-]N[CWBKMG]
N: (N-1,n]
-N:[0,N-1]
+n: (n,+∞)

Example:
Find-size +2k
All files larger than 2KB in the current directory;
find-size 2k
Files between all 1kb-2kb in the current directory;
find-size-2k
All files less than 1KB in the current directory;

Combination conditions:
-A: Logical AND, default can be omitted;
-O: Logical OR
-not,!: Logical Non-

The logic combination condition follows the Demogan law:
Non-(A and b) = = = Non-A or non-B
Non-(A or b) = = = Non-A and non-B

Search by permissions:
-perm [/|-]mode
Mode: Exact Match of specified permissions
/mode: The implied logic or the relationship, any one permission bit of permission to have a permission to match, can satisfy the condition;
-mode: The implied logic and the relationship, each permission bit permission must contain the specified permission bit, in order to satisfy the condition;

All have to take back any one No
! (A and B and c) =!a or!b or!c
All are not taken against any one that has
! (!a and!b and!c) = A or B or C

Handling actions:
-print: Output to the display screen, the default action;
-ls: Performs Ls-li command display on the results that are found;
-exec COMMAND {} \;:
-ok COMMAND {} \;:
Execute command for the found results;
Difference:
-exec is non-interactive;
The-ok is interactive;
{}: Placeholder used to refer to the path information of all files found by the Find command;

-exec and-ok to perform operations:
chmod a-r $ (find-perm-444-type f)
Find-perm-444-type F | Xargs chmod a-r

Note: Pipelines convey pure string information, so if the command after the pipeline is not a command to process the string, you need to use the Xargs command to convert it to a parameter that can be processed by the following command;

=============================================================
Give some chestnuts:


Check all the normal files in the current directory
# Find. -type f-exec ls-l {} \;
-rw-r–r–1 root root 34928 2003-02-25./conf/httpd.conf
-rw-r–r–1 root root 12959 2003-02-25./conf/magic
-rw-r–r–1 root root 2003-02-25./conf.d/readme
Check all the normal files in the current directory and use the LS-L command in the-e x E C option to list them

=================================================
In the/Logs directory, look for files that change time before 5th and delete them:
$ find Logs-type f-mtime +5-exec-ok rm {} \;

=================================================
Query for files modified on the day
]# Find/-mtime-1-type f-exec ls-l {} \;


=================================================
Query and give it to awk to handle it.
]# who | awk ' {print ' \ t '
CNSCN pts/0

=================================================
Awk-grep-sed

]# Df-k |   awk ' {print '} ' |   Grep-v ' None ' | sed S "/\/dev\///g"
File system
Sda2
Sda1
]# Df-k |   awk ' {print '} ' | Grep-v ' None '
File system
/dev/sda2
/dev/sda1
1) Find all the *.h in/tmp and look for "syscall_vector" in these files, and finally print out all the filenames that contain "syscall_vector"

A) find/tmp-name "*.h" | Xargs-n50 grep syscall_vector
B) grep syscall_vector/tmp/*.h | Cut-d ': '-f1| Uniq > FileName
C) find/tmp-name "*.h"-exec grep "Syscall_vector" {} \; -print

2) Find/-name filename-exec rm-rf {} \;
Find/-name filename-ok rm-rf {} \;

3) For example, to find files larger than 3M on the disk:
Find. -size +3000k-exec ls-ld {};






This article from "Fuboyuan" blog, declined reprint!

File lookup commands under Linux

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.