Linux file search and other basic commands (3)

Time:2021-4-2

1. Linux contains a large number of files, for file search, Linux provides the find command.

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

amongexpressionThere are three types: options, tests and actions. Multiple expressions are separated by operators. When the operator is omitted, the default operator – and is used.
When the expression does not contain any actions, – print is used by default,
That is to say, print out all the searched files and separate them with newline.
In fact, the three expressions can be regarded as options, representing some restrictions on the search (for example, – maxdepth represents the maximum depth of the search path), some tests on the found target file (for example, – readable to judge whether it is readable), or some actions on the results (for example, – print).

Name – pattern 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, all the files in the directory / root are searched to find the file name that matches the file? And since no action is specified, the default – print is used to print the result. In the find command, the search path and some file names can be represented by shell wildcards (see the previous article), but to avoid confusion, the wildcards after the options need to be quoted.

option-maxdepth nSpecify 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 case, the 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, – mindepth represents the minimum depth of the specified search path.

option-user nameTo find a file by its owner:


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

Or something like that-uid nUid by file owner, – GID n by file group, and – group name by file group.

option-mtime n The last time the content of the file was modified is now n * 24 hours:

[[email protected] temp]# ls -lt file1?
-Rw-r -- R -- 1 root 64 Oct 27 15:06 file11
-Rw-r -- R -- 1 root 132 Oct 27 13:28 file10
-Rw-r -- R -- 1 root 22 Oct 26 21:31 file12
-Rw-r -- R -- 1 root 137 October 12 16:42 file13
[ [email protected]  Temp] # find. - name "file1?" - Mtime + 5 # five days ago
./file13
[[email protected] temp]# 
[ [email protected]  Temp] # find. - name "file1?" - Mtime - 5 # within five days
./file10
./file11
[[email protected] temp]#
[ [email protected]  [temp] # find. - name "file1?" - Mtime 5 # just five days
./file12
[[email protected] temp]#

In this example, the option – t of the command LS is used to sort the time of the files, and the most recently modified file comes first. In option mtime n, n can be expressed as:

+N means greater than n
-N means less than n
N is equal to n

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

option-newer fileIndicates that the searched file is’ new ‘than the specified file (the last time the content was modified is shorter than the present time)


[[email protected] temp]# find . -name "file1?" -newer file12
./file10
./file11
[[email protected] temp]# 

Option – path pattern file name matching pattern (wildcard)


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

Note that there is no special treatment for / and. In pattern matching.

Usually – path is used with the option – prune 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]#

Here – O means or, it and – and are both operators. 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, the operator! Or – not represents logical not, and the operator (…) is similar to the bracket in mathematical operation, which 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]#

Note that the (…) operator here needs to be escaped (to avoid being interpreted by the shell as something else), with a backslash ‘\’ before the symbol. (about escape or reference in shell, we will explain it in detail when we talk about bash programming.)

option-type xRepresents a file of search type X, where the possible values of X include B, C, D, P, F, l, S. They are consistent with the file type displayed by the command LS (see basic command introduction 1), and f stands for ordinary file.

[[email protected] temp]# ln -s file13 file14
[[email protected] temp]# ls -l file14
Lrwxrwxrwx 1 root 6 November 12:29 file14 - > file13
[[email protected] temp]# find . -type l
./file14
[[email protected] temp]#

option-perm modeFiles that represent search specific permissions:

[[email protected] temp]# chmod 777 file14
[[email protected] temp]# ls -l file1[3-4]
-Rwxrwxrwx 1 root 137 Oct 12 16:42 file13
Lrwxrwxrwx 1 root 6 November 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]#

The – size n option indicates the size of the search file


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

In this example, + 100C refers to the files larger than 100 bytes in the current directory, n is similar to the previous way of expressing time (+ N, – N, n), and the characters after n also include:

B block in 512 bytes (the default unit when there is no suffix after n)
k 1024 bytes
M 1048576 bytes
G 1073741824 bytes
The option – print0 is similar to – print output file names, but no characters are used to separate them. Used when a file name contains special characters. It can be used with the command xargs with option – 0 (described later).

option-exec command ;Represents the command to execute
-After exec, you can follow any shell command to further process the searched file. Between command and semicolon, it is regarded as the parameter of command, and {} is used to represent the searched file. Semicolons need to be escaped.
For example, execute the command LS – L on the searched file:

[[email protected] temp]# find . -name "file*" -exec ls -l {} \;
-Rw-r -- R -- 1 root 132 Oct 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 Oct 12 16:42. / file13
Lrwxrwxrwx 1 root 6 November 12:29. / file14 - > file13

-execThe command after the option is startedfindThe command is executed once for every file searched, instead of listing all files after the command only once.
Here are some examples to illustrate the differences:

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

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

When the format – exec command {} + is used, it 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 of "- exec"
[[email protected] temp]#

Because the file is appended to the directory ABCD /, an error is reported.
At the same time, using the format – exec command {} + may cause too many files to be appended, which exceeds the operating system’s limit on the length of the command line.
Using – exec may have a security vulnerability. Usually, pipeline and another command xargs are used to execute the command instead of – exec.

