Common Use examples of find

Source: Internet
Author: User

Common usage examples of find in Linux
· Find path-option [-print] [-exec-OK command] {}\;
#-Print: output the searched files to the standard output
#-Exec command {}\; ----- execute the command operation on the retrieved file. There is a space between {} And \;
#-OK is the same as-exec, but you need to consult the user before the operation

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

-Name filename # search for a file named filename
-Perm # search by execution permission
-User username # search by file owner
-Group groupname # search by group
-Mtime-n + n # Find the file based on the file change time.-n indicates that the file is less than n days, and + n indicates that the file is earlier than n days.
-Atime-n + n # query GIN by file access time: 0px ">-perm # query by execution permission
-User username # search by file owner
-Group groupname # search by group
-Mtime-n + n # Find the file based on the file change time.-n indicates that the file is less than n days, and + n indicates that the file is earlier than n days.
-Atime-n + n # search for files based on the file access time.-n indicates that the file is less than n days, and + n indicates that the file is earlier than n days.
-Ctime-n + n # Find the file based on the file creation time.-n indicates that the file is less than n days, and + n indicates that the file is earlier than n days.
-Nogroup # Check for files with no valid group, that is, the file group does not exist in/etc/groups.
-Nouser # Check files with no valid owner, that is, the file owner is not stored in/etc/passwd.
-Newer f1! F2 file search,-n refers to n days or less, + n refers to n days ago
-Ctime-n + n # Find the file based on the file creation time.-n indicates that the file is less than n days, and + n indicates that the file is earlier than n days.
-Nogroup # Check for files with no valid group, that is, the file group does not exist in/etc/groups.
-Nouser # Check files with no valid owner, that is, the file owner is not stored in/etc/passwd.
-Newer f1! F2 # Query files whose modification time is earlier than f1 but earlier than f2
-Type B/d/c/p/l/f # Check Block devices, directories, character devices, pipelines, symbolic links, and common files.
-Size n [c] # query objects with a length of n [or n Bytes]
-Depth # search for the local directory before entering the subdirectory
-Fstype # Query files whose change time is newer than f1 but earlier than f2
-Type B/d/c/p/l/f # Check Block devices, directories, character devices, pipelines, symbolic links, and common files.
-Size n [c] # query objects with a length of n [or n Bytes]
-Depth # search for the local directory before entering the subdirectory
-Fstype # Query files in a certain type of file system. These file system types can be found in/etc/fstab.
-Mount # The mount point of the file system is not crossed during file query.
-Follow # If a symbolic link file is encountered, the file indicated by the link will be tracked.
-Cpio %; # Query files in a certain type of file system. These file system types can be found in/etc/fstab.
-Mount # The mount point of the file system is not crossed during file query.
-Follow # If a symbolic link file is encountered, the file indicated by the link will be tracked.
-Cpio # Use the cpio command to back up the matching files to the tape device.
-Prune # ignore a directory

========================================================== ================
$ Find ~ -Name "*. txt"-print example: Check the. txt file and display it in home ‑.
$ Find.-name "*. txt"-print
$ Find.-name "[A-Z] *"-pri26nbsp; # Use the cpio command for matching files to back them up to tape Devices
-Prune # ignore a directory

========================================================== ==================
$ Find ~ -Name "*. txt"-print example: Check the. txt file and display it in home ‑.
$ Find.-name "*. txt"-print
$ Find.-name "[A-Z] *"-print # Query files starting with an uppercase letter
$ Find/etc-name "host *"-print # Query files starting with host
$ Find.-name "commana-z00000000a-z0000000000000--9000000000--90000.txt"-print # query txt files starting with two lower-case letters and two numbers
$ Find.-perm 755-print
$ Find.-perm-007-exec ls-l {}\; # check whether all files that can be read and written by users are the same as-perm 777
$ Find.-type d-print
$ Find .! -Type d-print
$ Find.-type l-print

$ Find.-size + 000000c-print # Query files with a length greater than 1 MB
$ Find.-size 100c-print # Query files with a length of C
$ Find.-size + 10-print # Query 10 files whose length exceeds the expiration date (1 file = 512 bytes)

$ Cd/
$ Find etc home apps-depth-print | cpio-ivcsc65536-o/dev/rmt0
$ Find/etc-name "passwd *"-exec grep "cnscn" {}\; # check whether a cnscn user exists
$ Find.-name "yao *" | xargs file
$ Find.-name "yao *" | xargs echo "">/tmp/core. log
$ Find.-name "yao *" | xargs chmod o-w

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

