A complete collection of commonly used Linux commands and explanations (2)

Table of contents

Preface

1. File permissions

2. Special attributes of files

3. Packaging and Compressing Files

4. View file contents

5. Text processing

5.1 grep

5.2 sed

5.3 Others

Summarize



Preface

This article continues from the collection of common Linux commands and explanations (1) and continues to introduce some common Linux commands, including file permissions, special attributes of files, packaging and compressing files, viewing file contents, and text processing. Please see the text for common Linux commands and explanations.


- Use "+" to set permissions and "-" to cancel

1. File permissions

  1. ls -lh: List file details, including file permissions.

  2. ls /tmp | pr -T5 -W$COLUMNS: Display the files in the /tmp directory in the terminal in a 5-column format.

  3. chmod ugo+rwx directory1: Set the read, write and execute permissions of the owner, group and others of directory directory1.

  4. chmod go-rwx directory1: Cancel the read, write and execute permissions of the group and others in directory directory1.

  5. chown user1 file1: Change the owner of file file1 to user1.

  6. chown -R user1 directory1: Change the owner of directory directory1 and the owners of all files in it to user1.

  7. chgrp group1 file1: Change the group of file file1 to group1.

  8. chown user1:group1 file1: Change the owner and group of file file1 to user1 and group1 at the same time.

  9. find / -perm -u+s: List all files in the system that use SUID permissions.

  10. chmod u+s /bin/file1: Set SUID permissions to file file1, allowing the user executing the file to have the same permissions as the owner.

  11. chmod us /bin/file1: Disable SUID permissions on file file1.

  12. chmod g+s /home/public: Set SGID permissions for the directory /public, similar to SUID, but for the directory.

  13. chmod gs /home/public: Disable SGID permissions for the directory /public.

  14. chmod o+t /home/public: Set paste (STIKY) permissions on the file /public, allowing only legal owners to delete files.

  15. chmod ot /home/public: Disable paste (STIKY) permissions for directory /public.

2. Special attributes of files

  1. chattr +a file1: Set the file file1 to the append-only attribute, which only allows the file to be read and written in append mode, and modification and deletion of the file are prohibited.

  2. chattr +c file1: Set file file1 to be automatically compressed and decompressed by the kernel (requires specific file system and tool support).

  3. chattr +d file1: Set file file1. When performing a file system backup, the dump program will ignore this file.

  4. chattr +i file1: Set file file1 as an immutable attribute, that is, it cannot be deleted, modified, renamed or linked.

  5. chattr +s file1: The setting file file1 can be safely deleted, that is, it will not be restored after deletion.

  6. chattr +S file1: Makes the system write the modified results to the disk immediately after the application performs a write operation on file file1.

  7. chattr +u file1: Set file file1 to allow recovery, that is, if the file is deleted, the system will allow the deleted file to be restored in the future.

  8. lsattr: Displays special attributes of the file.


3. Packaging and Compressing Files

  1. bunzip2 file1.bz2: Unzip the file named 'file1.bz2'.

  2. bzip2 file1: Compress the file named 'file1'.

  3. gunzip file1.gz: Unzip the file named 'file1.gz'.

  4. gzip file1: Compress the file named 'file1'.

  5. gzip -9 file1: Compress 'file1' with maximum compression.

  6. rar a file1.rar test_file: Create a compressed package named 'file1.rar', which contains 'test_file'.

  7. rar a file1.rar file1 file2 dir1: Compress 'file1', 'file2' and directory 'dir1' to 'file1.rar' at the same time.

  8. rar x file1.rar: Unzip the compressed package named 'file1.rar'.

  9. unrar x file1.rar: Unzip the compressed package named 'file1.rar'.

  10. tar -cvf archive.tar file1: Create a non-compressed tarball and put 'file1' into it.

  11. tar -cvf archive.tar file1 file2 dir1: Creates an archive file containing 'file1', 'file2' and 'dir1'.

  12. tar -tf archive.tar: displays the contents of the tar package.

  13. tar -xvf archive.tar: Decompress the tar package.

  14. tar -xvf archive.tar -C /tmp: Extract the compressed package to the /tmp directory.

  15. tar -cvfj archive.tar.bz2 dir1: Create a tar package compressed in bzip2 format.

  16. tar -jxvf archive.tar.bz2: Decompress the tar package compressed in bzip2 format.

  17. tar -cvfz archive.tar.gz dir1: Create a tar package compressed in gzip format.

  18. tar -zxvf archive.tar.gz: Decompress the tar package compressed using gzip format.

  19. zip file1.zip file1: Create a compressed package in zip format, which contains 'file1'.

  20. zip -r file1.zip file1 file2 dir1: Compress multiple files and directories into a compressed package in zip format at the same time.

  21. unzip file1.zip: Unzip the compressed package in zip format.


