Linux basic command introduction 3: file search and others

Time:2019-11-20

1. Linux contains a large number of files. For file search, Linux providesfindCommand.

findIs 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]

amongexpressionThere are three types:optionstestsandactions。 Multiple expressions are separated by operators. When operators are omitted, the default operator is used-and
When the expression does not contain anyactionsBy default-print, i.e. print out all the searched files, separated by line breaks.
In fact, all three expressions can be regarded as options, indicating some restrictions on search (such as-maxdepthRepresents the maximum depth of the search path, or some test of the found target file (such as-readableDetermine whether it is readable, or take some action on the result (such as-print)。

option-name patternSearch 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 directory / root to find the matchfile?The file name of, and because it is not specifiedaction, so use the default-printPrint 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 to avoid confusion, wildcards after options need to be quoted.

option-maxdepth nSpecify the maximum depth of the search path

[root @ centos7 ~] ා find / root - maxdepth 1 - name "file?"; note the implicit operator - and between expressions
/root/file1
[[email protected] ~]#

In this case, a maximum depth of 1 is specified, which means that only the / root directory is searched without entering any of its subdirectories.
Corresponding to this option,-mindepthRepresents the minimum depth of the specified search path.

option-user nameFind files by file owner

[[email protected] ~]# find /root/temp -name "file?" -user learner
/root/temp/file1
/root/temp/file2
[[email protected] ~]# 

Or something like that-uid nIndicates the uid by file owner,-gid nIndicates the GID of the group to which the file belongs,-group nameIndicates by file group.

option-mtime nN * 24 hours since the last modification of the file

[[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
[root @ centos7 temp] ා find. - name "file1?" - Mtime + 5 days ago
./file13
[[email protected] temp]# 
[root @ centos7 temp] ා find. - name "file1?" - Mtime - 5 within five days
./file10
./file11
[[email protected] temp]#
[root @ centos7 temp] ා find. - name "file1?" - Mtime 5 ා just five days
./file12
[[email protected] temp]#

The command is used in this examplelsOptions-tSort the time of the file. The latest modified file is in front. option-mtime nN in can be expressed as:

+N is greater than n
-N is less than n
N equals n

There are other time (such as atime, CTime) comparisons, the same usage.

option-newer fileIndicates that the searched file is larger than the specifiedfileTo be 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 patternFile name matching pattern (wildcard):

[[email protected] temp]# find . -name "file1?" -path "./file1[13]"
./file11
./file13
[[email protected] temp]#

Be carefulpatternNo match/and.Carry out special treatment.

Typically – path will work with options-pruneUse 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-oOr, it’s the same as what we said before-andAll operators. Represents the logical relationship between expressions. In this case, it can be understood that if the directory matches./tmpThen execute-pruneSkip the directory, otherwise match-nameSpecified file and execute-print
In addition to these two operators, the!or-notLogical non, operator(...)Similar to parentheses in mathematical operations, it means to increase the priority:

[[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(...)Operators need to be escaped (to avoid being interpreted by the shell), precede the symbol with a backslash ‘\’. (we will elaborate on the escape or reference in the shell when we talk about bash programming)

option-type xRepresents a file of search type X, where the possible values of X arebcdpfls。 They and orderslsThe displayed file types are the same (see basic command introduction 1),fFor and on behalf of 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 modeFiles that represent 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]entos7 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
[[email protected] temp]#

option-size nIndicates search file size

[[email protected] temp]# find . -path "./*" -size +100c
./file10
./file13
[[email protected] temp]#

In this case+100cIt indicates the files larger than 100 bytes in the current directory. N is similar to the previous method (+ N, – N, n). The characters after n also include:

Block with B unit of 512 bytes (default unit when n is not followed by suffix)
k 1024 bytes
M 1048576 bytes
G 1073741824 bytes

option-print0Similar-printOutput file names, but do not separate them with any characters. Used when the filename contains special characters. Available with options-0OrdersxargsUse together (later).

option-exec command ;Indicates the command to execute
-execAfter that, you can further process the searched file with any shell command. Between command and semicolon, it is regarded as the parameter of command, where{}Represents the searched file. Semicolons need 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

-execThe command after the option startsfindThe command is executed once for each searched file in the directory, instead of listing all files after the command only once.
The following differences are illustrated:

#Command echo is executed only once
[[email protected] temp]# echo ./file11 ./file12 ./file13
./file11 ./file12 ./file13

#Command echo executed three times
[[email protected] temp]# find . -name "file1[1-3]" -exec echo {} \;
./file12
./file11
./file13
[[email protected] temp]#

When using format-exec command {} +This means that each file is appended to the command, so that the command is executed only once:

[[email protected] temp]# find . -name "file1[1-3]" -exec echo {} +
./file12 ./file11 ./file13
[[email protected] temp]#

But sometimes there are problems:

[[email protected] temp]# find . -name "file1[1-3]" -exec mv {} abcd/ +
Find: missing parameter for '- exec'
[[email protected] temp]#

Because here the file is appended to the directoryabcd/The result is 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 command line length.
Use-execThere may be a security vulnerability, usually using a pipeline and another commandxargsTo replace-execExecute the command.

2、xargsObtain the parameters of the command from the standard input and execute

xargsGet space delimited items from standard input and execute commands (default is / bin / echo)
option-0The separator of the item will be ignored with the option of find-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 filename contains spaces

When not in use:

[[email protected] temp]# find . -name "file*" | xargs ls
Ls: cannot access. / file: no file or directory
Ls: cannot access 14: no file or directory
./file10  ./file11  ./file12  ./file13

option-I stringSpecify an alternative string for the input project:

[[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 using-IThe following strings replace the input items so that they can be executed as a whole anywhere in the command. Also avoided.-exec command {} +Mistakes.

option-dSpecify 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-PSpecifies the maximum number of processes. The default number of processes is 1. Multiple processes execute concurrently.

3、datePrint or set system time

date [OPTION]... [+FORMAT]

When there is no parameter, the current time is displayed:

[[email protected] temp]# date
Tuesday, November 1, 2016 15:30:46 CST
[[email protected] temp]#

option-d stringDisplay time by description string (in the example, the string represents the number of seconds from zero 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

-dThe following string can also be:

[[email protected] temp]# date -d "-1 day"
Monday, October 31, 2016 16:11:27 CST

Yesterday

Next year:

[[email protected] temp]# date -d "1 year"
Wednesday, November 1, 2017 16:12:27 CST

option-sSet 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

Because the Linux system will read the CMOS to get the time when it starts, the system will write the system time to CMOS every other period of time. In order to avoid that the time is not written to CMOS due to the immediate restart of the system after the time is changed, the command is usually used after the time is setclock -wWrite system time to CMOS.
dateOutput format controlled by format in command, plus sign+Before format means format start:

[[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 represents year
%m means month.
%d means sky.
%H for hour
%M for minutes
%S indicates seconds.

There are many other formats that can 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 the output distance 1970-01-01 to the present time:

[[email protected] temp]# date +%s
1477987540
[[email protected] temp]#

For example, today’s day of the week:

[[email protected] temp]# date +%A
Tuesday
[[email protected] temp]#

Other formats, man

4、gzipCompress or decompress files

gzip [OPTION]... [FILE]...

When the command is followed directly 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 files to.gzAt the end, gzip does not keep the source file
option-dRepresents 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-rYou can recursively enter the directory and compress the files in it
option-nSpecifies 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, but the compression ratio is the largest. By default, n is 6.

[[email protected] temp]# gzip -r9 ./tmp

No file or file after gzip-Will be read from 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 the output of gzip in the example is redirected to a filehello.gzIf you unzip this file, a file will be generatedhello。 If the redirected file suffix is not.gz, the filename will not be decompressed until it is changed to the. GZ suffix.

5、zcatOutput compressed file contents to standard output

[roo[email protected] temp]# zcat hello.gz 
hello world
[[email protected] temp]#

Zcat reads the file compressed by gzip, only the file format is correct, and the file name does not need the suffix of. GZ.

6、bzip2Compress and decompress files

bzip2 [OPTION]... [FILE]...

commandbzip2andgzipSimilar are compression commands, but the compression algorithm used is different, usuallybzip2High compression. This command does not retain the source file by default. The default filename 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-kKeep 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-dIndicates decompression (error if source file exists):

[[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-fIndicates force overwrite of source file:

[[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-nandgzipConsistent usage, indicating compression ratio.

7、tarPack and compress files

tar [OPTION...] [FILE]...

commandgzipandbzip2No compression of directory is supported (although gzip can use the option – r to compress the directory, it still can’t compress the directory)tarThe 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
Drwx R-X 2 root 6 November 1 16:23 ABCD
-RWX r-x-x 1 root 12 Oct 28 17:24 test.sh
Drwxrxrxrx 2 root 425984 November 1 17:08 TMP
-Rw-r -- R -- 1 root 18001920 November 1 17:17 tmp.tar

Options in the example-cRepresents the creation of a package file,-f tmp.tarIndicates that the specified packing file name is tmp.tar, followed by the packed directory nametmp/

option-tList archived content
option-vDetailed list of processed files

[[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-uUpdate 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-xExtract the archive file. (Jie Bao)

[[email protected] temp]# rm -rf abcd/
[[email protected] temp]# tar -xvf abcd.tar 
abcd/
abcd/file10
abcd/file11
abcd/file12
abcd/file13
abcd/file15
[root @ centos7 temp] ා LS ABCD ා here is the completion result of pressing tab twice
abcd/     abcd.tar  
[[email protected] temp]#

option-OExtract files to standard output

[[email protected] temp]# tar -xf abcd.tar -O 
hello
hello
hello
hello
[root @ centos7 temp] × note that the contents of each archive are output here
[[email protected] temp]# tar -xf abcd.tar -O | xargs echo
hello hello hello hello
[[email protected] temp]#

option-pKeep file permissions (when unpacking).

option-j-J-zFor compression.
among-jUse commandbzip2-JUse commandxz-zUse commandgzipCompress and decompress the archive file (commandtarThe latter option 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 case, three compression formats are used for compression. You can see the commandbzip2Highest compression ratio, commandgzipThe compression ratio of is the lowest. Compression time is also an important factor to consider when performing compression. By default, usegzipThe fastest,xzThe slowest.
To decompress the compressed files of these three formats, just select the-cchange into-xYes.

option-X FILEExclude 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 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 DIRChange to directory dir (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
Drwxrxrxrx 2 root 425984 November 1 17:08 TMP

Extract only the specified file:

[[email protected] temp]# tar zxvf tmp.tar.gz -C abcd/ file1[23]
file12
file13
[[email protected] temp]# ls -l abcd
Total dosage 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 selected in the-Cbefore

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 paper describes the commands and some options related to file search and archive compression in Linux, which are often used in the process of system management. Use it skillfully.