Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

2.8 Package compression and search commands


May 23, 2021 That's what Linux should learn



On the network, people are more and more inclined to transfer compressed files because of the small size of compressed files, in the same speed, the transfer time is short. H ere's how to package and compress and unzip files in a Linux system, and let users search for matching information in text files based on keywords, and search for specific files based on specified names or attributes throughout the file system. Although there are only 3 commands in this section, they are complex in function and have many parameters, so they are explained at the end of this chapter.

1. tar command

The tar command is used to package, compress, or unzip a file in the form of "tar (options) .

In Linux systems, there are many common file formats, the main ones are .tar or .tar.gz or .tar.bz2 formats, we do not have to worry about too many formats to remember, in fact, most of these formats are generated by tar commands. M r. Liu wei will explain the most important parameters for everyone to understand. The parameters of the tar command and their function are shown in Table 2-14.

The parameters of the Table 2-14 tar command and their role

Parameter Role -c Create a compressed file -x Unzip a compressed file -t See what files are inside the compression package -z Compress or unzip with Gzip -j Use bzip2 to compress or unzip -v Show the process of compression or decompression -f Target file name -p Retains the original permissions and properties -P uses absolute paths to compress -C specifies the decompression to the directory First, -c parameters are used to extract the compressed file, -x parameters for the decompression file, so the two parameters cannot be used at the same time. S econd, the -z parameter specifies the use of Gzip format to compress or unzip files, and the -j parameter specifies the use of bzip2 format to compress or unzip files. W hen used by the user, the suffix of the file determines which format parameters should be used for decompression. W hen performing some compression or decompression operations, it can take several hours, and if the screen has not been output, you can't tell the progress of the package on the one hand, and you'll suspect that the computer is crashing on the other, so it's highly recommended to use the -v parameter to continuously show the user the process of compression or decompression. T he -C parameter specifies which specified directory to unziw to. T he -f parameter is particularly important because it must be placed at the last bit of the parameter, representing the name of the package to be compressed or decompressed. M r. Liu typically uses the "tar -czvf package name.tar.gz.tar.gz direction to be packaged" command to package and compress the specified files; L et's demonstrate the packaging compression and decompression operations one by one. Start by using the tar command to package and compress the /etc directory in gzip format and name the file .tar.gz:

[root@linuxprobe ~]# tar czvf etc.tar.gz /etc tar: Removing leading `/' from member names /etc/ /etc/fstab /etc/crypttab /etc/mtab /etc/fonts/ /etc/fonts/conf.d/ /etc/fonts/conf.d/65-0-madan.conf /etc/fonts/conf.d/59-liberation-sans.conf /etc/fonts/conf.d/90-ttf-arphic-uming-embolden.conf /etc/fonts/conf.d/59-liberation-mono.conf /etc/fonts/conf.d/66- s il-nuosu.conf .................. O mit part of the compression process information... Next, the packaged package file is specified to be unzipped into the /root/etc directory (first use the mkdir command to create the /root/etc directory):

[root@linuxprobe ~]# mkdir /root/etc [root@linuxprobe ~]# tar xzvf etc.tar.gz -C /root/etc etc/ etc/fstab etc/crypttab etc/mtab etc/fonts/ etc/fonts/conf.d/ etc/fonts/conf.d/65-0-madan.conf etc/fonts/conf.d/59-liberation-sans.conf etc/fonts/conf.d/90-ttf-arphic-uming-embolden.conf etc/fonts/conf.d/59-liberation-mono.conf etc/fonts/conf.d/66- s il-nuosu.conf etc/fonts/conf.d/65-1-vlgothic-gothic.conf etc/fonts/conf.d/65-0-lohit-bengali.conf etc/fonts/conf.d/20-unhint-small-dejavu-sans.conf .................. O mitting part of the decompression process information... 2 Grep command

The grep command is used to perform a keyword search in the text and displays the matching results in the format "grep (options) ( files) " . The parameters of the grep command and their function are shown in Table 2-15.

Table 2-15 the parameters of the grep command and its role

