File Lookup command in Linux environment--find

Source: Internet
Author: User

Familiar with the Linux partners know that in the open-source Linux environment, the main idea is that everything is a file, anything in Linux can be seen as files, then we use these files when we face a problem how to find a file, Here I take CentOS6.5 virtual machine as an example, do a demonstration.

There are several search file commands under Linux: Find, Whereis, which, and locate.

which: View the location of the executable file

whereis: Viewing the location of a file

Locate: Viewing files with a database

find: Actual search drive Query

Where find is the most commonly used and most powerful find command. It can be found in real-time, accurate search, but slow. The following is a detailed description of the find instruction related usage.

First open the virtual machine and enter the terminal


Find command format: "Format" Find [specify Find directory] [find rule] [action executed after find]

The default path is: current directory

default action to be performed when the search is complete as: -print.

The actions that are performed after the search have been completed are:

-print//actions by default, output of found files to standard output

-ls//Find and then use LS to display it

-exec [commend]       //find command executes the shell command given by this parameter to the matching file. The corresponding command is in the form of ' command ' {} \;, note the space between {} and \; search after executing the command without asking the user, directly execute

-OK [commend]// and-exec are the same, except to ask the user if they want to perform the operation before

Usage Example: Find-name filename-ls



Find main Usage Description:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-name filename//Find file named filename-perm//By Execute permission to find- User username//by file owner to find-group groupname//by group to find-mtime-n +n//By file change time to find files,- n refers to n days, +n refers to n days ago-atime-n +n//By file access time to check gin:0px ">-ctime-n +n//By file creation time to find files,-n means less than n days, +n refers to n days ago-nogroup//Check no effective group of documents, that is, the group of documents in the/etc/groups does not exist-nouser//search for the document is not a valid master, that is, the owner of the document in the/ETC/PA SSWD-newer F1!f2//Find files,-n refers to the nth days, +n means n days ago-ctime-n +n//By file creation time to find files,-n refers to n days, +n refers to n days ago-no Group//Do not have a valid group of documents, that is, the group of documents in the/etc/groups does not exist-nouser//Check the document is not a valid owner, that is, the owner of the document in the/ETC/PASSWD               -newer F1!F2//Check change time than F1 new but F2 old file-type b/d/c/p/l/f//check is block device, directory, character device, pipe, symbolic link, ordinary file-size N[c] Check for files of length n [or n bytes]-depTh//Make the search complete the directory before entering the subdirectory-fstype//Check change time than F1 new but older than F2 file-type b/d/c/p/l/f Check is a block device, directory, character device, pipe, symbolic link, ordinary file-size N[c]//Check the length of n block [or N bytes] of the file-depth//make the lookup before entering the subdirectory to find out the Directory-fstype//Check files located in a file system of a certain type, these file system types are usually found in the/etc/fstab-mount//Check files without spanning the file system mount      Point-follow//If you encounter a symbolic link file, follow the link to refer to the file-cpio%; Files located in a file system of a certain type, these file system types can usually be found in/etc/fstab-mount//Check files without crossing the file system mount point-follow/ /If you encounter a symbolic link file, follow the link to the file-cpio//to the matching files using the Cpio command, back them up to the tape device-prune//Ignore a directory
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~
$find   ~   -name   "*.txt"   -print      //Check the. txt file in $home and show $find   .    -name   "*.txt"   -print$find   .    -name   "[a-z]*"   -print   //Check for files beginning with capital letters   $find   /etc-name   "host*"   -print       // Check the file $find that begins with host   .   -name   "[a-z][a-z][0–9][0–9].txt"    -print   //Check for TXT file with two lowercase letters and two digits $find.   -perm   755   -print$find   .   -perm-007   -exec ls-l {} \ <span style= "font-family:arial, Helvetica, Sans-serif;" >  //</span><span style= "font-family:arial, Helvetica, Sans-serif;" > All users can read and write executable files with-perm 777</span>
$find   .-type d   -print$find   .   !   -type   d   -print $find   .   -type l   -print$find   .   -size   +1000000c   -print       //Check file length is greater than 1Mb $find   .   -size   100c         -print       //Check file $find for length 100c   .   -size   +10   -print//             Check the length of ultra-expired 10 blocks of files (1 = 512 bytes) $CD//                                       /to the root directory $find   etc   Home   apps    -depth   -print   | cpio   -ivcdc65536-   o   /dev/rmt0$find   /etc-name "passwd*"   -exec grep   "Cnscn"   {}   \;     See if there are CNSCN user $find. -name "yao*"   | xargs file$find   .-name "yao*"   |   Xargs   echo    "" >/tmp/core.log$find   -name "yao*"   | xargs   chmod   o-w
</pre><pre name= "code" class= "CPP" ><span style= "Color:rgb (57, 57, 57); Font-family:verdana, Arial, Helvetica, Sans-serif; font-size:14px; line-height:21px; Background-color:rgb (250, 247, 239); " > Check all common Files in current directory </span>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~find-name april*//Find files starting with April in the current directory find-name april* fprint file//In   Find files starting with April in the current directory and output the results to file Find-name ap*-o-name may* Find files that start with an AP or May find/mnt-name Tom.txt-ftype VFAT Under/mnt, look for files with name Tom.txt and file system type VFAT find/mnt-name t.txt! -ftype vfat//Find files with the name Tom.txt and file system type not VFAT under/mnt find/tmp-name wa*-type L//Find a file with a type of symbolic link at/tmp that starts with the filename fi Nd/home-mtime-2//In the last two days after the changes in the file find/home-atime-1//Check the files accessed within 1 days find /home-mmin +60//In/home 60 Minutes ago Changed files find/home-amin +30//Check the files that were accessed in the last 30 minutes find/ Home-newer Tmp.txt//At home/home update time than tmp.txt near the file or directory Find/home-anewer Tmp.txt//In/home check access time than   Tmp.txt files or directories find/home-used-2//list files or directories that have been changed since the file or directory was accessed within 2nd find/home-user CNSCN//lists the files or directories in the/home directory that belong to the user Cnscn Find/home-uid +501//Lists the users in the/home directory  A file or directory with a size greater than 501 find/home-group CNSCN//lists the files or directories in the/home group as CNSCN Find/home-gid 501//                   Lists files or directories with group ID 501 in/home Find/home-nouser//lists files or directories within/home that are not local users Find/home-nogroup Lists files or directories in/home that do not belong to a local group Find/home-name tmp.txt-maxdepth 4//Lists Tmp.txt in/home with a depth of up to 3 layers Find/home- Name Tmp.txt-mindepth 3//start with the 2nd level Find/home-empty//Find file size 0 or empty directory find/home-size +5               12k//Check for files larger than 512k find/home-size-512k//Check files smaller than 512k find/home-links +2  Check the number of files or directories with a hard connection greater than 2 Find/home-perm 0700//Check the file or directory with permission 700 Find/tmp-name tmp.txt-exec Cat {}        \;find/tmp-name tmp.txt-ok RM {} \;find/-amin-10//Find files accessed in the last 10 minutes of the system find/-atime-2 Find the last 48 hours of access in the systemFile Find/-empty//Find files or folders that are empty in the system Find/-group CAT//Find files belonging to Groupcat in the system find/-mmi N-5//Find files that have been modified in the last 5 minutes of the system Find/-mtime-1//Find files that have been modified in the last 24 hours of the system find/-nouser//Find in the Department Files belonging to obsolete users find/-user Fred//Find files belonging to Fred This user in the system
<span style= "font-family:arial, Helvetica, Sans-serif;" >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~</span>

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/L o G S 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

