Common Linux commands – file related operations
1. User switching
su (switch user)
2. Display a list of files in the current directory
ls(list)
ls -l
ls -a(all)
ll
ll -a
In Linux, hide files as “. At the beginning
3. Directory operations
Change directory: CD (change Directory)
PWD (print working Directory)
Create directory: mkdir(make directoriy)
-p Parents if the parent directory does not exist
Cp Copy files or directories
-r recursively copies files in a specified directory with subdirectories (recursive)
Mv Move a file or directory, rename a file or directory
Rmdir Delete empty directory (remove directoriy)
Rm Deleting a file (remove)
-r Delete all files in this directory at the same time (recursive)
-f Forcibly deleting a file or directory (force)
Ordinary users do not have any prompt when deleting
However, the super administrator will prompt you to delete it
Add point: CD ~ can quickly home directory
4. Edit file content -VI,VIM(key)
Workflow flow chart:
Insert command parsing:
A: Add text after the current character;
A: Add text at the end of the line;
I: inserts text before the current character;
I: Insert text at the beginning of the line;
O: Insert an empty line after the current line;
O: Inserts an empty line before the current line;
Quick commands:
Locate the command
: set number Displays the line number
: set nonumber Cancels the line number
:n to the NTH line of text
Gg to the first line of the text
G to the last line of text
The delete command
X: Deletes a single character where the cursor is
Dd: deletes the row
Cancel the order
U undo: cancels the previous operation
Ctrl+r redo, go back to undo
Copy command
yy+p
5. View and statistics of file contents
Touch creates an empty file
Display file contents:
Cat displays text file contents
More Displays text file content in pages
less
Head and tail View the beginning or end of the text
Head -n 3 java. TXT View the first three lines of the java. TXT file
Wc statistics the number of lines, words and characters of text (Word count)
-m Indicates the number of text characters
-w Indicates the number of words in the text
-l Indicates the number of text lines
File merge and redirection
The contents of the merged files are displayed in cat 1.txt 2. TXT
TXT file: cat 1.txt 2. TXT > 3. TXT
As a standard input,
cat > 1.txt
cat >> 1.txt
> This is a symbol to redirect output
Example: Quickly clear the contents of a file
Redirection is adopted
>1.txt
7. File search
Find finds the specified file in the file system
find /usr/local/ -name word.txt
8. File compression, decompression and packaging
Command overview
Gzip: compressed (decompressed) files. The compressed file suffix is gz
Bzip2: compresses (decompresses) a file. The file suffix is bz2
Tar: package a file or directory
The command,
Gzip: gzip[option] File name to compress (uncompress)
-d Decompress compressed files
-l Displays the size, size, and compression ratio of compressed files
-num Specifies the number num to adjust the compression speed. -1 or –fast indicates the fastest compression method (but with a low compression ratio).
-9 or –best indicates the slowest compression method (high compression ratio). The default value is 6
Bzip2: bzip2 [option] File name
– d decompression
Z compression
The above – num
Description of the Tar command: Tar [option] Package file name File to be packed 1 File to be packed 2
-c Creates a package file create
-x Unlocks a package file extract
-z Uses gzip to compress files
-j Compresses files with bzip2
-v Displays the compressed file
-f Indicates the document name, which must be immediately followed by f
Case study:
Realize the packaging and unpacking of files
Realize the file compression and decompression
Gzip 1. TXT Compressed file
Gzip -d 1.txt.zip Decompress the file
Tar -cf 33.tar 1. TXT 2
Tar -xf 33.tar // Unpack the file
Gzip 33.tar // Compress the package file tar.gz
Gz 1. TXT 2. TXT // Pack the 1. TXT and 2. TXT and compress them into 33.tar.gz
Tar -zxf 33.tar.gz // Decompress 33.tar
9. Pipe command
Command format:
command1 | command2
For example:
ls -l /usr/bin | less
ls -l /usr/bin | grep less
The standard output of one command can be piped to the standard input of another command:
Grep is a powerful program for finding matching text in files