Linux common command practice

1. Directory operation command

  •   Create directory
Switch to the specified directory and execute mkdir catalogue
mkdir test01    --Create under current directory test01 catalogue



mkdir    test01/test001    Create a file named test001 Directory of



mkdir    /opt/test01    Create a file named test01 Directory of,Note: the specified directory is preceded by a backslash


  • Delete directory
rm file name    --Delete the specified file in the current directory, but generally do not have permission    
                rm: cannot remove 'test01/': Is a directory
rm -f file    Delete files in the current directory (don't ask)

Delete directory:-r Recursive processing  -f Is forced deletion.
rm -r aaa    Recursively delete files under the current directory aaa catalogue
rm -rf aaa    Recursively delete files under the current directory aaa Table of contents (do not ask)

  rm -rf t*  --Delete to t The first file or directory


  • View directory
Command: ls [-al]
ls --View all directories and files in the current directory ls -a -- View all directories and files in the current directory (including hidden files) ls -l or ll -- List view all directories and files in the current directory (list view, display more information) ls /dir --View all directories and files in the specified directory, such as: ls /usr


  • Directory modification  
--Directory and file renaming
mv Current directory new directory
    mv test001 test002   --Modify current directory test001 by test002


 -- Cut directory

mv Directory name the new location of the directory
    mv test01/test002/  test02
   ---The current directory test01 Medium test002 Clip and paste to the in the current directory test02 in


 -- replica catalog

    cp -rf Directory name the destination location of the directory copy  
    cp -rf test02 /opt/soft/tomcat/qisheng-war-bak/test01
    --The test02 Copy to/opt/soft/tomcat/qisheng-war-bak/test01 In the file


 -- Search directory [query] find

    find Directory parameter file name
     find test02/ -name 't*'
---search test02 The name in the directory is t The first directory or file


2. File operation

  • File addition


--new file touch
     touch file name.suffix
     touch  wenjian.txt
  --Create a file named wenjian.txt File       






  • File deletion
--Deleting a file is the same as deleting a directory command
    rm -rf file name
  • Document content modification
--[vi Three modes of editor]
    vi File name or vim file name
--use vi After the editor opens the file, it cannot be edited because it is in command mode. Click the keyboard i/a/o Enter edit mode---Type.



[three modes of vi editor]
Basically vi can be divided into three states: command mode, Insert mode and last line mode. The functions of each mode are as follows:
1) command mode
Control the movement of the screen cursor, the deletion of characters, words or lines, search, move, copy a section and enter Insert mode, or go to last line mode.
Common commands in command line mode:
[1] Control cursor movement: ↑, ↓, j
[2] Delete current row: dd
[3] Find: / character
[4] Enter edit mode: i o a
[5] Enter bottom line mode:

2) Insert mode
Text input can only be made in Insert mode. Press "ESC" to return to command line mode.
Common commands in edit mode:
[1] ESC exits the editing mode to the command line mode;

3) last line mode
Save the file or exit vi, or set the editing environment, such as finding string, listing line number, etc.
Common commands in bottom line mode:
[1] Exit editing:: q
[2] Forced exit:: q!
[3] Save and exit:: wq

/**Edit file

After opening the file with vi editor, click the key: i, a or o to enter the editing mode.

i:Start insertion before the character of the cursor
a:Start insertion after the character of the cursor
o:Insert a new line below the line where the cursor is located

Save or cancel editing

Save file:

Step 1: ESC enters the command line mode
 Step 2: enter bottom line mode
 Step 3: wq save and exit editing

Cancel editing:

Step 1: ESC enters the command line mode
 Step 2: enter bottom line mode
 Step 3: q! Undo this modification and exit editing 


  • View of files

--File view command: cat/more/less/tail

cat: Look at the last screen

Example: Using cat see/etc/sudo.conf File, only the last screen can be displayed
cat sudo.conf

more: Percentage display

Example: Using more see/etc/sudo.conf File, percentage can be displayed, carriage return can be on the next line, and space can be on the next page, q You can exit the view
more sudo.conf

less: Page view

Example: Using less see/etc/sudo.conf File, you can use the PgUp and PgDn Up    And page down, q End view
less sudo.conf

tail: Specify the number of rows or view dynamically

Example: Using tail -10 see/etc/sudo.conf The last 10 lines of the file, Ctrl+C end  
tail -10 sudo.conf


3. Compress and decompress files


Packaging: organize some scattered files into a package, in which the size and volume of the package have not changed. Integrate multiple files into one package.
Compression: convert a large file into a small file through a compression algorithm. Note that compression only operates on one file. When you want to compress multiple files, you need to use packaging. Package first and then compress.
Decompression: the reverse process of compression, which restores the documents, files and other things compressed by the software to what they were before compression. 


Common compression formats

Compression format:  . zip,.gz,.bz2,.tar.gz,.tar.bz2,.tar.xz
The most commonly used formats are. tar.gz and. tar.bz2

. zip format

The zip format is actually the same as that of windows, that is, it can be decompressed and used with windows.

zip Compressed file name source file    //File compression: compress the source file into a compressed file. zip format
zip -r Compressed file name original directory   //Compressed directory
unzip Compressed file   //Unzip file








. gz format

In fact, the gz format itself does not provide the function of retaining the source file. To retain the source file, use the - c parameter + >, - c will output the compression result to the console, and > can write the output result to the file








 . bz2 format

Note: the bzip2 command cannot compress a directory


. tar package

Packaged files in Linux usually end in. tar, and compressed commands usually end in. gz.
In general, packaging and compression are carried out together. The suffix of the packaged and compressed file is generally. tar.gz.

Command: tar -zcvf package the compressed file name the file to be packaged
Where: z: call gzip compression command for compression
c: Package file
v: Display running process
f: Specify a file name



.tar.gz format
 actually.tar.gz The format is first packaged as.tar Format, and then compressed to.gz Format, focus on mastering!
To compress multiple files, you only need to separate the spaces of multiple source files. You can use the path to compress the files to the specified directory+Compressed file name.
Option Description:
-z: Compress to.tar.gz format
-x: decompression .tar.gz format
-t: Only view the compressed files without decompressing



  tar.bz2 format

In fact, the. tar.bz2 format is first packaged into. tar format, and then compressed into. Bz2 format. Focus on mastering!
The usage is basically consistent with the format of. tar.gz, and there will be no more explanation here.



  . tar.xz format

linux does not support direct compression and decompression of files in this format. xz format needs to be combined with tar format.




Reference documents:

Tags: Operating System

Posted on Sun, 31 Oct 2021 23:46:27 -0400 by johnsworld