4. View file contents

  1. cat file1: View the contents of file 'file1' forward starting from the first byte.

  2. tac file1: View the contents of file 'file1' in reverse direction starting from the last line.

  3. more file1: View the contents of a long file 'file1' and browse page by page.

  4. less file1: Similar to the 'more' command, but allows the use of reverse operations, forward and reverse operations in the file.

  5. head -2 file1: View the first two lines of file 'file1'.

  6. tail -2 file1: View the last two lines of file 'file1'.

  7. tail -f /var/log/messages: View the content added to the file '/var/log/messages' in real time, suitable for log files and other files that require real-time monitoring.


5. Text processing

5.1 grep

These commands use pipes, standard input (STDIN), and standard output (STDOUT) for text manipulation:

  1. Apply the command to the contents of file1 and output the processing results to the result.txt file:

    cat file1 | command( sed, grep, awk, 等等...) > result.txt
    
  2. Apply the command to the contents of file1 and append the processing results to the end of the existing result.txt file:

    cat file1 | command( sed, grep, awk, 等等...) >> result.txt
    
  3. Find the line containing the keyword "Aug" in the file '/var/log/messages':

    grep Aug /var/log/messages
    
  4. Find words starting with "Aug" in the file '/var/log/messages':

    grep ^Aug /var/log/messages
    
  5. Select all lines containing numbers in the file '/var/log/messages':

    grep [0-9] /var/log/messages
    
  6. Recursively search the directory '/var/log' and its subdirectories for the string "Aug":

    grep Aug -R /var/log/*
    

5.2 sed

These  sed commands can replace, delete, search, etc. text files:

  1. Replace "string1" with "string2" in the example.txt file:

    sed 's/stringa1/stringa2/g' example.txt
    
  2. Remove all blank lines from the example.txt file:

    sed '/^$/d' example.txt
    
  3. Remove all comment lines starting with zero or more spaces, and all blank lines from the example.txt file:

    sed '/ *#/d; /^$/d' example.txt
    
  4. Convert the input string "esempio" to uppercase letters:

    echo 'esempio' | tr '[:lower:]' '[:upper:]'
    
  5. Exclude the first line from the result.txt file:

    sed -e '1d' result.txt
    
  6. View only the lines containing the keyword "string1" in the example.txt file:

    sed -n '/stringa1/p' example.txt
    
  7. Remove zero or more whitespace characters at the end of each line from the example.txt file:

    sed -e 's/ *$//' example.txt
    
  8. Remove all "string1" terms from the document, leaving the rest:

    sed -e 's/stringa1//g' example.txt
    
  9. Look at the content from the first to the fifth line in the example.txt file:

    sed -n '1,5p;5q' example.txt
    
  10. Look at the fifth line in the example.txt file:

    sed -n '5p;5q' example.txt
    
  11. Replace each consecutive zero in the example.txt file with a single zero:

    sed -e 's/00*/0/g' example.txt
    

5.3 Others

These commands can help you process, compare and merge files:

  1. Display the contents of file1 with the line number in front of each line:

    cat -n file1
    
  2. Delete all even lines in the example.txt file. Use the awk command to filter out odd-numbered lines:

    cat example.txt | awk 'NR%2==1'
    
  3. In the string "abc", use the awk command to print the first column (words separated by spaces):

    echo a b c | awk '{print $1}'
    
  4. In the string "abc", use the awk command to print the first and third columns:

    echo a b c | awk '{print $1,$3}'
    
  5. Merge the contents of file1 and file2 line by line:

    paste file1 file2
    
  6. Merge the contents of file1 and file2 line by line, separated by the "+" character:

    paste -d '+' file1 file2
    
  7. Sort the contents of file1 and file2:

    sort file1 file2
    
  8. Take the union of file1 and file2 and remove duplicate lines:

    sort file1 file2 | uniq
    
  9. Delete the intersection of file1 and file2, and keep the other lines:

    sort file1 file2 | uniq -u
    
  10. Take the intersection of file1 and file2 and keep only the lines that exist in both files:

    sort file1 file2 | uniq -d
    
  11. Compare the contents of file1 and file2 and delete only the lines contained in file1:

    comm -1 file1 file2
    
  12. Compare the contents of file1 and file2 and delete only the lines contained in file2:

    comm -2 file1 file2
    
  13. Compare the contents of file1 and file2 and delete only the lines contained in both files:

    comm -3 file1 file2
    


Summarize

The use of Linux commands is flexible, efficient and powerful. They provide fast, automated and highly customizable operations. Large tasks can be processed with low resource consumption using Linux commands, and programming and batch operations are supported. All in all, Linux command is a powerful tool that can provide efficient, flexible and customizable operations, which is very valuable for the understanding and mastery of computer systems.

Follow, like, collect, I hope friends can connect three times with one click!

Guess you like

Origin blog.csdn.net/m0_71369515/article/details/132732902
Recommended