"Go" Linux under find Find command usage

Source: Internet
Author: User
Tags print print






Original link http://www.jb51.net/os/RedHat/1307.html


The find command under Linux searches for files in the directory structure and performs the specified operation. The find command under Linux provides quite a few search conditions and is very powerful. Because find has powerful functions, it has many options, and most of them are worth our time to understand. Even if the system contains a network file system (NFS), the find command works equally well on that file system, only you have the appropriate permissions. When running a very resource-consuming find command, many people tend to execute it in the background, because it may take a long time to traverse a large file system (here refers to a file system above 30G bytes).

1. Command format:

find pathname -options [-print -exec -ok ...]

2. Command function:

Used to find files in the file tree and process them accordingly

3. Command parameters:

pathname: The path to the directory found by the find command. For example, use. To represent the current directory and / to represent the system root directory.
-print: The find command prints the matched files to standard output.
-exec: The find command executes the shell command given by this parameter on the matching files. The corresponding command is of the form ‘command’ {} \ ;, note the space between {} and \ ;.
-ok: Same as -exec, except that the shell command given by this parameter is executed in a more secure mode. Before each command is executed, a prompt will be given to the user to determine whether to execute it.

4. Command options:

-name Find files by file name.
-perm Find files by file permissions.
-prune Use this option to prevent the find command from searching in the currently specified directory. If the -depth option is also used, -prune will be ignored by the find command.
-user Find files by file owner.
-group Find files by the group they belong to.
-mtime -n + n Find files based on the file change time, -n means that the file change time is within n days, and + n means the file change time is n days ago. The find command also has -atime and -ctime options, but they are both with the -m time option.
-nogroup finds files without a valid group, that is, the group to which the file belongs does not exist in / etc / groups.
-nouser finds files without a valid owner, that is, the owner of the file does not exist in / etc / passwd.
-newer file1! file2 finds files that are newer than file1 but older than file2.
-type find files of a certain type, such as:
b-the block device file.
d-the directory.
c-the character device file.
p-the pipeline file.
l-symbolic link file.
f-ordinary file.
-size n: [c] Find files with file length n blocks. With c, the file length is in bytes. -depth: When looking for a file, first look for the file in the current directory, then look in its subdirectories.
-fstype: find files located in a certain type of file system. These file system types can usually be found in the configuration file / etc / fstab, which contains information about the file system in this system.
-mount: Do not cross the file system mount point when finding files.
-follow: If the find command encounters a symbolic link file, it follows the file pointed to by the link.
-cpio: Use the cpio command on the matching files to back them up to the tape device.

In addition, the following three differences:

-amin n find files accessed in the system for the last N minutes
-atime n find the last n * 24 hours of files in the system
-cmin n find files in the system whose status was changed in the last N minutes
-ctime n find files in the system that have been changed for the last n * 24 hours
-mmin n find files in the system whose file data was changed in the last N minutes
-mtime n find files in the system that have been changed for the last n * 24 hours

5. Use case:

Example 1: Find files that have been modified within a specified time

command:
           find -atime -2

Output:



Copy the code code as follows:

[[email protected] ~] # find -atime -2
.
./logs/monitor
./.bashrc
./.bash_profile
./.bash_history
Explanation:

Find files modified within 48 hours

Example 2: Find by keyword

command:

find. -name "* .log"

Output:



Copy the code code as follows:

[[email protected] test] # find. -name "* .log"
./log_link.log
./log2014.log
./test4/log3-2.log
./test4/log3-3.log
./test4/log3-1.log
./log2013.log
./log2012.log
./log.log
./test5/log5-2.log
./test5/log5-3.log
./test5/log.log
./test5/log5-1.log
./test5/test3/log3-2.log
./test5/test3/log3-3.log
./test5/test3/log3-1.log
./test3/log3-2.log
./test3/log3-3.log
./test3/log3-1.log
Explanation:

Find files ending in .log in the current directory. "." Represents the current directory

Example 3: Find files by directory or file permissions

command:

find / opt / soft / test / -perm 777

Output:



Copy the code code as follows:

[[email protected] test] # find / opt / soft / test / -perm 777
/opt/soft/test/log_link.log
/ opt / soft / test / test4
/ opt / soft / test / test5 / test3
/ opt / soft / test / test3
Explanation:

Find files with permissions of 777 in the / opt / soft / test / directory

Example 4: Find by type

command:

find. -type f -name "* .log"

Output:



Copy the code code as follows:

[[email protected] test] # find. -type f -name "* .log"
./log2014.log
./test4/log3-2.log
./test4/log3-3.log
./test4/log3-1.log
./log2013.log
./log2012.log
./log.log
./test5/log5-2.log
./test5/log5-3.log
./test5/log.log
./test5/log5-1.log
./test5/test3/log3-2.log
./test5/test3/log3-3.log
./test5/test3/log3-1.log
./test3/log3-2.log
./test3/log3-3.log
./test3/log3-1.log
[[email protected] test] #
Explanation:

Find ordinary files ending with .log as directories

Example 5: Find all current directories and sort

command:

find. -type d | sort

Output:



Copy the code code as follows:

[[email protected] test] # find. -type d | sort
.
./scf
./scf/bin
./scf/doc
./scf/lib
./scf/service
./scf/service/deploy
./scf/service/deploy/info
./scf/service/deploy/product
./test3
./test4
./test5
./test5/test3
[[email protected] test] #
Example 6: Find files by size

command:

find .-size + 1000c -print

Output:



Copy the code code as follows:

[[email protected] test] # find. -size + 1000c -print
.
./test4
./scf
./scf/lib
./scf/service
./scf/service/deploy
./scf/service/deploy/product
./scf/service/deploy/info
./scf/doc
./scf/bin
./log2012.log
./test5
./test5/test3
./test3
[[email protected] test] #
Explanation:

Find files larger than 1K in the current directory

First, find common usage examples in Linux

· Find path -option [-print] [-exec -ok command] {} \;
# -print print the found files to standard output
# -exec command () \; ----- execute command operation on the found files, there is a space between () and \;
# -ok is the same as -exec, except that the user must be consulted before the operation ==================================== =================== -name filename #Find a file named filename
-perm #Find by execute permission
-user username #Find by file owner
-group groupname #Find by group
-mtime -n + n #Find files by file change time, -n means within n days, + n means n days ago
-atime -n + n #Check by file access time GIN: 0px ">-perm #Find by execute permission
-user username #Find by file owner
-group groupname #Find by group
-mtime -n + n #Find files by file change time, -n means within n days, + n means n days ago
-atime -n + n #Find files by file access time, -n means within n days, + n means n days ago
-ctime -n + n #Find files by file creation time, -n means within n days, + n means n days ago
-nogroup #Check for files that do not have a valid group, that is, the file's group does not exist in / etc / groups
-nouser #Check for files with no valid owner, that is, the owner of the file does not exist in / etc / passwd
-newer f1! f2 find files, -n means n days or less, + n means n days ago
-ctime -n + n #Find files by file creation time, -n means within n days, + n means n days ago
-nogroup #Check for files that do not have a valid group, that is, the file's group does not exist in / etc / groups
-nouser #Check for files with no valid owner, that is, the owner of the file does not exist in / etc / passwd
-newer f1! f2 #Check files whose change time is newer than f1 but older than f2
-type b / d / c / p / l / f #Check block devices, directories, character devices, pipes, symbolic links, ordinary files
-size n [c] #Check files of length n blocks [or n bytes]
-depth #Make the search complete the directory before entering the subdirectory
-fstype #Check files whose change time is newer than f1 but older than f2
-mount #Do not cross file system mount points when checking files
-follow #If a symbolic link file is encountered, follow the file pointed to by the link
-cpio #Use the cpio command on the matching files and back them up to the tape device
-prune #Ignore a directory ================================================= ==========
$ find ~ -name "* .txt" -print #Check the .txt file in $ HOME and display
$ find. -name "* .txt" -print
$ find. -name "[A-Z] *" -pri26nbsp; #Use the cpio command for matching files and back them up to the tape device
-prune #Ignore a directory $ find. -name "[A-Z] *" -print #Check files that start with a capital letter
$ find / etc -name "host *" -print #Check files starting with host
$ find. -name "[a-z] [a-z] [0--9] [0--9] .txt" -print #Check the txt file that starts with two lowercase letters and two numbers
$ find. -perm 755 -print
$ find. -perm -007 -exec ls -l {} \; #Check all files that can be read, written and executed by all users. -perm 777
$ find .-type d -print print directory structure
$ find.! -type d -print Print non-directory files find / usr / include -name ‘* .h’ -exec grep AF_INEF6 {} \; Because grep cannot search subdirectories recursively, it can be used in combination with find. Find the string AF_INEF6 in .h files in all subdirectories of / usr / include
$ find. -type l -print $ find. -size + 1000000c -print #Check files longer than 1Mb
$ find. -size 100c -print # Search for files with a length of 100c
$ find. -size +10 -print #Check files that are overdue for 10 blocks (1 block = 512 bytes) $ cd /
$ find etc home apps -depth -print | cpio -ivcdC65536 -o / dev / rmt0
$ find / etc -name "passwd *" -exec grep "cnscn" {} \; #See if there is a cnscn user
$ find. -name "yao *" | xargs file
$ find. -name "yao *" | xargs echo ""> /tmp/core.log
$ find. -name "yao *" | xargs chmod ow ========================================= ================== find -name april * Find files starting with april in the current directory
find -name april * fprint file finds files starting with april in the current directory and outputs the results to file
find -name ap * -o -name may * find files starting with ap or may
find / mnt -name tom.txt -ftype vfat Find a file named tom.txt and file system type vfat under / mnt
find / mnt -name t.txt! -ftype vfat Find files under / mnt whose name is tom.txt and the file system type is not vfat
find / tmp -name wa * -type l Find files under / tmp that start with wa and are of type symbolic link
find / home -mtime -2 Find files changed in the last two days under / home
find / home -atime -1 find files accessed within 1 day
find / home -mmin +60 Find files that were changed 60 minutes ago under / home
find / home -amin +30 find files accessed 30 minutes ago
find / home -newer tmp.txt Find files or directories with a newer update time than tmp.txt under / home
find / home -anewer tmp.txt Under / home, search for files or directories whose access time is shorter than tmp.txt
find / home -used -2 List files or directories that have been accessed within 2 days after the files or directories have been changed
find / home -user cnscn List the files or directories belonging to user cnscn in the / home directory
find / home -uid +501 List files or directories with user ID greater than 501 in the / home directory
find / home -group cnscn List files or directories in / home as cnscn
find / home -gid 501 list files or directories with group id 501 in / home
find / home -nouser list files or directories in / home that do not belong to local users
find / home -nogroup list files or directories in / home that do not belong to a local group
find / home -name tmp.txt -maxdepth 4 List tmp.txt in / home. The depth of time is up to 3 layers.
find / home -name tmp.txt -mindepth 3
find / home -empty find files or empty directories of size 0
find / home -size + 512k find files larger than 512k
find / home -size -512k find files smaller than 512k
find / home -links +2 find files or directories with hard links greater than 2
find / home -perm 0700 find files or directories with permissions 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 in the system
find / -atime -2 # find files accessed in the last 48 hours in the system
find / -empty # find files or folders that are empty in the system
find / -group cat # find files belonging to groupcat in the system
find / -mmin -5 # Find files modified in the last 5 minutes of the system
find / -mtime -1 #Find files modified in the last 24 hours in the system
find / -nouser #Find files belonging to obsolete users in the system
find / -user fred #Find files belonging to the user FRED in the system

