Linux basic operation, getting started

Linux Distribution

Linus towaz's Linux is just a kernel.
Kernel refers to a system software that provides functions such as device driver, file system, process management and network communication. The kernel is not a complete operating system, it is only the core of the operating system. Some organizations or manufacturers package the Linux kernel with various software and documents, and provide system installation interface and system configuration, setting and management tools, which constitute the distribution version of Linux.

Linux distributions can be broadly divided into two categories:

  • The distribution version maintained by commercial companies, represented by the famous Red Hat;
  • The distribution version maintained by community organizations, represented by Debian.

Several common centralized linux distributions

My common commands

cd /    #Return to the root directory
cd ~    #Return to home directory
pwd    #Current absolute directory
ls [-l: detailed information] [Directory or file]  #Lists the contents of the specified directory
ll		#Displays the details of the contents in the current directory

touch file name    #Create a file
cat [<< q] >> filename    #create folder

mkdir catalogue    #Create a directory (folder)
mv    #Move or rename
cp    #copy
rm [-r: Delete directory] [-i: Interactive deletion]  #delete
rmdir [-p] catalogue   #Delete the directory. If there is content in the file, bring - p

chmod    #Modify directory or file permissions

alias    #Show alias list
alias name=value(shell (command)    #Define alias
unalias name    #Cancel alias

#File compilation
:noh    #Cancel search

shell program

#Execute shell script
bash < Script name    #Redirection mode
bash Script name [parameter]
./Script name    #Scripts are executable
. Script name    #ditto

<		#Input redirection, read from file
<<		#Even file orientation
>		#Output redirection, redirecting the command (or executable) to the specified file at standard output
<<		#Output additional redirection, added to the specified file

1, Common commands

1.1 shutdown and restart

Shut down

    shutdown -h now        Turn it off immediately
    shutdown -h 5        5 Shutdown in minutes
    poweroff            Turn it off immediately


    shutdown -r now        Restart now 
    shutdown -r 5        5 Restart in minutes
    reboot                Restart now 

1.2 help commands

helpThe help command displays shell built-in command information
manman displays pagination. Press q to exit
infoDisplay more information than man, also pagination, press q to exit

Example diagram

Other small commands

[who command]: view the current process

[echo command]: display the command line parameters to the display screen (print)

echo can be followed by quotation marks or no quotation marks. For quotation marks, there are single quotation marks, double quotation marks and inverted quotation marks. The functions of these quotation marks will be described later
Single quotation mark:
Double quotation marks:
Inverted quotation marks:

[data command]: display the current date and time on the screen, and add option parameters

[cal command]: display the calendar of any year or any month from AD 1 to 9999

cal 11 #List the calendar of 11 A.D
cal 6 2021 #List the calendar for June 2021

[clear command]: clear the information on the screen

[pwd command]: displays the absolute path of the current directory

[ls]: displays all files and folders (names) in the current directory

2, Directories and files

/Is the root directory (root)
File names that begin with a dot (.) are implied files
Linux systems are case sensitive

file type

1) Ordinary file

  • ① Text file
  • ② Data file
  • ③ Executable binary program file

2) Catalog file
3) Device files are usually stored in the / dev directory.
4) Symbolic link file, equivalent to shortcut

Directory operation

2.1 directory switching cd

cd /        Switch to the root directory
cd /usr        Switch to under the root directory usr catalogue
cd ../        Switch to the previous directory or  cd ..
cd ~        Switch to home catalogue
cd -        Switch to the last accessed directory

2.2 directory view ls [-a/l]

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) ll You can view file attribute information
ls /home/user            View all directories and files under the specified directory (preferably absolute path), such as: ls /home/user

2.3 create directory mkdir

mkdir    my            Create a file named my Directory of
mkdir    /home/usr/my    Under the specified directory (absolute path), create a file named my Directory of

2.4 deleting directories or files rm

The rm command deletes files and directories
Delete file:

rm file        Delete files in the current directory
rm -i file    Delete files in the current directory (ask, yes/no)

