Linux directory structure and methods of viewing, counting, and filtering files, and how to use compression tools

One, Linux directory structure

The directory and file data in the Linux operating system are organized into a tree-shaped directory structure, and all partitions, directories, files, etc. have the same starting point-the root directory.

Insert picture description here
When locating files or directories, the Linux operating system uses slashes "/" to separate them. In the entire tree-shaped directory structure, an independent "/" is used to indicate the root directory. The root directory is the starting point of Linux operating the file system, and the partition where it is located is called the root partition. Under the root directory, the Linux operating system will create some special subdirectories by default, each with different purposes.

  • /boot: This directory is the directory where the system kernel is stored, and it is also the directory where the files needed during system startup, such as vmlinuz and initrd.img. When installing the Linux operating system, create a partition for the boot directory, which is helpful to back up the system.
  • /bin: bin is short for binary. This directory stores commands that are executable and frequently used by all users, such as cp, ls, etc.
  • /dev: This directory saves interface device files, such as /dev/hda1, /dev/cdrom, etc.
  • /etc: Almost all the main configuration files of the system are placed in this directory, such as personnel account and password files, starting files of various services, etc.
  • /home: Stores the default working folders (ie, home directory, home directory) of all ordinary system users. For example, the home directory corresponding to the user account "teacher" is located in "/home/teacher/". If the server needs to be provided to a large number of ordinary users, it is recommended to divide the "/home" directory into separate partitions to facilitate user data backup.
  • /root: This directory is the home directory of the Linux operating system administrator (super user) root. By default, only the home directory of the root user is in the root directory instead of the "/home" directory.
  • /sbin: Store the most basic management commands in the Linux operating system, and only general administrator users have the authority to execute them.
  • /usr: Store other user applications, usually divided into many subdirectories for storing different types of applications.
  • /var: Stores some files that often need to be changed in the system, such as system log files, user mailbox directories, etc. In actual application systems, the "/var" directory is usually divided into independent partitions.

2. Methods to view, count and filter files

1. View the usage of files (cat, more, less, head, tail)

1) cat command-display and concatenate the contents of the file

  • The cat command was originally used to concatenate the contents of multiple files, but in actual use it is more used to view the contents of files.
  • The cat command is the most widely used file content viewing command. When using this command, you only need to use the file path to be viewed as a parameter.
  • The cat command can view the contents of multiple files at the same time, just add multiple file paths.
  • The cat command cannot turn pages, but can only see the information displayed on the screen. It is mostly used to view small files.
[root@www ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@www ~]# cat /etc/redhat-release /proc/version 
//产看/etc/redhat-release、/proc/version 文件的内容,
//前者记录了 CentOS 系统的发行版本信息,后者记录了系统内核及开发环境、时间等信息。
CentOS Linux release 7.4.1708 (Core) 
Linux version 3.10.0-693.el7.x86_64 ([email protected]) 
(gcc version 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) ) #1 SMP Tue Aug 22 21:09:27 UTC 2017

2) More command-view the contents of the file by page.
The cat command can be used to display the contents of the entire file very simply, but when there are too many contents in the file, it is likely that only the last part of the information can be seen, and most of the contents in front of the file are Too late to see. The more and less commands display files in full-screen pages, so that we can read the contents of the file carefully from beginning to end.
When you use the more command to view the content of a file that exceeds one screen, it will be displayed on a split screen, and the percentage of the current content in the entire file will be displayed in the lower left corner.
Use the more command to view the usage of the file :
more file path

  • Enter key: scroll down line by line
  • Space bar: scroll down one screen
  • b key: scroll up one screen
  • q key: exit
  • / Key: find a field
  • n key: the position of the next occurrence
  • N key: the position of the last occurrence
    3) Less command-view file content
    by page. Using less is basically the same as more.
    Use the less command to view the usage of the file :
    less file path
  • Page Up: key to scroll up one screen
  • Page Down key: scroll down one screen
  • q key: exit
  • / Key: find a field
  • n key: the position of the next occurrence
  • N key: the position of the last occurrence
    4) head-view part of the content at the beginning of the file
    head and tail are a pair of commands with opposite functions. The former is used to display part of the content at the beginning of the file, and the latter is used to display part of the content at the end of the file . You can use the "-n" option (n is the specific number of lines) to specify how many lines of content need to be displayed. If the number of lines is not specified, only ten lines are displayed by default .
    Syntax format of head command
    head -n file path

5) tail-view part of the content
at the end of the file The tail command is the opposite, used to view the content at the end of the file. The tail command is usually used to view system logs (because newer log records are always added to the end of the file) in order to observe network access, service debugging and other related information.
Syntax format of tail command
tail -n file path

  • -f: When the option is used, you can also track the dynamic update of the content of the file tail, which is convenient for real-time monitoring of changes in the file content. (Press Ctrl + C to terminate the real-time viewing)

2. Contents of statistical files

wc command-count the number of words (Word Count), the number of lines and other information in the content of the file.
The syntax format of the
wc command wc option file path
Common options:

  • -l: count the number of lines in the file content.
  • -w: count the number of words in the file content (separated by spaces or tab stops).
  • -c: Count the number of bytes in the file content.

3. Filter file content

grep command-filter file content
grep command syntax format
grep [options]... Search condition target file
Common options:

  • -i: Ignore case when searching for content (Ignore Case).
  • -v: Invert search (Invert), that is, output lines that do not match the search conditions.

Third, the use of compression tools

1. Compress and decompress with gzip and gunzip commands

The default extension of compressed files made with gzip is ".gz".
gzip command-
the syntax format of the gzip command to compress files gzip
[Options] File path
Common options:

  • -9: The compression ratio can be increased, but larger files will take more time.
  • -d: Decompress files compressed by gzip

gunzip command-decompress files
the syntax format of gunzip command
gunzip file path

2, bzip2 and bunzip2 command compression and decompression

The default extension of compressed files made with bzip2 is ".bz2".
bzip2 command-compressed file
bzip2 command syntax format
bzip2 [options] file path
Common options:

  • -9: The compression ratio can be increased, but larger files will take more time.
  • -d: Decompress files compressed by gzip

bunzip2 command-decompress file
bunzip2 command syntax format
bunzip2 file path

3. Use the tar archive and release tool

The tar command is mainly used to archive directories and files. In actual backup work, the package file is usually compressed at the same time as the archive (you need to call the previous gzip or bzip2 command) to save disk space. When using the tar command, the "-" sign before the option can be omitted.

1) tar command-make archive backup files

The syntax format of the tar command to make archive backup files
tar [option]… The source file or directory to be archived for the archive and compressed file name…

2) tar command-restore data from archive files

The syntax format of the tar command to restore data from the archive file is
tar [Option]… Archive and compressed file name [-C target directory]
Common options:

  • -c: Create a package file in .tar format.
  • -C: Specify the target folder to be released during decompression.
  • -f: Indicates the use of archive files.
  • -j: Call the bzip2 program to compress or decompress.
  • -p: Keep file and directory permissions when packaging.
  • -P: Keep the absolute path of files and directories when packaging.
  • -t: List the files in the package.
  • -v: output detailed information (Verbose).
  • -x: Unzip the package file in .tar format.
  • -z: call gzip program to compress or decompress.

Guess you like

Origin blog.csdn.net/wulimingde/article/details/109286480