Detailed Find command

Find instruction detailed explanation
20130116 Chenxin finishing
201809 update
Common examples

View the file name
find according to the file content . -Type f -exec grep -l 'xyz' {};
find. -Type fxargs grep -l 'xyz'

find statistical file size
find ./* -name "* .rdb.gz" -exec ls -l {}; | awk 'BEGIN {SUM7 = 0} {SUM7 + = $ 5} END {print SUM7}'

find ignores files in the specified format
find -type f! -name " .h" -exec rm -rf {};
find ./ -type f! -name "
.h" -exec rm -rf {}; #Delete. All other files except the h suffix

find ignores individual directories
If you want to ignore a certain directory when searching for files, because you know that there is no file you want to find in that directory, you can use the -prune option to indicate the directory to be ignored. Be careful when using the -prune option, because if you also use the -depth option, the -prune option will be ignored by the find command. If you want to find files in the / apps directory, but do not want to search in the / apps / bin directory, you can use:
$ find / apps -path "/ apps / bin" -prune -o -print (ignore a directory)

find finds files
with a specified size or time or by size:
find / -size 1500c (find files of 1,500 bytes, c means bytes)
find / -size + 1500c (find files larger than 1,500 bytes, + means greater than )
Find / -size -1500c (find files smaller than 1,500 bytes,-means less than)
by time:
find / -amin n last n minutes
find / -atime n last n days
find / -cmin n last n minutes change state
find / -ctime n Change the status in the last n days
View the data before 7 days:
/ usr / bin / find / bak / -mtime +7 -name " .gz" -exec ls -al {};
View the data of the last day :
/ Usr / bin / find / bak / -mtime -1 -name "
.gz" -exec ls -al {};

find reported that "the path must be before the expression". For
more than one match, dekstop generates multiple words, and only the first one will be used as the parameter of the -name option. The rest of the find was not recognized, so the error "The path must be before the expression". Therefore, if you want to use a regular expression in -name, you must escape it to prevent the shell from expanding it first. The way to escape is ' .desktop' or "* desktop", so the -name option always only accepts this parameter.
find ./ -name " .txt" -exec ls -al {};
regular match regex
find find multiple files at the same time -regex, and the corresponding operation (batch mv or ls)
will suffix .log and .out mv
Find ./ -maxdepth 1 -type f -regex '.
( .Log | .out)' -exec mv "{}" "restart-log / {}- date +%Y-%m%d-%H%M%S" in the restart-log folder ;

The following example pays attention to the use of the -o parameter, which includes -mtime before and after. If -exec is included, it is required before and after -o. Otherwise, it only matches the -o option with -exec added.
Find / data / servers_backup -regex '. (list.tmp | .gz)' -mtime +10
find / data / servers_backup -name "
list.tmp" -mtime +10 -o -name "* .gz" -mtime +10

Find files that contain htm and at least one character behind; and file names that contain iss characters and can contain multiple or 0 u characters.
Find ./ -regex '. (Htm. | Issu ). *' As follows
. /issue.net
./issue
./index.html

Execution mode 1 Exec description The
command execution process, first find the file, then pass the file to the "{}" braces, and then execute the rm command. {} Stands for the file (probably the file name).
-Exec must end with a;, and because the shell usually processes the;, use; to prevent this. {} May need to be written as '{}', also to avoid being filtered by the shell.
Note: {} must be followed by ";" and there must be a space between "{}" and ";"
find ./ -name " .tar.gz" -exec ls -al {}; | grep "dump "

Batch rename,
find ./ -maxdepth 1 -type f -name " .out" -exec mv "{}" "tmp / {}- date +%Y-%m%d-%H%M%S" in the
current directory ; find ./ -maxdepth 1 -type f -name " . log "-exec mv" {} "" tmp / {}- date +%Y-%m%d-%H%M%S"; two in
one (regular)
find ./ -maxdepth 1 -type f -regex '. (.log | .out)' -exec mv" { } "" restart-log / {}- date +%Y-%m%d-%H%M%S"; combine two
into one (use -o parameter)
find ./ -maxdepth 1 -type f -name"
.out "-exec mv" {} "" restart-log / { }- date +%Y-%m%d-%H%M%S"; -o -name" * .log "-exec mv" {} "" restart-log / {}- date +%Y-%m%d-%H%M%S";

Find matches and copy in batches:
find ./ -name " .sql.tar.gz" -mtime +30 -exec cp -aprf "{}" / data / old_data_2013;
remotely copy the matched files to the remote machine:
find. / -name "Network_Test_20120
.txt" -exec scp -P 4399 "{}" [email protected]: / home / admin /;

Execution method 2 xargs description
Refer to http://czmmiao.iteye.com/blog/1949225
Introduction The
reason why this command can be used is that many commands do not support the "|" pipeline to pass parameters, and this is included in daily work. Necessary, so there is the xargs command. For example:
this command is wrong find / sbin -perm +700 | ls -l
This is the correct find / sbin -perm +700 | xargs ls -l

When using the -exec option of the find command to process the matched files, the find command passes all the matched files to exec for execution. However, some systems have restrictions on the length of commands that can be passed to exec, so that the find command runs for a few minutes. After that, an overflow error will occur. The error message is usually "Parameter column too long" or "Parameter column overflow".

The xargs command only gets a part of the file instead of all at a time, unlike the -exec option. In some systems, in order to avoid the disadvantage of -exec, the parameters are divided into multiple processes for execution. With the xargs command, there is only one process.

find ./ -maxdepth 1 -type f -regex '. * (. log | .out)' | xargs -i mv {} {} date +%Y%m%d-%H%M%S
-i or -I, it depends on linux support, each item of xargs The name is generally assigned to {} line by line, which can be replaced with {}.
$ ls
1.txt 2.txt 3.txt log.xml
$ ls * .txt | xargs -t -i mv {} {} .bak
mv 1.txt 1.txt.bak
mv 2.txt 2.txt.bak
mv 3.txt 3.txt.bak
$ ls
1.txt.bak 2.txt.bak 3.txt.bak log.xml
Note that -I must specify the replacement character-whether to specify the replacement character-optional

Other common parameters
of find 1. The general form of the
find command find pathname -options [-print -exec -ok ...]

2. The
path path of the find command parameter pathname.
-Print Output the matched file to standard output.
-Exec Execute the shell command given by the parameter to the matched file. The corresponding command is in the form of 'command' {} ;, Pay attention to the space between {} and;.
-Ok and -exec have the same effect, but execute commands in a safer way, give a prompt before executing each command, and let the user determine whether to execute.

3. The find command option
-name finds files according to the file name. Followed by "filename".
$ Find. -Name "[AZ] " -print
$ find / etc -name "host
" -print
$ find ~ -name " " -print Or find. -Print
$ find / -name "
" -print
$ find. -Name "[az] [az] [0--9] [0--9] .txt" -print

-perm Find files according to file permissions.
$ find. -perm 755 -print

-prune How to avoid a file directory when using find to find files.

-user finds files by file owner.
-nouser finds files without a valid owner, that is, the owner of the file does not exist in / etc / passwd.
-group searches files by the group to which the file belongs.
-nogroup finds none The file to which the group belongs is valid, that is, the group to which the file belongs does not exist in / etc / groups.

-newer file1! file2 Find files whose change time is newer than file1 but older than file2.
If you want to find all files whose change time is newer than a certain file but older than another file, you can use the -newer option. Its general form For: newest_file_name! Oldest_file_name
where ,! It is a logical not symbol.

find -newer httpd1.conf ! -newer temp -ls

Find the file whose change time is newer than the temp file:
$ find. -Newer temp -print

-type Find a certain type of file, such as: b-block device file. d-directory. c-character device file. p-pipeline file. l-symbolic link file. f-ordinary file.

-size n: [c] Find a file with a length of n blocks. With c, the file length is in bytes.
Find files with a file length greater than 1 M bytes in the current directory:
find. -size + 1000000c
find . -size + 1k
find. -size + 1M
Find files with a file length of exactly 100 bytes in the / home / apache directory:
find / home / apache -size 100c -print
Find files longer than 10 blocks in the current directory (One block is equal to 512 bytes):
find. -Size +10 -print

-maxdepth 2 Search directory depth is level 2.

-fstype Find files located in a certain type of file system. These file system types can be found in / etc / fstab. This file contains information about the file system in this system.

-follow If the find command encounters a symbolic link file, it tracks to the file pointed to by the link.

-cpio Use the cpio command on the matching files to back up these files to the tape device.
Use the find and cpio commands in combination
cd / path / to / source / dir
find. | cpio -pdumv / path / to / destination / dir
cpio command Is a copy command, it is designed to copy files to or from a cpio or tar archive file, and automatically maintain the permissions, time and ownership of files and subdirectories.

-mtime -n + n to find the file according to the modification time of the file,
-n means the file change time is within n days from the present,
+ n means the file change time is n days before the present.

The find command also has -atime (access time) and -ctime (creation time) options, but they are similar to the -m time option.

-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 file status was changed in the last N minutes
-ctime n find last n
24 hours in the system The file
whose file status is changed -mmin n Find the file whose file data was changed in the last N minutes in the
system -mtime n Find the file whose file data was changed in the last n * 24 hours in the system

If you want to find the file according to the changed time, you can use the mtime, atime or ctime option.
If the system suddenly has no free space, it is likely that the length of a certain file grows rapidly during this period, then you can use the mtime option to find this Files.
Use minus sign to limit files whose change time is within n days ago, and use plus sign + to limit files whose change time is before n days ago.
Hope to find the change time in the system root directory on the 5th Within the file, you can use:
$ find / -mtime -5 -print
In order to find the file with the change time before 3 days in the / var / adm directory, you can use:
$ find / var / adm -mtime +3 -print

Guess you like

Origin www.cnblogs.com/chanix/p/12737834.html