Linux file management command: rmdir \ chattr \ cksum \ cmp \ split

rmdir: delete directory

  • Function: delete one or more directories.
  • Usage: rmdir [options] ...directory...
  • The main options are as follows:
Order describe
--ignore-fail-on-non-empty ignore any errors due to directories still having data
-p, --parents After deleting the specified directory, if the upper directory of the directory has become an empty directory, delete it together
-v, --verbose Show command execution process
--help show this help message and exit
--version Print version information and exit.

Description If the directory has no data, delete the directory. The directory deleted by rmdir is empty in each level directory, and there are no other files. If there are files in any first-level directory, they cannot be deleted.

case study

When using the -p parameter to delete directories, it is done step by step.

mkdir -p /sirdoc/redhat/ 

Note First create a directory sirdoc under the current directory, and there is a subdirectory redhat in sirdoc.

# rmdir -p sirdoc/redhat/ 

chattr: change the attributes of a file

  • Function: Prevent key files from being modified.
  • Usage: chattr [options] file or directory
  • The main options are as follows:
Order describe
-R Process all files and subdirectories recursively.
-V Display the modified content in detail and print the output.
- failure attribute.
+ Activate properties.
= Specify properties.
A Atime, tells the system not to modify the last access time of this file.
S Sync, once the application program writes to this file, the system immediately writes the modified result to the disk.
a Append Only, the system only allows data to be appended after this file, and no process is allowed to overwrite or truncate this file. If the directory has this attribute, the system will only allow the creation and modification of files in this directory, and will not allow any files to be deleted.
i Immutable, the system does not allow any modification of this file. If the directory has this attribute, any process can only modify the files in the directory, and is not allowed to create and delete files.
D Checks for errors in compressed files.
d No dump, the dump program will ignore this file when backing up the file system.
C Compress, the system compresses this file transparently. When reading from this file, the decompressed data is returned; when writing data to this file, the data is compressed before being written to disk.
s Secure Delete, let the system use 0 to fill the area where the file is located when deleting this file.
u Undelete, when an application requests to delete a file, the system retains its data blocks so that the deleted file can be recovered later.

It shows that the chattr command is very useful, and some of its functions are supported by the Linux kernel version. If the Linux kernel version is lower than 2.2, many functions will not be implemented. Similarly, -D checks for errors in compressed files, which requires kernels above 2.5.19 to support. In addition, modifying attributes through the chattr command can improve system security, but it is not suitable for all directories. The chattr command cannot protect /, /dev, /tmp, /var directories.

case study

(1) Restore the /root directory, that is, all files in subdirectories.

# chattr -R +u/root

(2) Use the chattr command to prevent a key file in the system from being modified. Under Linux, some configuration files (passwd, fatab) are not allowed to be modified by anyone. In order to prevent accidental deletion or modification, you can set the "immutable bit (immutable)" of the file, the command is as follows:

# chattr +i /etc/fstab

cksum: CRC checksum of the file

  • Function: Print the CRC checksum and byte statistics of each file.
  • Usage: cksum [file] ... cksum [options]
  • The main options are as follows:
Order describe
--help Display this help message and exit.
--version Print version information and exit.

Explain that CRC (Cyclic Redundancy Check, cyclic redundancy check code) is a commonly used check code, which was widely used in early communication, because early communication technology was not reliable enough (the source of unreliability is determined by communication technology, such as Electromagnetic wave communication is affected by factors such as lightning), unreliable communication will bring confusion about "confirmation information", such as the example of the Red Army and the Blue Army communicating jointly to attack the enemy under the mountain. On the first day, the Red Army sent a message asking the Blue Army to attack together the next day. After receiving the message, the Blue Army sent a confirmation message, but the Blue Army was worried that if the "confirmation message" was unreliable and failed to reach the Red Army, then Isn't it dangerous? So the Red Army sent another "confirmation message for confirmation", but the same problem still could not be solved, and the Red Army still did not dare to act rashly. The reliability check of communication requires "verification". The verification is to check the data itself. It relies on some mathematically agreed form to check. The result of the verification is reliable or unreliable. If it is reliable, the data will be checked. If it is unreliable, discard the retransmission or perform repair.

The CRC code consists of two parts, the first half is the information code, which is the information that needs to be verified, and the second half is the check code. If the CRC code has a total length of n bits and the information code has a length of k bits, it is called (n,k) code. Its encoding rules are as follows. (1) First shift the original information code (kbit) to the left by r bits (k+r=n). (2) Then use a generator polynomial g(x) (which can also be regarded as a binary number) to divide the above formula by modulo 2, and the remainder obtained is the check code. It should be noted that modulo 2 division is adding modulo 2 in the process of division. Modulo 2 addition is actually the XOR operation we are familiar with, that is, addition does not consider carry. The formula is as follows.

0+0=1+1=0,1+0=0+1=1

That is, "different" is true, and "non-different" is false. From this we get the theorem:

a+b+b=a

That is, the "modulo 2 subtraction" and "modulo 2 addition" truth tables are exactly the same. With addition and subtraction, it can be used to define modulo 2 division, so the generator polynomial g(x) can be used to generate CRC check code. For example, g(x)=x 4 +x 3 +x 2 +1, (7,3) code, the CRC code generated by information code 110 is: 11101 | 110,0000 (set a=11101, b=1100000). Take the first 5 digits of b 11000 and XOR with a to get 101, add 101 to 00 that is not taken by b to get 10100, and then XOR with a to get 01001, which is the remainder 1001, the remainder is 1001, so the CRC code is 110,1001 . For the explanation of g(x)=x 4 +x 3 +x 2 +1: (both count from right to left) x 4 means that the fifth bit is 1, because there is no x 1, so the second bit is 0.

The standard CRC codes are CRC-CCITT and CRC-16, and their generating polynomials are as follows.

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-BB1nOhme-1688911325881) (linux common command/image-20230706152253716.png)]

case study

Use CRC to check the file, the code is as follows:

cksum 1.txt 
2422907486 124 1.txt

cmp: compare file differences

  • Role: Compare file differences.
  • Usage: cmp [options] [1st file] [2nd file]
  • The main options are as follows:
Order describe
-c--print-chars In addition to the decimal character code indicating the difference, the character corresponding to the character code is also displayed.
-i<字符数目> –ignore-initial=<number of characters>: Specify a number.
-l--verbose Mark any differences.
-s--quiet Or --silent: Do not display error messages.
-v--version Display version information.
--help online help.

The return value of the function is as follows.

  • 0: The file is the same.
  • 1: The files are different.
  • >1:An error occurred.

case study

To compare two text files, the code is as follows:

# cmp hello.cpp hello1.cpp 
hello.cpp hello1.cpp differ: byte 9, line 1

split: split the file

  • Function: Split files.
  • Usage: split [option]... [input[prefix]]
  • The main options are as follows:
Order describe
-d Numbers indicate small files after cutting.
-v Display detailed processing information.
-b <bytes> The size of each split file.
-c <bytes> Output the size of each row.
-l<number of lines> specified number of lines.
–help 显示帮助信息。
–version 显示版本信息。
[PREFIX] 设置分割后文件的前置文件名,split 会自动在前置文件名后加上编号。

案例练习

把一个大文件 file 分割成 1MB 大小的文件。

Split -b 1m file filebak_

Guess you like

Origin blog.csdn.net/u014096024/article/details/131628203