1. Linux contains a large number of files. For file search, Linux provides the find command.
find is a very effective tool that can traverse the target directory or even the entire file system to find certain files or directories:
find [path...] [expression]
inexpressionIt includes three types: options, tests and actions. Multiple expressions are separated by operators. When the operator is omitted, it means that the default operator -and is used.
When the expression does not contain any actions, -print is used by default.That is, print out all the searched files and separate them with newlines.
In fact, all three expressions can be regarded as options, indicating some limitations on search (such as -maxdepth represents the maximum depth of the search path), or some tests on the found target file (such as -readable to determine whether it is readable), or some action taken on the result (such as -print).
Option-name pattern Search for file name:
[root@centos7 temp]# find /root/* -name "file?" /root/file1 /root/temp/file1 /root/temp/file2 /root/temp/file3 /root/temp/file4 /root/temp/file5 /root/temp/file6 /root/temp/file7 /root/temp/file8 /root/temp/file9 [root@centos7 temp]#
In this example, search for all files in directory/root to find the file name that matches file?. At the same time, since there is no action specified, the default -print is used to print the result. In the find command, the shell wildcard character can be used for the search path and the representation of certain file names (see previous article), but to avoid confusion, the wildcard character after the option needs to be enclosed in quotation marks.
Options-maxdepth nSpecify the maximum depth of the search path:
[root@centos7 ~]# find /root -maxdepth 1 -name "file?" #Note the implicit operators between expressions -and/root/file1 [root@centos7 ~]#
In this example, specifying the maximum depth of 1 means that only searches for /root directory, without entering any of its subdirectories to search.
Corresponding to this option, -mindepth represents the minimum depth of the specified search path.
Options-user nameSearch for files according to the file owner:
[root@centos7 ~]# find /root/temp -name "file?" -user learner /root/temp/file1 /root/temp/file2 [root@centos7 ~]#
Or similar options-uid nIt indicates the uid of the file owner, -gid n indicates the gid of the group to which the file belongs, and -group name indicates the group to which the file belongs.
Options-mtime n The last time the file content was modified, it is now n*24 hours:
[root@centos7 temp]# ls -lt file1? -rw-r--r-- 1 root root 64 10moon 27 15:06 file11 -rw-r--r-- 1 root root 132 10moon 27 13:28 file10 -rw-r--r-- 1 root root 22 10moon 26 21:31 file12 -rw-r--r-- 1 root root 137 10moon 12 16:42 file13 [root@centos7 temp]# find . -name "file1?" -mtime +5 #5 days ago./file13 [root@centos7 temp]# [root@centos7 temp]# find . -name "file1?" -mtime -5 #within five days./file10 ./file11 [root@centos7 temp]# [root@centos7 temp]# find . -name "file1?" -mtime 5 #It's just five days./file12 [root@centos7 temp]#
In this example, the option -t of the command ls is used to sort the file time, and the most recently modified file is in front. The option -mtime n in n can be represented as:
+n means greater than n
-n means less than n
n means equal to n
There are also comparisons of other times (such as atime, ctime), and the usage is the same.
Options-newer fileIndicates that the searched file is 'new' than the specified file (the last content was modified shorter than the current time):
[root@centos7 temp]# find . -name "file1?" -newer file12 ./file10 ./file11 [root@centos7 temp]#
Option-path pattern filename matches pattern (wildcard):
[root@centos7 temp]# find . -name "file1?" -path "./file1[13]" ./file11 ./file13 [root@centos7 temp]#
Note that the pattern matches will not be specially processed for / and .
Usually -path will be used with option -prune to indicate the exclusion of a certain directory:
[root@centos7 temp]# find . -name "file*" ./file10 ./file12 ./file11 ./tmp/file ./file13 [root@centos7 temp]# [root@centos7 temp]# find . -path "./tmp" -prune -o -name "file*" -print ./file10 ./file12 ./file11 ./file13 [root@centos7 temp]#
The -o means or, it is an operator with the -and mentioned earlier. Represents the logical relationship between expressions. In this example, it can be understood as: if the directory matches ./tmp, execute -prune to skip the directory, otherwise match the file specified by -name and execute -print.
Except for these two operators, operator! or -not means logical non-, operator (...) is similar to brackets in mathematical operations, indicating increased priority:
[root@centos7 temp]# find . ! -path "./tmp*" -name "file*" ./file10 ./file12 ./file11 ./file13 [root@centos7 temp]# #Exclude multiple directories:[root@centos7 temp]# find . \( -path "./tmp" -o -path "./abcd" \) -prune -o -name "file*" -print ./file10 ./file12 ./file11 ./file13 [root@centos7 temp]#
Note that the (...) operator here needs to be escaped (to avoid being interpreted by shell as other meanings), with a backslash '\' before the symbol. (We will explain in detail about escapes or references in shells when talking about bash programming)
Options-type xRepresents a file of search type x, where possible values of x include b, c, d, p, f, l, s. They are the same as the file type displayed by the command ls (see Introduction to Basic Command 1), and f represents a normal file.
[root@centos7 temp]# ln -s file13 file14 [root@centos7 temp]# ls -l file14 lrwxrwxrwx 1 root root 6 11moon 1 12:29 file14 -> file13 [root@centos7 temp]# find . -type l ./file14 [root@centos7 temp]#
Options-perm modeFiles that indicate searching for specific permissions:
[root@centos7 temp]# chmod 777 file14 [root@centos7 temp]# ls -l file1[3-4] -rwxrwxrwx 1 root root 137 10moon 12 16:42 file13 lrwxrwxrwx 1 root root 6 11moon 1 12:29 file14 -> file13 [root@centos7 temp]# [root@centos7 temp]# find . -perm 777 ./file13 ./file14 [root@centos7 temp]#
Or expressed as:
[root@centos7 temp]# find . -perm -g=rwx # indicates that the permissions of the group to which the file belongs are readable, writable, and executable../file13 ./file14 [root@centos7 temp]#
Option -size n means search file size
[root@centos7 temp]# find . -path "./*" -size +100c ./file10 ./file13 [root@centos7 temp]#
In this example +100c means a file greater than 100 bytes in the current directory. n is similar to the previous method of representing time (+n,-n,n). The characters after n also include:
b block with unit 512 bytes (the default unit when n is not followed by suffix)
k 1024 bytes
M 1048576 bytes
G 1073741824 bytes
Option -print0 is similar to -print outputs file names, but does not separate them with any characters. Used when the file name contains special characters. It can be used with the command xargs with option -0 (described later).
Options-exec command ;Indicates the command to be executed
-exec can be followed by any shell command to further process the searched file. It is regarded as a command parameter between command and semicolon, where {} is used to represent the searched file. The semicolon needs to be escaped.
For example, execute the command ls -l on the searched file:
[root@centos7 temp]# find . -name "file*" -exec ls -l {} \; -rw-r--r-- 1 root root 132 10moon 27 13:28 ./file10 -rw-r--r-- 1 root root 22 10moon 26 21:31 ./file12 -rw-r--r-- 1 root root 64 10moon 27 15:06 ./file11 -rw-r--r-- 1 root root 67 10moon 31 17:50 ./tmp/file -rw-r--r-- 1 root root 0 11moon 1 12:05 ./abcd/file15 -rwxrwxrwx 1 root root 137 10moon 12 16:42 ./file13 lrwxrwxrwx 1 root root 6 11moon 1 12:29 ./file14 -> file13
-execThe command after the option is startedfindExecutes in the directory where it is located, and for each searched file, the command is executed once, instead of listing all files after the command, it is executed only once.
Give an example to illustrate the difference:
#Command echo is executed only once[root@centos7 temp]# echo ./file11 ./file12 ./file13 ./file11 ./file12 ./file13 #Command echo was executed three times[root@centos7 temp]# find . -name "file1[1-3]" -exec echo {} \; ./file12 ./file11 ./file13 [root@centos7 temp]#
When using the format -exec command {} +, it means that each file is appended to the command, so that the command is executed only once:
[root@centos7 temp]# find . -name "file1[1-3]" -exec echo {} + ./file12 ./file11 ./file13 [root@centos7 temp]#
But sometimes problems arise:
[root@centos7 temp]# find . -name "file1[1-3]" -exec mv {} abcd/ + find: Leaked“-exec”Parameters of [root@centos7 temp]#
Because this file is appended to the directory abcd/, an error is reported.
At the same time, using the format -exec command {} + may also cause too many appended files to exceed the operating system's limit on the command line length.
Using -exec can have security vulnerabilities, and usually use pipelines and another command xargs to execute commands instead of -exec.
2. xargs obtains the command parameters from standard input and executes
xargs gets space-delimited items from standard input and executes the command (default is /bin/echo)
Option -0 will ignore the project's separator and cooperate with the find option -print0 to process files with special symbols.
[root@centos7 temp]# find . -name "file*" -print0 | xargs -0 ls -l -rw-r--r-- 1 root root 132 10moon 27 13:28 ./file10 -rw-r--r-- 1 root root 64 10moon 27 15:06 ./file11 -rw-r--r-- 1 root root 22 10moon 26 21:31 ./file12 -rwxrwxrwx 1 root root 137 10moon 12 16:42 ./file13 -rw-r--r-- 1 root root 0 11moon 1 14:45 ./file 14 #Note that this file name contains spaces
When not in use:
[root@centos7 temp]# find . -name "file*" | xargs ls ls: Unable to access./file: There is no file or directory ls: Unable to access14: There is no file or directory ./file10 ./file11 ./file12 ./file13
selectitem-I stringSpecify an alternative string for the input item:
[root@centos7 temp]# ls abcd/ [root@centos7 temp]# find . -name "file*" | xargs -I{} mv {} abcd/ [root@centos7 temp]# ls abcd/ file10 file11 file12 file13 [root@centos7 temp]#
What this means is to use the strings after -I instead of input items, so that they can be executed as a whole at any position in the command. The error of -exec command {} + is also avoided.
Options-dSpecify the separator for the input item:
[root@centos7 temp]# head -n1 /etc/passwd root:x:0:0:root:/root:/bin/bash [root@centos7 temp]# head -n1 /etc/passwd|xargs -d ":" echo -n root x 0 0 root /root /bin/bash [root@centos7 temp]#
Option -P specifies the maximum number of processes, the default number of processes is 1, and multiple processes execute concurrently.
3. Date Print or set the system time
date [OPTION]... [+FORMAT]
When there are no parameters, it means that the current time is displayed:
[root@centos7 temp]# date 2016Year 11moon 01day Tuesday 15:30:46 CST [root@centos7 temp]#
Option -d string displays the time according to the description string (in the example, the string represents the number of seconds from 1970-01-01 zero point):
[root@centos7 temp]# date --date='@2147483647' 2038Year 01moon 19day Tuesday 11:14:07 CST
or:
[root@centos7 temp]# date -d@2147483647 2038Year 01moon 19day Tuesday 11:14:07 CST
The string after -d can also be:
[root@centos7 temp]# date -d "-1 day" 2016Year 10moon 31day Monday 16:11:27 CST
Say yesterday
Another example is expressed next year:
[root@centos7 temp]# date -d "1 year" 2017Year 11moon 01day Wednesday 16:12:27 CST
Options -s set system time:
[root@centos7 temp]# date -s "2016-11-01 15:49" 2016Year 11moon 01day Tuesday 15:49:00 CST [root@centos7 temp]# date 2016Year 11moon 01day Tuesday 15:49:03 CST
Since CMOS is read when the Linux system is started to obtain time, the system will write system time to CMOS every once in a while. In order to avoid the immediate restart of the system after changing the time, the time is not written to CMOS, usually after setting the time, the command clock -w will be used to write system time to CMOS.
FORMAT controls the output format in the date command, and the plus sign + indicates the start of the format before the format:
[root@centos7 temp]# date "+%Y-%m-%d %H:%M:%S" 2016-11-01 16:00:45 [root@centos7 temp]#
In this example, the format is enclosed in double quotes to avoid being misunderstood by the shell, where:
%Y means year
%m means month
%d means day
%H means hour
%M means minutes
%S means seconds
You can also specify many other formats such as outputting only the current time:
[root@centos7 temp]# date "+%T" 16:03:50 [root@centos7 temp]#
For example, the number of seconds from 1970-01-01 to the current time:
[root@centos7 temp]# date +%s 1477987540 [root@centos7 temp]#
For example, output today's week:
[root@centos7 temp]# date +%A Tuesday [root@centos7 temp]#
Please use other formats
4. gzip compress or decompress files
gzip [OPTION]... [FILE]...
When the file is directly followed by the command, it means that the file is compressed:
[root@centos7 temp]# ls -l file1* -rw-r--r-- 1 root root 132 10moon 27 13:28 file10 -rw-r--r-- 1 root root 64 10moon 27 15:06 file11 -rw-r--r-- 1 root root 22 10moon 26 21:31 file12 -rw-r--r-- 1 root root 137 10moon 12 16:42 file13 [root@centos7 temp]# [root@centos7 temp]# gzip file10 file11 file12 file13 [root@centos7 temp]# ls -l file1* -rw-r--r-- 1 root root 75 10moon 27 13:28 -rw-r--r-- 1 root root 49 10moon 27 15:06 -rw-r--r-- 1 root root 44 10moon 26 21:31 -rw-r--r-- 1 root root 109 10moon 12 16:42
The compressed file ends with .gz, gzip does not retain the source file.
Options-dIndicates decompression
[root@centos7 temp]# gzip -d *.gz [root@centos7 temp]# ls -l file1* -rw-r--r-- 1 root root 132 10moon 27 13:28 file10 -rw-r--r-- 1 root root 64 10moon 27 15:06 file11 -rw-r--r-- 1 root root 22 10moon 26 21:31 file12 -rw-r--r-- 1 root root 137 10moon 12 16:42 file13
Options-rYou can recursively enter the directory and compress the files inside
Options-nSpecify the compression level, n is a number from 1-9. 1 is the fastest compression, but the compression ratio is the smallest; 9 is the slowest compression speed, but the compression ratio is the largest. When default, n is 6.
[root@centos7 temp]# gzip -r9 ./tmp
When there is no file after gzip or the file is -, it will be read and compressed from standard input:
[root@centos7 temp]# echo "hello world" | gzip > [root@centos7 temp]# ls -l *.gz -rw-r--r-- 1 root root 32 11moon 1 16:40
Note that in the example, the output of gzip is redirected to the file. If the file is decompressed, the file hello will be generated. If the redirected file suffix is not .gz, the file name will not be decompressed until it is changed to the .gz suffix.
5. zcat outputs the compressed file content to standard output
[root@centos7 temp]# zcat hello world [root@centos7 temp]#
zcat reads files compressed by gzip, only if the file format is correct, and the file name does not need to have a suffix of .gz.
6. bzip2 compresses and decompresses files
bzip2 [OPTION]... [FILE]...
The commands bzip2 and gzip are similar to compression commands, but the compression algorithms used are different, and bzip2 usually has higher compression. This command also does not retain the source file by default, and the default file name suffix is .bz2:
[root@centos7 temp]# bzip2 file11 [root@centos7 temp]# ls -l file11.bz2 -rw-r--r-- 1 root root 61 10moon 27 15:06 file11.bz2
Options-kThe source files can be retained:
[root@centos7 temp]# bzip2 -k file10 [root@centos7 temp]# ls -l file10* -rw-r--r-- 1 root root 132 10moon 27 13:28 file10 -rw-r--r-- 1 root root 96 10moon 27 13:28 file10.bz2
Options-dIndicates decompression (if the source file exists, an error will be reported):
[root@centos7 temp]# bzip2 -d file10.bz2 bzip2: Output file file10 already exists. [root@centos7 temp]# bzip2 -d file11.bz2 [root@centos7 temp]# ls -l file11 -rw-r--r-- 1 root root 64 10moon 27 15:06 file11
Options-fIndicates that the source file is forced to be overwritten:
[root@centos7 temp]# bzip2 -d -f file10.bz2 [root@centos7 temp]# ls -l file10* -rw-r--r-- 1 root root 132 10moon 27 13:28 file10
The usage of option -n and gzip are the same, indicating the compression ratio.
7. tar package compressed files
tar [OPTION...] [FILE]...
Both gzip and bzip2 do not support compressing directories (although gzip can be compressed into the directory with option -r, but the directory cannot be compressed). Use the tar command to archive the directory, and then use the compression command to compress it:
[root@centos7 temp]# tar -cf tmp/ [root@centos7 temp]# ls -l Total usage 18256 drwxr-xr-x 2 root root 6 11moon 1 16:23 abcd -rwxr-xr-x 1 root root 12 10moon 28 17:24 drwxr-xr-x 2 root root 425984 11moon 1 17:08 tmp -rw-r--r-- 1 root root 18001920 11moon 1 17:17
In the example, option -c means creating a packaged file, and -f means specifying the packaged file name, followed by the packaged directory name tmp/.
Option -t lists archive content
Options -v lists the processed files in detail
[root@centos7 temp]# ls -l -rw-r--r-- 1 root root 10240 11moon 2 08:58 [root@centos7 temp]# tar -tvf drwxr-xr-x root/root 0 2016-11-02 08:57 abcd/ -rw-r--r-- root/root 6 2016-11-02 08:57 abcd/file10 -rw-r--r-- root/root 6 2016-11-02 08:57 abcd/file11 -rw-r--r-- root/root 6 2016-11-02 08:57 abcd/file12 -rw-r--r-- root/root 6 2016-11-02 08:57 abcd/file13
Option -u Update archive file (update).
[root@centos7 temp]# touch abcd/file15 [root@centos7 temp]# tar uvf abcd abcd/file15 [root@centos7 temp]# tar tvf drwxr-xr-x root/root 0 2016-11-02 08:57 abcd/ -rw-r--r-- root/root 6 2016-11-02 08:57 abcd/file10 -rw-r--r-- root/root 6 2016-11-02 08:57 abcd/file11 -rw-r--r-- root/root 6 2016-11-02 08:57 abcd/file12 -rw-r--r-- root/root 6 2016-11-02 08:57 abcd/file13 -rw-r--r-- root/root 0 2016-11-02 09:07 abcd/file15
Option -x performs extraction operation on the archive file. (Unpacking)
[root@centos7 temp]# rm -rf abcd/ [root@centos7 temp]# tar -xvf abcd/ abcd/file10 abcd/file11 abcd/file12 abcd/file13 abcd/file15 [root@centos7 temp]# ls abcd # Here is the completion result of pressing the tab key twiceabcd/ [root@centos7 temp]#
Option-O Decompress the file to standard output
[root@centos7 temp]# tar -xf -O hello hello hello hello [root@centos7 temp]# #Note that the content of each archive file is output here[root@centos7 temp]# tar -xf -O | xargs echo hello hello hello hello [root@centos7 temp]#
Option -p retains file permissions (for unpacking).
Options -j, -J, -z are used for compression.
Among them -j uses the command bzip2, -J uses the command xz, and -z uses the command gzip to compress and decompress the archive file (the option after the command tar can be omitted-):
[root@centos7 temp]# tar zcf tmp [root@centos7 temp]# tar jcf .bz2 tmp [root@centos7 temp]# tar Jcf tmp [root@centos7 temp]# du -sh tmp* 70M tmp 28K .bz2 180K 40K [root@centos7 temp]#
In this example, three compression formats are used for compression. You can see that the compression ratio is the highest with the command bzip2 and the compression ratio is the lowest with the command gzip. When executing compressed files, compression time is also an important factor in our consideration. By default, gzip is the fastest and xz is the slowest.
For these three formats, just replace -c with -x in the option.
Option -X FILE Exclude files that match the patterns listed in FILE:
[root@centos7 abcd]# cat file file10 file13 [root@centos7 abcd]# tar -X file -cf file* [root@centos7 abcd]# tar -tvf -rw-r--r-- root/root 14 2016-11-02 10:10 file -rw-r--r-- root/root 6 2016-11-02 10:02 file11 -rw-r--r-- root/root 6 2016-11-02 10:02 file12 -rw-r--r-- root/root 0 2016-11-02 09:07 file15
Note that wildcard matching is supported in file FILE:
[root@centos7 abcd]# cat file file1[2-3] [root@centos7 abcd]# tar -X file -cf file* [root@centos7 abcd]# tar -tvf -rw-r--r-- root/root 11 2016-11-02 10:20 file -rw-r--r-- root/root 6 2016-11-02 10:02 file10 -rw-r--r-- root/root 6 2016-11-02 10:02 file11 -rw-r--r-- root/root 0 2016-11-02 09:07 file15
Option -C DIR changes to directory DIR (for unpacking):
[root@centos7 temp]# tar zxf -C abcd [root@centos7 temp]# ls -l abcd/ Total usage 688 -rw-r--r-- 1 root root 11 11moon 2 10:20 file -rw-r--r-- 1 root root 6 11moon 2 10:02 file10 -rw-r--r-- 1 root root 6 11moon 2 10:02 file11 -rw-r--r-- 1 root root 6 11moon 2 10:02 file12 -rw-r--r-- 1 root root 6 11moon 2 10:02 file13 -rw-r--r-- 1 root root 0 11moon 2 09:07 file15 drwxr-xr-x 2 root root 425984 11moon 1 17:08 tmp
Only decompress the specified file:
[root@centos7 temp]# tar zxvf -C abcd/ file1[23] file12 file13 [root@centos7 temp]# ls -l abcd Total usage 12 -rw-r--r-- 1 root root 6 11moon 16 15:26 file12 -rw-r--r-- 1 root root 6 11moon 16 15:26 file13
Note that when decompressing here, the specified file cannot be before option -C
If you don't want to decompress the compressed package but want to view the contents of a file in the compressed package, you can use the following tips:
[root@centos7 temp]# tar zxf file -O BLOG ADDRESS IS "https:///article/" [root@centos7 temp]#
This article describes the usage of commands and some options related to file search and archive compression in Linux. They are often used in the system management process and need to be used proficiently.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.