Linux common commands: find command overview


  The find command in Linux searches for files in the directory structure and executes the specified operation. The find command under Linux provides quite a few search conditions and is very powerful. Since find is powerful, it has many options, most of which are worth taking the time to explore. Even if the system contains a Network File System (NFS), the find command is also valid on that file system, as long as you have the appropriate permissions. When running a very resource-intensive find command, many people tend to run it in the background, because traversing a large filesystem can take a long time (in this case, a filesystem with more than 30G bytes).

1. Command format:

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

2. Command function:

  Used to find files in the file tree species and make corresponding processing 

3. Command parameters:

pathname: The directory path that the find command looks for. For example, use . to represent the current directory and / to represent the system root directory. 

-print: The find command outputs matching files to standard output. 

-exec: The find command executes the shell command given by this parameter to the matching files. The corresponding command is of the form 'command' { } \;, note the space between { } and \;. 

-ok: The same as -exec, except that the shell command given by this parameter is executed in a safer mode. Before executing each command, a prompt will be given to let the user 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 make the find command not search in the currently specified directory. If the -depth option is used at the same time, -prune will be ignored by the find command.

-user Find files by file owner.

-group Find files by group they belong to.

-mtime -n +n Find the file according to the change time of the file, - n means the file change time is within n days from now, + n means the file change time is n days ago from now. The find command also has the -atime and -ctime options, but they are both the same as the -m time option.

-nogroup Find files with no valid group, that is, the group to which the file belongs does not exist in /etc/groups.

-nouser Find files with no valid owner, that is, the owner of the file does not exist in /etc/passwd.

-newer file1 ! file2 Find files whose change time is newer than file11 but older than file2.

-type Find files of a certain type, such as:

b - block device file.

d - directory.

c - character device file.

p - the pipe file.

l - Symbolically linked file.

f - normal file.

-size n: [c] Find files with a file length of n blocks, with c, it means the file length is in bytes. -depth: When looking for files, look for files in the current directory first, and then in their 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 filesystem mount points when looking for files.

-follow: If the find command encounters a symbolic link file, follow the file to which the link points.

-cpio: Use the cpio command on matching files to back them up to a tape device.

In addition, the following three differences:

-amin n Find files accessed in the last N minutes in the system

-atime n Find files accessed in the last n*24 hours in the system

-cmin n Find files in the system whose state has been changed in the last N minutes

-ctime n Find files in the system whose state has been changed in the last n*24 hours

-mmin n Find files in the system that have changed file data in the last N minutes

-mtime n Find files in the system with changed file data in the last n*24 hours

5. Example of use:

Example 1: Find files modified within a specified time 

Order:

           find -atime -2

output:

[root@peidachang ~]# find -atime -2

.

./logs/monitor

./.bashrc

./.bash_profile

./.bash_history

illustrate:

  Find files modified within 48 hours 

Example 2: Search by keyword 

Order:

find . -name "*.log"

output:

[root@localhost 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

 illustrate:

  Look for files ending in .log in the current directory. "." represents the current directory 

Example 3: Find files according to the permissions of a directory or file

Order:

find /opt/soft/test/ -perm 777

output:

[root@localhost 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

illustrate: 

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

Example 4: Find by Type 

Order:

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

output:

[root@localhost 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

[root@localhost test]#

illustrate:

  Find ordinary files ending with .log in the current directory 

Example 5: Find all current directories and sort

Order:

  find . -type d | sort

output:

[root@localhost 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

[root@localhost test]#

 

 

 

 

 

Example 6: Find files by size

Order:

find . -size +1000c -print

output:

[root@localhost 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

[root@localhost test]#

illustrate:

  Find files larger than 1K in the current directory

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325211251&siteId=291194637
Recommended