[[email protected] zming]# find   ./   -mtime   -1   -type f   -exec ls-l   {} \;


Query the file and ask if you want to display


Query and give it to awk to handle it.

$DF-   k |   awk ' {print '} ' |   grep-   v   ' None ' |   Sed   S "/\/dev\///g"
Other usage

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_vectorb) grep   syscall_vector   /tmp/*.h | cut-    d ': '   -f1| uniq > Filenamec) Find   /tmp-name "*.h"   -exec grep "Syscall_vector"   {}   \;-print2) Find/-name Filename-exec RM-RF {} \;    Find/-name filename-ok rm-rf {} \;3) For example to find files larger than 3M on disk: Find. -size +3000k-exec ls-ld {}; 4) Copy something from find to another place find *.c-exec cp ' {} '/tmp '; ' If you have special files, you can use Cpio, or you can use this syntax: Find Dir-name filename-print | CPIO-PDV newdir6) Look for files changed at 2016-5-26 16:30:30 a= ' find./-name ' *php ' |   Ls-l–full-time $A 2>/dev/null | grep "2016-5-26 16:30:30"

In addition, at the end of this article suggest that if you are not very familiar with Linux, do not easily use root (super User) permissions to work, although the root is very convenient, powerful, but this is a double-edged sword, for the novice, more harm than good.

Enter the root command as: su


Exit root command: Exit



about find usage is many, want to fully memorize is not easy, so only in use, it is easier to remember, so to use more, you are welcome to criticize!





















File Lookup command in Linux environment--find

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.