07_Linux directory file operation command 4 decompression, file search_My Linux road

 

This section is still the same to learn the commands for operating directory files

In this section, I'll talk about decompression tar and zip commands, and the text search command grep

 

tar package compression command

tar command can create archives for linux files and directories

First of all, two concepts must be clarified: packing and compression. Packing refers to turning a large number of files or directories into a total file; compression refers to turning a large file into a small file through some compression algorithms.

Why distinguish these two concepts? This is due to the fact that many compression programs in Linux can only compress one file, so when you want to compress a large number of files, you have to first package the large number of files into a package (tar command), and then use Compressor to compress (combined with gzip, bzip2 commands).

tar (options) (arguments)

  

His main options are as follows

-A or --catenate: add files to existing backup files;
-B: Set the block size;
-c or --create: create a new backup file;
-C <directory>: This option is used for decompression. To decompress in a specific directory, you can use this option.
-d: difference between log files;
-x or --extract or --get: restore files from backup files;
-t or --list: List the contents of the backup file;
-z or --gzip or --ungzip: process backup files via gzip command;
-Z or --compress or --uncompress: Process backup files through the compress command;
-f <backup file> or --file=<backup file>: specify the backup file;
-v or --verbose: Display the instruction execution process;
-r: add files to already compressed files;
-u: add changed and existing files to an existing archive;
-j: support bzip2 decompression files;
-v: display the operation process;
-l: file system boundary settings;
-k: keep the original file without overwriting;
-m: keep the file from being overwritten;
-w: Confirm the correctness of the compressed file;
-p or --same-permissions: restore the file with the original file permissions;
-P or --absolute-names: The file name uses an absolute name without removing the "/" sign before the file name;
-N <date format> or --newer=<date time>: only save files newer than the specified date to the backup file;
--exclude=<template style>: Exclude files matching the template style.

  

For example, we package a file

tar -cvf log.tar somelogs.log     

  

Note that this is only packaging, not compression

 

After packaging, compress with gzip

tar -zcvf log.tar.gz somelogs.log

  

After packaging, compress with bzip2

tar -jcvf log.tar.bz2 somelogs.log

  

Note that the first parameter is the file name, which can be defined by yourself, but according to common sense, it needs to end with a specific suffix, which makes it easier to distinguish

 

Check what files are in the tar package

tar -ztvf log.tar.gz

  

Unzip the tar file

tar -zxvf /opt/soft/test/log.tar.gz

  

Extract only some of the files in the tar

tar -zxvf /opt/soft/test/log30.tar.gz log2013.log

  We can view the file name in the compressed package with the following command

tar -ztvf

  

In fact, our most commonly used commands are compression and decompression

Compression: tar -jcv -f filename.tar.bz2 The name of the file or directory to be compressed
Query: tar -jtv -f filename.tar.bz2
Decompression: tar -jxv -f filename.tar.bz2 -C The directory to be decompressed

  

 

zip unzip command

The zip command can be used to unzip or pack files. zip is a widely used compression program that compresses files to additionally produce compressed files with a ".zip" extension.

zip(options)(parameters)

  

All options are as follows

-A: Adjust executable automatic decompression files;
-b<working directory>: Specify the directory where files are temporarily stored;
-c: add a comment to each compressed file;
-d: delete the specified file from the compressed file;
-D: Do not create a directory name in the compressed file;
-f: The effect of this parameter is similar to specifying the "-u" parameter, but not only updates existing files, but if some files do not originally exist in the compressed file, using this parameter will add them to the compressed file together;
-F: Attempt to repair corrupted compressed files;
-g: Append the compressed file to the existing compressed file instead of creating a new compressed file;
-h: online help;
-i <template style>: only compress files that meet the conditions;
-j: save only the file name and its content, not any directory name;
-J: delete unnecessary data in front of the compressed file;
-k: use the file name in MS-DOS compatible format;
-l: When compressing files, replace LF characters with LF+CR characters;
-ll: When compressing files, replace LF+cp characters with LF characters;
-L: Display copyright information;
-m: After compressing the file and adding it to the compressed file, delete the original file, that is, move the file to the compressed file;
-n <suffix string>: do not compress files with a specific suffix string;
-o: The file with the latest change time in the compressed file shall prevail, and the change time of the compressed file is set to be the same as the file;
-q: Do not display the instruction execution process;
-r: Recursive processing, processing all files and subdirectories in the specified directory together;
-S: include system and hidden files;
-t <date time>: Set the date of the compressed file to the specified date;
-T: Check that each file in the backup file is correct;
-u: replace the newer file into the compressed file;
-v: Display instruction execution process or display version information;
-V: Save the file attributes of the VMS operating system;
-w: If the version number is in the file name, this parameter is only valid under the VMS operating system;
-x <template style>: Exclude eligible files when compressing;
-X: do not save extra file attributes;
-y: directly save the symbolic link, not the file pointed to by the link, this parameter is only valid under systems such as UNIX;
-z: add a comment to the compressed file;
-$: Save the volume name of the disk where the first compressed file is located;
-<compression efficiency>: The compression efficiency is a value between 1 and 9.

  

Let's familiarize yourself with this command with a few examples

Package all files and folders in the /home/susmtoe/python/ directory as python.zip in the current directory:

zip -q -r python.zip /home/susmote/python

  

 

 

grep text search command

grep (global search regular expression(RE) and print out the line) is a powerful text search tool that uses regular expressions to search text and print out the matching lines come out.

 

His options are as follows

-a Do not ignore binary data.
-A <display_columns> In addition to showing the line that conforms to the template style, and display the content after that line.
-b Displays the contents of the line before the line that matches the template style.
-c Count the number of columns conforming to the template style.
-C <display_columns> or -<display_columns> In addition to displaying the column that conforms to the template style, and display the content before and after the column.
-d <action> This parameter must be used when specifying that the directory to be searched is not a file, otherwise the grep command will report information and stop the action.
-e <template style> Specifies the string as the template style to look for the contents of the file.
-E Use the template style as extended normal notation, which means that extended regular expressions can be used.
-f <template file> Specify a template file, whose content has one or more template styles, let grep find the file content that meets the template conditions, and the format is the template style of each column.
-F Treat template styles as a list of fixed strings.
-G Use template styles as normal notation.
-h Do not indicate the name of the file to which the column belongs before displaying the column that conforms to the template style.
-H Before displaying the row that conforms to the template style, mark the file name of the row.
-i Ignore differences in character case.
-l List file names whose contents conform to the specified template style.
-L List file names whose contents do not conform to the specified template style.
-n Indicates the column number before displaying the column that conforms to the template style.
-q Do not display any information.
-R/-r This parameter has the same effect as specifying the "-d recurse" parameter.
-s Do not display error messages.
-v Reverse lookup.
-w Show only whole-word columns.
-x Show only columns that match all columns.
-y This parameter has the same effect as "-i".
-o Output only the matched part of the file.

  

Let's familiarize yourself with the grep command through a few examples

Search the file for a word, and the command returns a text line containing "match_pattern":

grep match_pattern file_name
grep "match_pattern" file_name

  

It is also possible to find in multiple files at the same time

grep "match_pattern" file_1 file_2 file_3 ...

  

Output all lines except -v option:

grep -v "match_pattern" file_name

  

Marker matching color --color=auto option:

grep "match_pattern" file_name --color=auto

  

Use the regex -E option:

grep -E "[1-9]+"
or
egrep "[1-9]+"

  Note that the egrep command is similar to grep -E

 

Only output the part of the -o option that matches in the file :

echo this is a test line. | grep -o -E "[a-z]+\."
line.

echo this is a test line. | egrep -o "[a-z]+\."
line.

  Note that here we use a joint command, connecting two commands through a " | ", note that the order is executed from left to right

  The echo command represents the output command, which is equivalent to the print in programming

 

Count the number of lines in a file or text that contain matching strings -c option:

grep -c "text" file_name

  

Output the number of lines containing the matched string with the -n option:

grep "text" -n file_name
or
cat file_name | grep "text" -n

#Multiple files
grep "text" -n file_1 file_2

  

The character or byte offset at which the print style match is located:

echo gun is not unix | grep -b -o "not"
7:not

# The character cheapness of a string in a line is counted from the first character of the line, with a starting value of 0. Options -b -o are generally always used together.

  

Search multiple files and find in which files the matching text is:

grep -l "text" file1 file2 file3...

  

grep can also be used to recursively search files

Recursive search for text in multiple levels of directories:

grep "text" . -r -n

  

Ignore character case in matching patterns:

echo "hello world" | grep -i "HELLO"
hello

  

Option -e brakes multiple matching styles:

echo this is a text line | grep -e "is" -e "line" -o
is
line

#You can also use the -f option to match multiple styles, and write out the characters that need to be matched line by line in the style file.
cat patfile
aaa
bbb

echo aaa bbb ccc ddd eee | grep -f patfile -o

  

Include or exclude specified files from grep search results:

#Search recursively for the character "main()" only in all .php and .html files in the directory
grep "main()" . -r --include *.{php,html}

#Exclude all README files from search results
grep "main()" . -r --exclude "README"

#Exclude files in the filelist file list from the search results
grep "main()" . -r --exclude-from filelist

  

grep and xargs with 0-valued byte suffix:

#Test file:
echo "aaa" > file1
echo "bbb" > file2
echo "aaa" > file3

grep "aaa" file* -lZ | xargs -0 rm

#file1 and file3 will be deleted after execution, grep output uses the -Z option to specify the filename with a 0-value byte as a terminator (\0), xargs -0 reads the input and separates the filename with a 0-value byte terminator, then Delete matching files, -Z is usually used in conjunction with -l.

  

grep silently outputs:

grep -q "test" filename

#No information will be output. If the command runs successfully, it returns 0, and if it fails, it returns a non-zero value. Generally used for conditional testing.

  

Print out the lines before or after the matching text:

#Display 3 lines after matching a result, use the -A option:
seq 10 | grep "5" -A 3
5
6
7
8

#Display 3 lines before matching a result, use the -B option:
seq 10 | grep "5" -B 3
2
3
4
5

#Display the first three lines and the last three lines matching a result, use the -C option:
seq 10 | grep "5" -C 3
2
3
4
5
6
7
8

#If there are multiple matching results, "--" will be used as the separator between the matching results:
echo -e "a\nb\nc\na\nb\nc" | grep a -A 1
a
b
--
a
b

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324886189&siteId=291194637