Check all ordinary 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 180 2003-02-25 ./conf.d/README
Check all ordinary files in the current directory and list them with the ls -l command in the -e x e c option
=====================================================
Find files in the / l o g s directory that were changed before 5 days and delete them:
$ find logs -type f -mtime +5 -exec -ok rm {} \;
=====================================================
Query files modified on that day
[[email protected] class] # find ./ -mtime -1 -type f -exec ls -l {} \;
=====================================================
Query file and ask if you want to display
[[email protected] class] # find ./ -mtime -1 -type f -ok ls -l {} \;
<ls ... ./classDB.inc.php>? y
-rw-r--r-- 1 cnscn cnscn13709 Jan 12 12:22 ./classDB.inc.php
[[email protected] class] # find ./ -mtime -1 -type f -ok ls -l {} \;
<ls ... ./classDB.inc.php>? n
[[email protected] class] # =============================================== =======
Query and hand it over to awk
[[email protected] class] # who | awk ‘{print $ 1" \ t "$ 2}’
cnscn pts / 0 ==================================================== ===
awk --- grep --- sed [[email protected] class] # df -k | awk ‘{print $ 1}’ | grep -v ‘none’ | sed s "/ \ / dev \ ////"
File system
sda2
sda1
[[email protected] class] # df -k | awk ‘{print $ 1}’ | grep -v ‘none’
File system
/ dev / sda2
/ dev / sda1


1) Find all * .h in / tmp, and "SYSCALL_VECTOR" in these files, and finally print out all the 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 find files larger than 3M in the disk:
find .-size + 3000k -exec ls -ld {};
4) Copy the found things to another place
find * .c -exec cp ‘{}’ / tmp ‘;’ If there are special files, you can use cpio, or use this syntax:
find dir -name filename -print | cpio -pdv newdir
6) Find 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
Second, the use of the find command under Linux

1. Basic usage:
      find / -name file name find ver1.d ver2.d -name '* .c' -print Find ver1.d, ver2.d * .c files and print find. -type d -print Find from current directory, find only The directory, when found, prints the path name. Can be used to print the directory structure.
2. Error-free search:
      find / -name access_log 2> / dev / null
3. Find by size:
      find / -size 1500c (find a 1,500-byte file, where c is the byte)
      find / -size + 1500c (Find files larger than 1,500 bytes in size, + means greater than)
      find / -size + 1500c (find files less than 1,500 bytes in size,-means less than)
4. By time:
      find / -amin n
      find / -atime n
      find / -cmin n change state in the last n minutes
      find / -ctime n change state in the last n days
5. Other:
      find / -empty blank files, blank folders, folders without subdirectories
      find / -false find files that are always wrong in the system
      find / -fstype type find files in the specified file system, such as type ext2
      find / -gid n group of files with id n
      find / -group gname group file named gname
      find / -depth n preferentially find file contents in a specified directory on a layer
      find / -maxdepth levels
6. Logic
      -and condition and -or condition or
7. Finding Strings
      find. -name ‘* .html’ -exec grep ‘mailto:’ {}  





"Go" Linux under find Find command usage


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.