2. Xargs takes the parameters of the command from standard input and executes it
Xargs takes items separated by spaces from standard input and executes the command (default is / bin / echo)
Option – 0 will ignore the separator of the project, and cooperate with the option – print0 of find to process the files with special symbols.

[[email protected] temp]# find . -name "file*" -print0 | xargs -0 ls -l
-Rw-r -- R -- 1 root 132 Oct 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 Oct 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: can't access. / file: no file or directory
Ls: can't access 14: don't have that file or directory
./file10 ./file11 ./file12 ./file13

chooseterm-I stringSpecify 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 the string after – I instead of the input items, so that they can be executed as a whole in any position of the command. It also avoids the error of – exec command {} +.

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 – P specifies the maximum number of processes. The default number of processes is 1. Multiple processes execute concurrently.

3. Date to print or set the system time

date [OPTION]… [+FORMAT]
When there are no parameters, the current time is displayed

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

Option – D string displays 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

-The string after D can also be:

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

It means yesterday

Another example is next year

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

Option – s set system time:

[[email protected] temp]# date -s "2016-11-01 15:49"
Tuesday, November 01, 2016 15:49:00 CST
[[email protected] temp]# date
Tuesday, November 01, 2016 15:49:03 CST

Since the Linux system will read 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 changing the time, the command clock – W is usually used to write the system time to CMOS after setting the time.
In the date command, format controls the output format. The plus sign + indicates the beginning of the format before the 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 is the year
%M is the month
%D means day
%H is the hour
%M is the minute
%S is the second
You can also specify many other formats, such as output current time only:


[[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]#

What day of the week is it today

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

For other formats, please make your own choice

4. Gzip compresses or decompresses the file

gzip [OPTION]… [FILE]…

When the command is followed by a file directly, it means to compress the file

[[email protected] temp]# ls -l file1*
-Rw-r -- R -- 1 root 132 Oct 27 13:28 file10
-Rw-r -- R -- 1 root 64 Oct 27 15:06 file11
-Rw-r -- R -- 1 root 22 Oct 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 Oct 27 13:28 file10.gz
-Rw-r -- R -- 1 root 49 Oct 27 15:06 file11.gz
-Rw-r -- R -- 1 root 44 Oct 26 21:31 file12.gz
-Rw-r -- R -- 1 root 109 Oct 12 16:42 file13.gz

The compressed file ends with. Gzip. 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 Oct 27 13:28 file10
-Rw-r -- R -- 1 root 64 Oct 27 15:06 file11
-Rw-r -- R -- 1 root 22 Oct 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 to 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

When there is no file after gzip or the file is -, it 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 16:40, Nov hello.gz

Notice that the output of gzip in the example is redirected to a file hello.gz If you unzip this file, 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. GZ.

5. Zcat outputs the compressed file content to standard output


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

Zcat read gzip compressed file, only the file format is correct, do not need the file name with. GZ suffix.

6. Bzip2 compression and decompression file

bzip2 [OPTION]… [FILE]…
The command bzip2 is similar to gzip, but the compression algorithm used is different. Generally, the compression of bzip2 is higher. This command does not reserve the source file by default. 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 Oct 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 Oct 27 13:28 file10
-Rw-r -- R -- 1 root 96 Oct 27 13:28 file10.bz2

option-dIndicates decompression (if there is a source file, an error will be 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 Oct 27 15:06 file11

option-fRepresents a forced override source file:

[[email protected] temp]# bzip2 -d -f file10.bz2
[[email protected] temp]# ls -l file10*
-Rw-r -- R -- 1 root 132 Oct 27 13:28 file10

Option – n is the same as gzip, which means compression ratio.

7. Tar package compressed file

tar [OPTION…] [FILE]…
The commands gzip and bzip2 do not support compressing the directory (although gzip can use the option – r to compress the directory, it is still unable to compress the directory). You can archive the directory with the tar command, 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 Oct 28 17:24 test.sh
Drwxr-xr-x 2 root 425984 November 1 17:08 TMP
-Rw-r -- R -- 1 root 18001920 tmp.tar

In the example, option – C means to create a package file, – F tmp.tar Indicates that the specified packaging file name is tmp.tar , followed by the packaged directory name TMP /.

Option – t list archived content
Option – V lists the processed files 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 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 extracts the archive file. (unpacking)

[r[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]  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]  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 preserves file permissions (for unpacking).

Options – J, – J, – Z are used for compression.
Where – J uses the command bzip2, – J uses the command XZ, – Z uses the command gzip to compress and decompress the archive file respectively (the option after the command tar 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, we can see that the compression ratio of bgzip is the highest. In the implementation of compressed files, compression time is also an important factor we consider. By default, gzip is the fastest and XZ is the slowest.
For these three formats of compressed files to decompress, just replace – C with – x in the options.

Option – x file excludes 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 dir change to directory dir (for 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
-Rw-r -- R -- 1 root 6 November 2 10:02 file10
-Rw-r -- R -- 1 root
-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

Unzip 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 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 precede option – C

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://www.jb51.net/article/101263.htm"
[[email protected] temp]# 

This paper describes the Linux about file search and archive compression and other related commands and some options usage, are often used in the process of system management, need to be skilled in using.

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.