Date Displays the system date
Curl ifconfig.me Curl ifconfig.me curl ifconfig.me
Disk space:
Df -h Displays the list of mounted partitions
Du -sh dir5 (-s indicates summary) lists only one summary value
Du -ah dir5 (-a indicates directories and files)
File and directory operations:
Ls -l Displays detailed information about files and directories
Ls -l test. TXT Displays details about files and directories
Ls -a Displays hidden files
Ls -f Displays files in the directory
PWD Displays the working path
Mkdir dir1 dir2 Create two directories simultaneously
Mkdir -p/TMP /dir1/dir2 Creates a directory tree
Rm file Drop a file called ‘file’
Rmdir dir delete an empty directory called ‘dir’
Rm -r dir Deletes a directory named ‘dir’ and deletes the files in the directory
Rm -r dir1 dir2 Delete both directories and their contents
Mv dir new_dir Renames or moves a directory
Cp file1 file2 Copies a file
Cp -a dir1 dir2 Copies a directory
Cp -a/TMP/dir1. copy a directory to the current working directory
File search:
Find / -name file Searches for files and directories in the root file system starting with ‘/’
Find /home/user-name *. Bin Searches for files ending in’.bin’ in the directory ‘/home/user’
Log file operations:
Grep ‘2020-07-06 11:3[4-7]’ pay.log Query logs generated between 11:34 and 11:37
Grep -e “word1 | word2 | word3” file. TXT satisfy any conditions (word1 word2, and one of word3) will match.
Grep word1 file. TXT | grep word2 | grep word3 must to satisfy the three conditions “(word1 word2 and word3) to match.
The grep word1 *. The log | grep word2 | more paging view what accords with a condition.
Grep -c 5 foo file Displays the line matching the string foo and the five lines above and below in file
Grep -b 5 foo file Displays foo and the first five lines
Grep -a 5 foo file Displays foo and the next five lines
Grep –color word1 file. TXT Displays the word1 in the query result in color
View file contents:
Cat file1 Forwards the file from the first byte
Tac file1 looks at the contents of a file in reverse, starting with the last line
More file1 Displays the contents of a long file
Less file1 is similar to the ‘more’ command, but it allows the same reverse operation in the file as the forward operation
Head-2 file1 Displays the first two lines of a file
Tail-2 file1 Displays the last two lines of a file
Tail -f /var/log/messages View the contents added to a file in real time
File packaging, compression, and decompression
Packaging is to make a large number of files or directories into a total file; Compression is the process of turning a large file into a small file using some compression algorithms.
Many Linux compressors can compress only one file, so if you want to compress a large number of files, you need to first pack the large number of files into a package (tar) and then use the compression program (gzip bzip2).
Gzip program
Disadvantages: Cannot compress the directory, the original file will be deleted after the compressed file is generated.
Compressed file:
gzip tt.txt
Unzip files:
gunzip tt.txt.gz
Zip program:
Compare with gzip: you can compress directories and keep the original files.
Compressed file:
zip test.zip tt.txt
Compressed directory (always include -r) :
zip -r tt.zip dir5
Unzip files:
unzip test.zip
Tar itself does not have compression. It is implemented by calling other compression functions.
Format of the tar command:
Tar [required parameters][Select parameters][file]
The required parameters are as follows:
-c Creates a new compressed file
-r Adds a file to a compressed file
-x Extracts files from compressed files
-t Displays the contents of compressed files
-z Supports gzip file compression or decompression
-j Supports bzip2 to compress or decompress files
-v Displays the operation process
-k Keeps the original file
-m Indicates that the file is not overwritten
-f(file) Uses archive files or devices. This option is usually mandatory.
Package files :(can be files and directories)
tar -vcf test.tar test.txt tt.txt
Unpack file:
tar -xvf test.tar
Compression:
Tar -zxcf test.gz Dir5 Exception information RTF
Tar -jvcf test.bz2 Dir5 Exception information RTF
Extract:
tar -zxvf test.bz2
tar -jxvf test.bz2
1.1 Searching for logs generated during a certain period of time in log files
sed -n ‘/2018-07-05 13:20:00/,/2018-07-05 13:25:00/p’ logfile.log
1.2 Do not Decompress the GZ File Perform search in the file
gunzip -c logfile.gz | grep ‘test’