Parameter Action -b Searches for executables (text) -c shows only the number of lines found -i ignores case -n shows line numbers -v reverse selection - lists only lines that do not have "keywords". T he grep command is the most versatile text search matching tool, and although there are many parameters, most are largely unavailable. A fter summing up nearly 10 years of experience in operation and operation work and training and teaching, Mr. Liu Wei put forward the idea of writing this book, "removing the unr practical" is definitely not the mouth of the river. I f the level of an IT training instructor can only stay at the "technical porter" level, but not to refine and summarize high-quality technical knowledge, it is not good for his students. H ere we will talk about only two of the most commonly used parameters: the -n parameter, which is used to display the line number of the search-to-information, and the -v parameter, which is used for anti-selection information (i.e., all lines of information that do not contain keywords). These two parameters will do almost 80% of your future work needs, and for hundreds of others, it's time to query with the man grep command even if you encounter them later during work.

In Linux, the /etc/passwd file holds all user information, and once the user's login terminal is set to /sbin/nologin, the login system is no longer allowed, so you can use the grep command to find all user information in the current system that is not allowed to log on to the system:

[root@linuxprobe ~]# grep /sbin/nologin /etc/passwd bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin .................. O mit part of the output process information... 3 Find command

The find command is used to find a file according to the specified criteria, in the format "Find (Find Path) Find Condition Action".

"Everything in the Linux system is a file" has been mentioned many times in this book, and the next step is to witness the weight of this sentence. I n Linux systems, search is usually done with the find command, which can use different file characteristics as a look-for condition (such as file name, size, modification time, permissions, etc.) and display the information on the screen by default if the match is successful. The parameters of the find command and its function are shown in Table 2-16.

The parameters and effects in the Find command in Table 2-16

Parameter Action -name match name -perm match permission (mode is exact match, -mode is included can) -user match owner -group matches all groups -mtime -n sn match the time of modification (-n refers to n days, sn refers to n days ago) - atime -n sn match access file time (-n refers to n days, sn refers to n days ago) - c -n -n refers to n days ago) -nouser matches files without owners -nogroup matches files without all groups -newer f1!f2 matches files that are newer than file f1 but older than f2 --type b/d/c/p/l/f match file type (the following subtitle letters in turn represent block devices, directories, character devices, pipelines, linked files, text files) - size matches the size of files that are more than 50KB. A nd -50KB for finding files less than 50KB) -prune ignores a directory -exec ... { }\; T he commands that can be followed for further processing of search results (as shown below) need to focus on the important role of the -exec parameter. This parameter is used to further process the results of the find command search by the immediate command, which is very similar to the pipe character technique to be explained in Chapter 3, and because of the special requirements of the find command for parameters, exec is a long format, but still requires only a minus sign (-).

According to the Fileystem Hierarchy Standard protocol, profiles in Linux systems are saved to the /etc directory (see Chapter 6 for details). If you want to get a list of all the files in the directory that start with host, you can do the following:

find root@linuxprobe /etc -name "host" -print /etc/avahi/hosts/etc/host.conf/hosts/etc/hosts.allow/etc/hosts.deny/etc/sel If you want to search throughout the system for all files with SUID permissions (see Chapter 5), just use -4000:

[root@linuxprobe ~]# find / -perm -4000 -print /usr/bin/fusermount /usr/bin/su /usr/bin/umount /usr/bin/passwd /usr/sbin/userhelper /usr/sbin/usernetctl .................. O mit some of the output information... Advanced experiment: Find all files belonging to linuxprobe users throughout the file system and copy them to the /root/findresults directory.

The focus of the experiment is "-exec" . " Parameters, in which the "" indicates that the find command searches out of each file, and the end of the command must be ";" ” The specific commands for completing the experiment are as follows:

[root@linuxprobe ~]# find / -user linuxprobe -exec cp -a {} /root/findresults/ \;

At the end of this chapter, Mr. Liu yu said a few more words: Many readers are worried about learning Linux system because of their poor English foundation, but we don't have to worry, because our books, training courses and even Red Hat questions are Chinese. A nd after learning this chapter, you must have found that we will use Linux commands in the future, and not pure English words, even if their spelling is 100% the same, the final use will certainly be different. S o in terms of learning Linux system technology, you and the English people are definitely standing on the same running line, not to mention the correct choice of a Linux textbook for you. Take a break and start learning Chapter 3!