1. Linux contains a large number of files. For file search, Linux providesfind
Command.
find
It is a very effective tool. It can traverse the target directory or even the entire file system to find some files or directories:
find [path...] [expression]
amongexpression
There are three types:options
、tests
andactions
。 Multiple expressions are separated by operators. When the operator is omitted, the default operator is used-and
。
When the expression does not contain anyactions
The default is-print
, that is, print out all the documents searched, separated by a newline.
In fact, all three expressions can be regarded as options, indicating some restrictions on search (e.g-maxdepth
Indicates the maximum depth of the search path), or some kind of test on the found target file (e.g-readable
Judge whether it is readable or not, or take some action on the result (e.g-print
)。
option-name pattern
Search file name:
[[email protected] 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
[[email protected] temp]#
In this example, search all files under the directory / root to find the matchfile?
The file name of is not specifiedaction
, so use the default-print
Print out the results. In the find command, shell wildcards can be used to represent the search path and some file names (see the previous article), but in order to avoid confusion, the wildcards after the options need to be enclosed in quotation marks.
option-maxdepth n
Specifies the maximum depth of the search path:
[ [email protected] ~]# find /root -maxdepth 1 -name "file?" # Note the implicit operator - and between expressions
/root/file1
[[email protected] ~]#
In this example, specifying a maximum depth of 1 means that only the / root directory is searched without entering any of its subdirectories.
Corresponding to this option,-mindepth
Represents the minimum depth of the specified search path.
option-user name
Find files by their owner:
[[email protected] ~]# find /root/temp -name "file?" -user learner
/root/temp/file1
/root/temp/file2
[[email protected] ~]#
Or similar options-uid n
Indicates the uid owned by the file,-gid n
Indicates the GID of the group to which the file belongs,-group name
Indicates the group to which the file belongs.
option-mtime n
The last time the content of the file was modified is n * 24 hours now:
[[email protected] temp]# ls -lt file1?
-Rw-r -- R -- 1 root 64 October 27 15:06 file11
-Rw-r -- R -- 1 root 132 October 27 13:28 file10
-Rw-r -- R -- 1 root 22 October 26 21:31 file12
-Rw-r -- R -- 1 root 137 October 12 16:42 file13
[ [email protected] temp]# find . - name "file1?" - Five days ago
./file13
[[email protected] temp]#
[ [email protected] temp]# find . - name "file1?" - Mtime - within 5 # five days
./file10
./file11
[[email protected] temp]#
[ [email protected] temp]# find . - name "file1?" - Mtime 5 # just five days
./file12
[[email protected] temp]#
The command is used in this examplels
Options for-t
Sort the time of files, and the most recently modified files come first. option-mtime n
Where n can be expressed as:
+N means greater than n
-N means less than n
N means equal to n
There are other times (such as atime, CTime) for comparison. The usage is the same.
option-newer file
Indicates that the searched file is larger than the specified onefile
“New”(the last time the content was modified is shorter than now):
[[email protected] temp]# find . -name "file1?" -newer file12
./file10
./file11
[[email protected] temp]#
option-path pattern
File name matches pattern (wildcard):
[[email protected] temp]# find . -name "file1?" -path "./file1[13]"
./file11
./file13
[[email protected] temp]#
be carefulpattern
Will not match/
and.
Special treatment.
Usually – path matches the option-prune
Use to exclude a directory:
[[email protected] temp]# find . -name "file*"
./file10
./file12
./file11
./tmp/file
./file13
[[email protected] temp]#
[[email protected] temp]# find . -path "./tmp" -prune -o -name "file*" -print
./file10
./file12
./file11
./file13
[[email protected] temp]#
there-o
It means or, it’s different from what I said before-and
All operators. Represents the logical relationship between expressions. In this example, it can be understood as: if the directories match./tmp
Then execute-prune
Skip the directory, otherwise it will not match-name
Specify the file and execute it-print
。
In addition to these two operators, the operator!
or-not
Represents a logical non operator(...)
Similar to parentheses in mathematical operations, it indicates that the priority is increased:
[[email protected] temp]# find . ! -path "./tmp*" -name "file*"
./file10
./file12
./file11
./file13
[[email protected] temp]#
#Exclude multiple directories:
[[email protected] temp]# find . \( -path "./tmp" -o -path "./abcd" \) -prune -o -name "file*" -print
./file10
./file12
./file11
./file13
[[email protected] temp]#
Pay attention here(...)
The operator needs to be escaped (to avoid being interpreted by the shell as other meanings), and a backslash ‘\’ is added before the symbol. (we will elaborate on escape or reference in shell when talking about bash programming)
option-type x
Represents a file of search type XWhere possible values of X includeb
、c
、d
、p
、f
、l
、s
。 They and commandsls
The displayed file types are the same (see Introduction 1 of basic commands),f
Represents ordinary documents.
[[email protected] temp]# ln -s file13 file14
[[email protected] temp]# ls -l file14
Lrwxrwxrwx 1 root 6 November 1 12:29 file14 - > file13
[[email protected] temp]# find . -type l
./file14
[[email protected] temp]#
option-perm mode
Represents files that search for specific permissions:
[[email protected] temp]# chmod 777 file14
[[email protected] temp]# ls -l file1[3-4]
-Rwxrwxrwx 1 root 137 October 12 16:42 file13
Lrwxrwxrwx 1 root 6 November 1 12:29 file14 - > file13
[[email protected] temp]#
[[email protected] temp]# find . -perm 777
./file13
./file14
[[email protected] temp]#
Or expressed as:
[ [email protected] temp]# find . - Perm - g = RWX # indicates that the permissions of the group to which the file belongs are readable, writable and executable.
./file13
./file14
[[email protected] temp]#
option-size n
Indicates the size of the search file
[[email protected] temp]# find . -path "./*" -size +100c
./file10
./file13
[[email protected] temp]#
In this example+100c
Represents files larger than 100 bytes in the current directory. N is similar to the previous way of representing time (+ N, – N, n). The characters after n also include:
B block whose unit is 512 bytes (the default unit when there is no suffix after n)
k 1024 bytes
M 1048576 bytes
G 1073741824 bytes
option-print0
similar-print
Output file names, but do not separate them with any characters. Used when the file name contains special characters. Can be matched with belt options-0
Command ofxargs
Use together (described later).
option-exec command ;
Indicates the command to execute-exec
After that, you can follow any shell command to further process the searched files. Between command and semicolon, they are regarded as the parameters of command, where{}
Represents the searched file. The semicolon needs to be escaped.
For example, execute the command on the searched filels -l
:
[[email protected] temp]# find . -name "file*" -exec ls -l {} \;
-Rw-r -- R -- 1 root 132 October 27 13:28/ file10
-Rw-r -- R -- 1 root 22 October 26 21:31/ file12
-Rw-r -- R -- 1 root 64 October 27 15:06/ file11
-Rw-r -- R -- 1 root 67 October 31 17:50/ tmp/file
-Rw-r -- R -- 1 root 0 November 1 12:05/ abcd/file15
-Rwxrwxrwx 1 root 137 October 12 16:42/ file13
Lrwxrwxrwx 1 root 6 November 1 12:29/ file14 -> file13
-exec
The command after the option is at startupfind
It is executed in the directory where it is located, and the command is executed once for each searched file, instead of listing all files after the command and executing it only once.
Give an example of the difference:
#The command echo is executed only once
[[email protected] temp]# echo ./file11 ./file12 ./file13
./file11 ./file12 ./file13
#The echo command was executed three times
[[email protected] temp]# find . -name "file1[1-3]" -exec echo {} \;
./file12
./file11
./file13
[[email protected] temp]#
When using format-exec command {} +
When, it means that each file is appended to the command, so the command is executed only once:
[[email protected] temp]# find . -name "file1[1-3]" -exec echo {} +
./file12 ./file11 ./file13
[[email protected] temp]#
But sometimes problems arise:
[[email protected] temp]# find . -name "file1[1-3]" -exec mv {} abcd/ +
Find: missing parameter of '- exec'
[[email protected] temp]#
Because here the file is appended to the directoryabcd/
After the, resulting in an error.
At the same time, use the format-exec command {} +
It may also cause too many files to be appended, exceeding the operating system’s limit on the length of the command line.
use-exec
There may be a security vulnerability, usually using a pipeline and another commandxargs
To replace-exec
Execute the command.
2、xargs
Obtain the parameters of the command from the standard input and execute it
xargs
Get the items separated by spaces from standard input and execute the command (the default is / bin / echo)
option-0
The delimiter of the item will be ignored in conjunction with the find option-print0
, processing files with special symbols.
[[email protected] temp]# find . -name "file*" -print0 | xargs -0 ls -l
-Rw-r -- R -- 1 root 132 October 27 13:28/ file10
-Rw-r -- R -- 1 root 64 October 27 15:06/ file11
-Rw-r -- R -- 1 root 22 October 26 21:31/ file12
-Rwxrwxrwx 1 root 137 October 12 16:42/ file13
-Rw-r -- R -- 1 root 0 November 1 14:45/ File 14 # note that this file name contains spaces
When not in use:
[[email protected] temp]# find . -name "file*" | xargs ls
Ls: cannot access/ File: there is no such file or directory
Ls: cannot access 14: there is no file or directory
./file10 ./file11 ./file12 ./file13
option-I string
Specify an alternate string for the input item:
[[email protected] temp]# ls abcd/
[[email protected] temp]# find . -name "file*" | xargs -I{} mv {} abcd/
[[email protected] temp]# ls abcd/
file10 file11 file12 file13
[[email protected] temp]#
This means to use-I
The following string is used to replace the input items, so that they can be put into any position of the command as a whole. Also avoided-exec command {} +
Error.
option-d
Specify the separator for the input item:
[[email protected] temp]# head -n1 /etc/passwd
root:x:0:0:root:/root:/bin/bash
[[email protected] temp]# head -n1 /etc/passwd|xargs -d ":" echo -n
root x 0 0 root /root /bin/bash
[[email protected] temp]#
option-P
Specify the maximum number of processes. The default number of processes is 1. Multiple processes execute concurrently.
3、date
Print or set system time
date [OPTION]... [+FORMAT]
When there are no parameters, the current time is displayed:
[[email protected] temp]# date
Tuesday, November 1, 2016 15:30:46 CST
[[email protected] temp]#
option-d string
Display the time according to the description string (in the example, the string represents the number of seconds from the zero point of 1970-01-01):
[[email protected] temp]# date --date='@2147483647'
Tuesday, January 19, 2038 11:14:07 CST
Or:
[[email protected] temp]# date [email protected]
Tuesday, January 19, 2038 11:14:07 CST
-d
The following string can also be:
[[email protected] temp]# date -d "-1 day"
Monday, October 31, 2016 16:11:27 CST
Yesterday
For example, next year is expressed as:
[[email protected] temp]# date -d "1 year"
Wednesday, November 1, 2017 16:12:27 CST
option-s
Set system time:
[[email protected] temp]# date -s "2016-11-01 15:49"
Tuesday, November 1, 2016 15:49:00 CST
[[email protected] temp]# date
Tuesday, November 1, 2016 15:49:03 CST
Since the Linux system will read the CMOS to obtain the time when it is started, the system will write the system time to CMOS at regular intervals. In order to avoid that the time is not written to CMOS due to the immediate restart of the system after changing the time, the command is usually used after setting the timeclock -w
Write the system time into CMOS.date
Format controls the output format in the command, plus sign+
Before format indicates the beginning of format:
[[email protected] temp]# date "+%Y-%m-%d %H:%M:%S"
2016-11-01 16:00:45
[[email protected] temp]#
In this example, the format is caused by double quotation marks to avoid being misunderstood by the shell, where:
%Y means year
%M means month
%D stands for days
%H is the hour
%M stands for minutes
%S stands for seconds
Many other formats can also be specified
If only the current time is output:
[[email protected] temp]# date "+%T"
16:03:50
[[email protected] temp]#
For example, the number of seconds from the zero point of 1970-01-01 to the present time:
[[email protected] temp]# date +%s
1477987540
[[email protected] temp]#
If the day of the week is output:
[[email protected] temp]# date +%A
Tuesday
[[email protected] temp]#
For other formats, please check by yourself
4、gzip
Compress or unzip files
gzip [OPTION]... [FILE]...
When the command is directly followed by a file, it means that the file is compressed:
[[email protected] temp]# ls -l file1*
-Rw-r -- R -- 1 root 132 October 27 13:28 file10
-Rw-r -- R -- 1 root 64 October 27 15:06 file11
-Rw-r -- R -- 1 root 22 October 26 21:31 file12
-Rw-r -- R -- 1 root 137 October 12 16:42 file13
[[email protected] temp]#
[[email protected] temp]# gzip file10 file11 file12 file13
[[email protected] temp]# ls -l file1*
-Rw-r -- R -- 1 root 75 October 27 13:28 file10 gz
-Rw-r -- R -- 1 root 49 October 27 15:06 file11 gz
-Rw-r -- R -- 1 root 44 October 26 21:31 file12 gz
-Rw-r -- R -- 1 root 109 October 12 16:42 file13 gz
Compressed file to.gz
At the end, gzip does not keep the source file
option-d
Represents decompression
[[email protected] temp]# gzip -d *.gz
[[email protected] temp]# ls -l file1*
-Rw-r -- R -- 1 root 132 October 27 13:28 file10
-Rw-r -- R -- 1 root 64 October 27 15:06 file11
-Rw-r -- R -- 1 root 22 October 26 21:31 file12
-Rw-r -- R -- 1 root 137 October 12 16:42 file13
option-r
You can recursively enter the directory and compress the files inside
option-n
Specify the compression level, where n is a number from 1 to 9. 1 is the fastest compression, but the compression ratio is the smallest; 9 has the slowest compression speed, but the compression ratio is the largest. By default, n is 6.
[[email protected] temp]# gzip -r9 ./tmp
After gzip, there is no file or the file is-
When, it will be read from the standard input and compressed:
[[email protected] temp]# echo "hello world" | gzip >hello.gz
[[email protected] temp]# ls -l *.gz
-Rw-r -- R -- 1 root 32 November 1 16:40 hello gz
Note that in the example, the output of gzip is redirected to a filehello.gz
If you unzip this file, a file will be generatedhello
。 If the suffix of the redirected file is not.gz
, the file name was changed to GZ suffix will not be decompressed before.
5、zcat
Output compressed file contents to standard output
[[email protected] temp]# zcat hello.gz
hello world
[[email protected] temp]#
Zcat reads gzip compressed files, as long as the file format is correct, and the file name does not need to have GZ suffix.
6、bzip2
Compress and decompress files
bzip2 [OPTION]... [FILE]...
commandbzip2
andgzip
Similar are compression commands, but the compression algorithms used are different, usuallybzip2
The compression of is relatively high. This command also does not retain the source file by default, and the default file name suffix is.bz2
:
[[email protected] temp]# bzip2 file11
[[email protected] temp]# ls -l file11.bz2
-Rw-r -- R -- 1 root 61 October 27 15:06 file11 bz2
option-k
Keep source files:
[[email protected] temp]# bzip2 -k file10
[[email protected] temp]# ls -l file10*
-Rw-r -- R -- 1 root 132 October 27 13:28 file10
-Rw-r -- R -- 1 root 96 October 27 13:28 file10 bz2
option-d
Indicates decompression (if there is a source file, an error is reported):
[[email protected] temp]# bzip2 -d file10.bz2
bzip2: Output file file10 already exists.
[[email protected] temp]# bzip2 -d file11.bz2
[[email protected] temp]# ls -l file11
-Rw-r -- R -- 1 root 64 October 27 15:06 file11
option-f
Indicates that the source file is forced to be overwritten:
[[email protected] temp]# bzip2 -d -f file10.bz2
[[email protected] temp]# ls -l file10*
-Rw-r -- R -- 1 root 132 October 27 13:28 file10
option-n
andgzip
Consistent usage, indicating compression ratio.
7、tar
Package compressed files
tar [OPTION...] [FILE]...
commandgzip
andbzip2
Compressed directories are not supported (although gzip can use the option – r to compress the directory, it still cannot compress the directory)tar
The command can archive the directory and then compress it with the compression command:
[[email protected] temp]# tar -cf tmp.tar tmp/
[[email protected] temp]# ls -l
Total consumption 18256
Drwxr-xr-x 2 root 6 November 1 16:23 ABCD
-Rwxr-xr-x 1 root 12 October 28 17:24 test sh
Drwxr-xr-x 2 root 425984 November 1 17:08 TMP
-Rw-r -- R -- 1 root 18001920 November 1 17:17 TMP tar
Options in the example-c
Represents the creation of a packaged file,-f tmp.tar
Indicates that the specified package file name is TMP Tar, followed by the name of the packaged directorytmp/
。
option-t
List archived content
option-v
List the documents processed in detail
[[email protected] temp]# ls -l abcd.tar
-Rw-r -- R -- 1 root 10240 November 2 08:58 ABCD tar
[[email protected] temp]# tar -tvf abcd.tar
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 the archive.
[[email protected] temp]# touch abcd/file15
[[email protected] temp]# tar uvf abcd.tar abcd
abcd/file15
[[email protected] temp]# tar tvf abcd.tar
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
Extract archive files. (unpacking)
[[email protected] temp]# rm -rf abcd/
[[email protected] temp]# tar -xvf abcd.tar
abcd/
abcd/file10
abcd/file11
abcd/file12
abcd/file13
abcd/file15
[ [email protected] Temp]# LS ABCD # here is the completion result of pressing tab twice
abcd/ abcd.tar
[[email protected] temp]#
option-O
Unzip the file to standard output
[[email protected] temp]# tar -xf abcd.tar -O
hello
hello
hello
hello
[ [email protected] Temp]## note that the contents of each archive file are output here
[[email protected] temp]# tar -xf abcd.tar -O | xargs echo
hello hello hello hello
[[email protected] temp]#
option-p
Retain file permissions (when unpacking).
option-j
、-J
、-z
For compression.
among-j
Use commandbzip2
,-J
Use commandxz
,-z
Use commandgzip
Compress and decompress the archive files respectively (commandtar
The following options can be omitted-
):
[[email protected] temp]# tar zcf tmp.tar.gz tmp
[[email protected] temp]# tar jcf tmp.tar.bz2 tmp
[[email protected] temp]# tar Jcf tmp.tar.xz tmp
[[email protected] temp]# du -sh tmp*
70M tmp
28K tmp.tar.bz2
180K tmp.tar.gz
40K tmp.tar.xz
[[email protected] temp]#
In this example, three compression formats are used for compression, and you can see the commandbzip2
The highest compression ratio, commandgzip
The compression ratio is the lowest. When executing compressed files, compression time is also an important factor we consider. By default, usegzip
Fastest,xz
Slowest.
For decompressing the compressed files of these three formats, you only need to add the options in the-c
change into-x
Just.
option-X FILE
Exclude files that match the patterns listed in file:
[[email protected] abcd]# cat file
file10
file13
[[email protected] abcd]# tar -X file -cf file.tar file*
[[email protected] abcd]# tar -tvf file.tar
-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 the file:
[[email protected] abcd]# cat file
file1[2-3]
[[email protected] abcd]# tar -X file -cf file.tar file*
[[email protected] abcd]# tar -tvf file.tar
-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
Change to dir directory (when unpacking):
[[email protected] temp]# tar zxf tmp.tar.gz -C abcd
[[email protected] temp]# ls -l abcd/
Total consumption 688
-Rw-r -- R -- 1 root November 2 10:20 file
-Rw-r -- R -- 1 root 6 November 2 10:02 file10
-Rw-r -- R -- 1 root 6 November 2 10:02 file11
-Rw-r -- R -- 1 root 6 November 2 10:02 file12
-Rw-r -- R -- 1 root 6 November 2 10:02 file13
-Rw-r -- R -- 1 root 0 November 2 09:07 file15
Drwxr-xr-x 2 root 425984 November 1 17:08 TMP
Extract only the specified files:
[[email protected] temp]# tar zxvf tmp.tar.gz -C abcd/ file1[23]
file12
file13
[[email protected] temp]# ls -l abcd
Total consumption 12
-Rw-r -- R -- 1 root 6 November 16 15:26 file12
-Rw-r -- R -- 1 root 6 November 16 15:26 file13
Note that when decompressing here, the specified file cannot be in the option-C
before
If you do not want to decompress the compressed package but want to view the contents of a file in the compressed package, you can use the following techniques:
[[email protected] temp]# tar zxf tmp.tar.gz file -O
BLOG ADDRESS IS "https://segmentfault.com/blog/learnning"
[[email protected] temp]#
This article describes the use of commands and some options related to file search, archiving and compression in Linux, which are often used in the process of system management. Skilled use is required.