Find-name CMDL * search for files starting with CMDL in the current directory
Find-name extension L * fprint file searches for files starting with extension l in the current directory, and outputs the results to the file.
Find-name ap *-o-name may * search for files starting with ap or may
Find/mnt-name tom.txt-ftype vfat find the file named tom.txt and the file system type is vfat under/mnt
Find/mnt-name t.txt! -Ftype vfat: Find the file named tom.txt under/mntand the file system type is not vfat.
Find/tmp-name wa *-type l search for files starting with wa and whose type is symbolic link under/tmp
Find/home-mtime-2 check files modified in the last two days under/home
Find/home-atime-1. Check the files that have been accessed within one day.
Find/home-mmin + 60 check files modified 60 minutes ago under/home
Find/home-amin + 30 check files that have been accessed in the last 30 minutes
Find/home-newer tmp.txt check for files or directories that are earlier than tmp.txt in/home.
Find/home-anewer tmp.txt check the files or directories with a time closer to tmp.txt under/home.
Find/home-used-2 list the files or directories that have been accessed within two days after the files or directories have been modified.
Find/home-user cnscn list the files or directories belonging to the user's cnscn in the/home Directory
Find/home-uid + 501 list files or directories with user IDs greater than 501 in the/home Directory
Find/home-group cnscn list/home files or directories in which the group is cnscn
Find/home-gid 501 list/home files or directories with group id 501
Find/home-nouser: list files or directories that do not belong to local users in/home.
Find/home-nogroup list files or directories in/home that do not belong to the Local Group
Find/home-name tmp.txt-maxdepth 4 list/The tmp.txt in homecan be found at a maximum of three layers.
Find/home-name tmp.txt-mindepth 3 query from Layer 1
Find/home-empty
Find/home-size + 512 k Query files larger than k
Find/home-size-512 k to query files smaller than k
Find/home-links + 2 check files or directories with hard connections greater than 2
Find/home-perm 0700 check the file or directory with the permission of 700
Find/tmp-name tmp.txt-exec cat {}\;
Find/tmp-name tmp.txt-OK rm {}\;

Find/-amin-10 # search for files accessed in the last 10 minutes in the system
Find/-atime-2 # search for files accessed in the last 48 hours in the system
Find/-empty # search for files or folders that are empty in the system
Find/-group cat # find the groupcat file in the system
Find/-mmin-5 # search for the files modified in the last 5 minutes in the system
Find/-mtime-1 # search for files modified in the last 24 hours in the system
Find/-nouser # search for files belonging to the void user in the system
Find/-user fred # search for files belonging to the user FRED IN THE SYSTEM

Query all common files in the current directory
--------------------------------------------------------------------------------

# Find.-type f-exec ls-l {}\;
-Rw-r -- 1 root 34928 2003-02-25./conf/httpd. conf
-Rw-r -- 1 root 12959 2003-02-25./conf/magic
-Rw-r -- 1 root 180 2003-02-25./conf. d/README
Query all common 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, find the files whose changes were earlier than 5 days and delete them:
$ Find logs-type f-mtime + 5-exec-OK rm {}\;


========================================================== ==========
Query the files modified on the current day
[Root @ book class] # find./-mtime-1-type f-exec ls-l {}\;


========================================================== ==========
Query the file and ask if you want to display it
[Root @ book class] # find./-mtime-1-type f-OK ls-l {}\;
<Ls.../classDB. inc. php>? Y
-Rw-r -- 1 cnscn 13709 January 12 12:22./classDB. inc. php
[Root @ book class] # find./-mtime-1-type f-OK ls-l {}\;
<Ls.../classDB. inc. php>? N
[Root @ book class] #

========================================================== ==========
Query and submit it to awk for processing
[Root @ book class] # who | awk '{print $1 "\ t" $2 }'
Cnscn pts/0

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

[Root @ book class] # df-k | awk '{print $1}' | grep-v 'none' | sed s "// dev \ // g"
File System
Sda2
Sda1
[Root @ book class] # df-k | awk '{print $1}' | grep-v 'none'
File System
/Dev/sda2
/Dev/sda1


1) Search for all *. h In/tmp, search for "SYSCALL_VECTOR" in these files, and print out all file names containing "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 search for files larger than 3 m on a disk:
Find.-size + 3000 k-exec ls-ld {};


4) copy the find to another place.
Find *. c-exec cp '{}'/tmp ';'

If a special file exists, you can use cpio or the following syntax:
Find dir-name filename-print | cpio-pdv newdir


6) Search for the files changed at 16:36:37,-11-30.
# A = 'Find./-name "* php" '| ls-l -- full-time $ A 2>/dev/null | grep "2004-11-30 16:36:37"

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.