Delete directory:

rm -r my    recursion(-r)Delete the in the current directory my catalogue
rm -rf my    Recursively delete files under the current directory my Table of contents (do not ask)
rm -ri my    Recursively delete files under the current directory my Directory (inquiry)

rmdir [-p] Directory delete the directory. If there is content in the file, bring it with you-p

Delete all:

rm -rf *    Delete all directories and files in the current directory
rm -rf /*    Delete all files in the root directory [use with caution]

Note: rm can delete not only directories, but also other files or compressed packages. To facilitate your memory, you can directly use rm -rf directories / files / compressed packages regardless of deleting any directories or files

2.5 copying directories or files cp

The cp command copies the source file or directory to the target file or directory, and the original file remains unchanged
General format:

cp [option] Source file or directory destination file or directory

If it is a directory, you need to add and use the "- r" option recursively to copy all files and subdirectories under the whole directory to the target location

2.6 renaming or moving directories or files mv

The mv command renames a file or directory, or moves a file from one directory to another

File operation command cat/more/less/head/tail

catIt is often used to display the contents of files, and files can also be created. cat file name: displays the contents of the file
moreDisplay the contents of the file, one screen at a time, and only browse down (forward) is allowed
lessThe less command displays the file contents in separate screens, and you can browse forward and backward
headThe head command displays several lines at the beginning of the specified file on the screen, with 10 lines by default
tailThe tail command displays several lines at the end of the specified file on the screen, with 10 lines by default

head [options] file

  • c byte
  • N specifies n rows

Example: head -5 file displays the first 5 lines of the file

Create file touch

create a file
touch can modify the time tag of the specified file or create an empty file, update the time tag of the existing file to the current time of the system (default), and create an empty file if the file does not exist.

-aOnly change the access time of the specified file
-mOnly change the modification time of the specified file

Command grep/sort/uniq to match, sort and display specified contents

Only wrote the command function, no options, you need to use your own Baidu

commandFunction description
grepFinds the word or phrase of the specified pattern in the text file and displays all lines including the given string pattern on the standard output device
sortSorts the rows of a text file
uniqRemove duplicate lines from ordered files (compare adjacent lines, remove duplicate lines, and sort first)

Command comm/diff for comparing text content

commandFunction description
commCompare the two sorted files line by line, and output three columns: the row appearing only in file1, the row appearing only in file2, and the row appearing in both files. Options [- 123], 1, 2, and 3 respectively mean that the first column, the second column, and the third column are not displayed
diffCompare the two files to find out their differences

Commands for linking files ln

Hard link: when a hard link is established, a directory entry of the target file is added in another directory or this directory, and no new file is created

  • You cannot create a hard link to a catalog file
  • Hard links cannot be established between different file systems

Soft link (symbolic link)
You can establish symbolic links to catalog files or between different file systems

  • Delete the source file or directory, only delete the data, not the link. Once the source file is created with the same file name, the link will continue to point to the new data of the file
  • In the long list of directories, symbolic links are represented by l
  • The number of bytes in the pathname of the symbolic link's linked file when it is large
  • When listing files with the ls -l command, you can see an arrow after the symbolic link name pointing to the source file or directory

ls command to create a link
ls [options] source file [target file]
ls defaults to hard link
ls -s create soft connection

A command that changes the access rights of a file or directory

4 different types of users:
① owner ② users in the same group ③ other users who can access the system ④ super user (root)
3 ways to access files or directories:
① Read (r) ② write (r) ③ executable or query (x)

The chmod command is used to modify the access permissions of a file or directory

The umask command sets a mask that limits the permissions for new files

File compression and decompression gzip/unzip

The gzip command compresses and decompresses files
gzip [options] [name]
Name table compressed (decompressed) file name

unzip command decompresses the zip file

3, File editing vi/vim

1. Basic concepts of vi

Basically, vi can be divided into three states: command mode, insert mode and bottom line mode:

  • command mode
    Control the movement of the screen cursor, deletion of characters, words or lines, moving and copying a section and entering Insert mode, or to last line mode.
  • Insert mode
    Text input can only be made in Insert mode. Press "ESC" to return to command line mode.
  • last line mode
    Save the file or exit vi, or set the editing environment, such as finding string, listing line number, etc.

All function keys operate in the bottom line mode (colon operation)

2. vi basic operation

a) After entering vi/vim, it is in the command line mode. The file can only be edited (input text) in the insertion mode to be switched

Press「i」Switch to insert mode「insert mode」,Press“ i"After entering the insertion mode, input the file from the current position of the cursor;
Press「a」After entering the insertion mode, input text from the next position where the current cursor is located;
Press「o」After entering the insert mode, insert a new line and input text from the beginning of the line.

b) Switch of insert
If you are currently in "Insert mode", you can only input text all the time. If you find that you have entered a wrong word! If you want to move back with the cursor keys and delete the word, you must first click "ESC" to go to "command mode" and then delete the text.

c) Exit vi and save file

: wq (input「wq」,Save and exit vi)
: q! (input q!, Forced exit without saving vi)
: w filename (input 「w filename」Returns the article to the specified file name filename (save)

3. command mode

1) Paste, copy, delete, etc

----Command line mode:----

yy             Copy cursor line
2yy            Copy the 2 lines starting from the line where the cursor is located

p              paste

x              Deletes the character of the current cursor
X((in words)       Deletes the character preceding the current cursor
dd             Delete the line where the cursor is located (actually cut)
3dd            Delete the 3 lines (cut) starting from the line where the cursor is located

2) . cursor movement

---Command mode:----
h Shift left   j Move down k Move up  l Move right (the four arrows can also move up, down, left and right)
H First line of current screen
M Middle of current screen
L Last line of current screen
ctrl+f  Page down
ctrl+b  Page up
 By number「0」: Move to the beginning of the article.
Press「G」: Move to the end of the article.
Press「$」: Move to the "end of line" of the line where the cursor is located.
Press「^」: Move to the "beginning of line" of the line where the cursor is located
 Press「w」: The cursor jumps to the beginning of the next word
 Press「e」: The cursor jumps to the end of the next word
 Press「b」: The cursor returns to the beginning of the previous word
G       Navigate to the last line (the last line of the entire document)
5G      Go to line 5
gg      Navigate to the first line (the first line of the entire document)
w       Skip to the next word
b       Skip to previous word
{       Move to previous code block
}       Move to next code block
v,V    Press first v Then select several lines with the up and down arrows, and then>         Move these lines collectively to the right and then.Continue to move right 
v,V    Press first v Then select several lines with the up and down arrows, and then<         These lines move left collectively
 Press「#l」: Move the cursor to the second line of the line#Two positions, such as 5L and 56L.

3). Reply to last operation
「u」 : if you execute a command by mistake, you can immediately press "U" to return to the previous operation. Press "U" multiple times to perform multiple replies.

4. Bottom line mode command

First press the "ESC" key to confirm that you are in "command mode", and then press the colon to enter "last line mode".

a) List line numbers

: set sun		#set number 
: set nonu    #Cancel line number display

b) Unhighlight

: noh

c) Skip to a line in the file

: num		#Go to line num

d) Find character

: /ch        #Find the character ch, n to continue with the next find
: ?ch

e) Regular expression lookup string

: /^string$				#Find string string

f) Bottom line mode string replacement

$ 		#Represents the last line
. 		#Indicates where the cursor is
1 		#Indicates the beginning of the first line
%s 		#Represents all rows
/g 		#Represents multiple of this line

g) Split screen

vsp xxx.c    	#The left and right split screens open XXX under this terminal C documents,
#Open without a file name. There is a currently open file
sp xxx.c   Open file in split screen  xxx.c  Same usage vsp

Keywords: Linux Operation & Maintenance vim

Added by expl0it on Fri, 17 Dec 2021 19:00:22 +0200