Shell Programming Basics-find Command

Source: Internet
Author: User

Examples of find common usages in Linux
Find Path-option [-print] [-exec-ok command] {} \;

The parameters of the Find command;
The directory path that the Path:find command looks for. For example, use. To represent the current directory, and/to represent the system root directory.
The-print:find command outputs the matched file to standard output.
The-exec:find command executes the shell command given by the parameter to the matching file. The corresponding command is in the form of ' command ' {} \;, note the space between {} and \;
-ok: The same as-exec, except that the shell command given by the parameter is executed in a more secure mode, prompting the user to determine whether to execute before executing each command.
#-print output of the found file to standard output
#-exec command {} \; -the command operation for the file to be found, with spaces between {} and \;
#-ok and-exec are the same, except to consult the user before operation
Example: Find. -name. SVN | Xargs RM-RF

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

-name filename #查找名为filename的文件
-perm #按执行权限来查找, read and write executable
-user username #按文件属主来查找
-group GroupName #按组来查找
-mtime-n +n #按文件更改 (Modify) time to find files, (detailed below)
-atime-n +n #按文件访问 (Access) time to check
-ctime-n +n #按文件创建 (Create) time to find files
-nogroup #查无有效属组的文件 that the genus of the file does not exist in the/etc/groups
-nouser #查无有效属主的文件, that is, the owner of the file does not exist in the/etc/passwd
-ctime-n +n #按文件创建时间来查找文件
-newer F1!F2 #查更改时间比f1新但比f2旧的文件
-type b/d/c/p/l/f #查是块设备, directories, character devices, pipelines, symbolic links, plain files
-size N[c] #查长度为n块 [or N-byte] files
-depth # before working on a directory, first process the child content under the directory
-fstype #find files located in a type of file system that are typically found in the configuration file/etc/fstab, which contains information about the file system in the system.
-mount #查文件时不跨越文件系统mount点
-follow #如果遇到符号链接文件, just follow the file that the link refers to
-cpio # Copy files to the archive package
-prune #忽略某个目录

=====================================================
$find ~-name "*.txt"-print #在 $HOME. txt file and displays
$find. -name "*.txt"-print
$find. -name "[a-z]*"-print #查以大写字母开头的文件
$find/etc-name "host*"-print #查以host开头的文件
$find. -name "[A-z][a-z][0–9][0–9].txt"-print #查以两个小写字母和两个数字开头的txt文件


$find. -perm 755-print
$find.           -perm-007-exec ls-l {} \; #查所有用户都可读写执行的文件同-perm 777


$find. -type D-print #查找类型为目录的文件
$find.   ! -type D-print
$find. -type L-print

$find. -size +1000000c-print #查长度大于1Mb的文件
$find. -size 100c-print # Check for files of length 100c
$find. -size +10-print #查长度超过10块的文件 (1 block = 512 bytes)


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

Find-name april* Find files starting with April in the current directory
Find-name april* >>file finds files starting with April in the current directory and outputs the results to file
Find-name ap*-o-name may* Find files that start with an AP or may (-O or OR)
Find/mnt-name tom.txt-ftype vfat to find files with the name Tom.txt and file system type VFAT under/MNT
Find/mnt-name T.txt! -ftype VFAT under/mnt to find files with the name Tom.txt and file system type not VFAT
Find/tmp-name wa*-type L Find a file with a type of symbolic link at/tmp that begins with the name WA
Find/home-mtime-2 in/home search for files that have changed in the last two days
find/home-atime-1 checked files accessed within 1 days
Find/home-mmin +60 The files that were changed 60 minutes ago at/home
Find/home-amin +30 Check the files that were accessed last 30 minutes ago
Find/home-newer Tmp.txt in/home check update time than tmp.txt files or directories
Find/home-anewer tmp.txt a file or directory that is accessed more than tmp.txt near/home
Find/home-used-2 lists files or directories that have been accessed within 2nd after the file or directory has been altered
Find/home-user CNSCN lists files or directories in the/home directory that belong to the user Cnscn
Find/home-uid +501 lists files or directories with a user's ID greater than 501 in the/home directory
Find/home-group CNSCN list files or directories in/home with group CNSCN
Find/home-gid 501 lists files or directories with a group ID of 501 in/home
Find/home-nouser lists files or directories in/home that are not local users
Find/home-nogroup lists files or directories in/home that are not part of a local group
Find/home-name tmp.txt-maxdepth 4 Lists the Tmp.txt in/home with a depth of up to 3 layers
Find/home-name tmp.txt-mindepth 3, starting from the 2nd floor.
Find/home-empty finding a file size of 0 or an empty directory
Find/home-size +512k files larger than 512k
find/home-size-512k files less than 512k
Find/home-links +2 Check hard connections more than 2 files or directories
Find/home-perm 700 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 # finds files accessed in the last 10 minutes of the system
Find/-atime-2 # finds files accessed in the last 48 hours of the system
Find/-empty # finds files or folders that are empty in the system
Find/-group Cat # finds files that belong to Groupcat in the system
Find/-mmin-5 # finds files that have been modified in the last 5 minutes of the system
Find/-mtime-1 #查找在系统中最后24小时里修改过的文件
Find/-nouser #查找在系统中属于作废用户的文件
Find/-user Fred #查找在系统中属于FRED这个用户的文件

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
Note: 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
Find./-mtime-1-type f-exec ls-l {} \;

=================================================
Query the file and ask if you want to display
Find./-mtime-1-type f-ok ls-l {} \;

=================================================
Query and give it to awk to handle it.
W.H.O. | awk ' {print ' \ t '

=================================================
File system disk Conditions
Df-k |   awk ' {print '} ' | Grep-v ' None '


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) Delete files by name

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 {};

4) Copy the find out 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 Newdir

5) Look for files changed at 2004-11-30 16:36:37
# a= ' Find/-name ' *php ' | Ls-l–full-time $A 2>/dev/null | grep "2004-11-30 16:36:37"

Shell Programming Basics-find Command

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.