Ubuntu Server Chapter 2 Command Line Basics

Chapter 2 Command Line Basics

Explain, this note is derived from Mr. Yuan’s Ubuntu Server entry to proficient

In the environment of enterprise Linux Server, there is often no graphical environment, only a terminal window (black command line and flashing prompt). The predecessor of Linux is Unix, which retains many usage habits under Unix, so most commands are also applicable to other UNIX systems, such as the Cisco router bottom layer that I am more familiar with.

Let’s get to know the Shell first. Shell itself is a program. This program can run other programs. The driver hardware (loading driver), process management, and hardware resource allocation (memory management) are all done by the kernel. Due to issues such as permissions and security, it is not allowed to directly manipulate the kernel. If you have a friend who has learned programming, you will find that the function of the shell is very similar to the command interpreter. Essentially speaking, it is indeed that to interpret the commands entered by the user to the kernel, we can also use shell commands to write shell scripts.

There are many kinds of shells, the most commonly used is bash (Ubuntu default shell), as well as web shells and SQL shells in the security industry. The predecessor of bash is the Bourne shell used by Bell Labs to develop UNIX. **General terminal (shell) The prompt format is "User@Hostname:~", "****" means the user's home directory and non-root account. If so, "~#" means that the user is called the directory and root account. **

Basic Shell commands

 cat #Combine the contents of multiple files
 echo a>a & echo b>b #If you are a completely zero-based friend, you don’t need to understand, just follow along.
 cat a b #Found a and b displayed at the same time
 cat a #Only a
 cat #The cursor flashes, what is input, what is returned

From the above experiment, we can draw a concept, input and output, cat is a program, input some content to cat, and get some output results. In fact, a computer is essentially a machine that calculates and interprets to the user based on the user's input. The computer program is also doing similar things. After a program calls the memory to run, it needs to accept some human or other program input. After the program runs, it is called process. Linux is equipped with input for each process. Stream and output stream, note that input and output can be files, devices, terminals, or even I/O of another process. Linux assigns default input and output to each process called stdin/stdout. For example, the stdin/stdout of the cat command used above are all terminals. In addition to the above two standard streams, there is a third standard stream stderr (error output), stderr is used when an error occurs in the process. Two shortcut keys:

ls #List the contents of the directory. The specified directory displays the content of the specified directory. Generally, the text colors of directories, files, and links are different. Note that Linux is case sensitive. The uppercase file name and lowercase file name under Win are the same directory, below Some parameters of this command do not need to be memorized deliberately, only need to be used continuously to be familiar. In addition"ll == ls -la",Other distributions may not
- l #Show detailed content (permission, inode number, owner, group, size, modification time mtime)
- a #Including all hidden content, the file name under Linux"."The beginning of the file use"ls"Can't view
- d #Only display information about the directory itself
- i #Display inode information
- S #Sort by file size
- r #Reverse order
- t #Sort by modification time
- h #Display in a readable way
drwxr-xr-x  2 xiaowu xiaowu 4.0K Apr 2420:09  Test #Illustrate a line of content
" d"File type is directory"rwx"Owner authority can read, write and execute"r-x"Group permissions are readable and executable"r-x"Other user-readable and executable files in the second list of hard links or subdirectories"xiaowu"Subordinate and"xiaowu"4k size"Apr 24 20:09"Change the time"Test"file name

Here is an explanation of the inode number. The file is stored on the hard disk. The smallest unit of the hard disk is called "Sector". Each sector stores 512 bytes (0.5KB). Multiple sectors help to form a "block" (common It is 4K), which is the smallest unit of file access. When the operating system reads the hard disk, in order to improve efficiency, it reads a block continuously at a time. The creation date, etc.) is stored in the inode. Each inode corresponds to a number. The operating system identifies the file by identifying different inodes. When reading the file, it first finds the inode information according to the inode, and finds the "block" through the inode information. Read the data.

cp #copy
cp file1 file2 #Copy file1 to the new name file2, the parameter can be a complete relative path and an absolute path, multiple files can be copied to a directory
- R/r #Copy the directory and all its contents
- L #Hard link copy,When using this option, the new file has the same inode number as the old file
- s #Soft link copy, use this option to link the new file to the source file
- S #Add a suffix to the target name
- u #Copy only if the source is newer than the target

Maybe you are a little dizzy after learning this. When you only remember the first letter of the command, use "TAB" for automatic completion.

mv #Move, mv is a bit similar, after cp goes to another path, the effect of deleting the source file is the same as mv
mv file1 file2 #If under the same directory, the effect is to rename
mv file1 file2 /dir #Move multiple files to the same directory
"- f" #Force move, cover target

touch file#Create an empty file, if the file name already exists, modify the file mtime without modifying the content

rm filename #delete
- rf #Coercion and recursion
- i #Reminder before deleting
- d #Delete empty directories
Introduce the source of all evil"rm -rf /"

echo #Display command parameters in stdout (standard output),If you want to output"""Symbol, please add"\"To escape
\ n #Newline,Here are some other escape characters, understand for yourself
- n No line break at the end of the display
- e Interpret the backslash character
echo $USER #Echo can get the value in the environment variable,"$"You can use more later

Directory structure commands

The directory structure of Linux is very different from that of Windows, which ends with the drive letter (different partition) from bottom to top. The directory under Linux is larger than the partition.

Borrow the pictures from their homes in the laboratory building. This picture is very simple and clear. It is also recommended to use their home courses for learning.

tree / #The tree command can intuitively display the directory structure

FHS (File System Hierarchy Standard) is a standard, and different distributions comply with this standard, making the experience of using Unix-like systems similar. Here we need to introduce two concepts: relative path and absolute path

pwd #pwd can view the current directory
- P physical path#If you are in the soft link directory, using this parameter will directly display the real location of the link
- L logical path#The display is the same as without parameters
cd ../ #Enter the computer directory of this and directory,relative path,"."Represents the current directory in the directory,".."Represents the upper level directory in the directory
cd /home/xiaowu/ #From the root (/) Start to enter the specified directory, absolute path
mkdir filename #Create a directory, you can specify the path"-p"Multi-level directories can be created

rmdir #Delete empty directories,"-p"Can delete multiple directories

echo * #"*"Match any string, special sentence wildcard, here is a wildcard, friends who have contact with regular, please distinguish, in the text tool (awk, sed) is called regular
ls a* #Match to the current directory"a"Files and folders at the beginning

grep #grep is a text search tool, it will search for regular expressions from the input stream, and print the results to standard output
- i #Ignore case
- v #Reverse match
- n #Show line number
- r #Recurse all files in directories and subdirectories
- c #Display the number of lines containing keywords in the input stream
grep xiaowu -n /etc/passwd #Find"/etc/passwd"File contains"xiaowu"And display the number of rows
grep -f a.txt b.txt #a.Multiple keywords in txt match at the same time
grep a[123] a.txt #The matching result will be a1, a2, a3...
grep -E '1|2|3' a.txt #The matching result is 1 or 2 or 3
more filename #View one page at a time, use the space bar to turn the page
less filename #Literally translated as a bit less, it is an enhanced version of more. The difference from cat is that it displays the content of one page at a time
v #Enter the edit mode, there is a shortcut key description below the terminal
Z/B #forward/Page backward
g/G #Jump directly to the first line/the last line(Or line n)/word #Search forward keywords
? word #Search keyword backward
n/N #Positive/Reverse search keywords, this key is used in conjunction with the previous command
q #drop out
grep root /etc/passwd | less #Can be used in conjunction with the pipe character

head -n filename #View the first 10 lines of the file by default, which can be specified
tail -n filename #View the contents of the last 10 lines of the file by default, which can be specified, parameters"-f"Real-time view content=='tailf'

A lot of command tools were introduced earlier. We said that there are only two types of objects in Linux, files and processes. And the process is just a file in operation, there is actually only one kind of object, that is the file. And this is "everything is a file" in Linux.

diff filea fileb #diff is a text comparison tool, very useful when comparing configuration files, pay attention:Can be used for folders,Only compare files with the same file name
diff 1.txt 2.txt #Example, use"-u"Parameters output in the same format, use"-y"Parameters output side by side, cooperate"-W"Specify the width, plus"-w"Ignore spaces,"-i"Ignore case
1 c1
< aaaaaaa
- - - > aaaaaab
"1 c1"Means"c-change:Different content";and also"d-delete file one more than file two";"a-add file two files one more"

Here is a tool for viewing file formats. Friends who are familiar with Windows know that Windows uses a file name suffix to distinguish file formats. Once the file suffix is changed, the original function cannot be performed. Linux uses "file" to identify the file format. It matches the test set in turn by identifying the special identifiers in the header of the file. When the result is matched, it stops executing and returns the result. The test set is:

file filename #It can be a file or a list of files
- b #Display summary information, not file name
- i #mime type
stat filename #View the time attribute of the file

There are a lot of files under Linux, and finding the file you want is a problem. Linux provides tools for quick search

locate filename #Very fast and not accurate(The database is periodically generated based on the file index. The search process is only to retrieve the database and use"sudo updatedb"Update index)
find filename #The default search scope is the current directory, pay attention to access permissions, wildcards can be used in filename
sudo find /-type f -user username -mtime +1-mtime -20-name 1.txt #Parameters can be used separately, type can be specified"b"(Block file), d (directory), c (character pipe file), P (pipe file), L (symbolic link file), You can also specify the user, specify the mtime time, etc.

sort filename #Reorder the content, the default is to sort the first letter, generally text files are easy to use
- r #Sort by direction
-N # Sort by numerical value
-M # Sort by month
ls -l --sort='size|time|extension' #"ls"Commands can also be sorted

Shortcut keys and VI text editing

Just like win10, good use of shortcut keys can improve your work efficiency. Here are some commonly used shortcut keys. These shortcut keys are not only commonly used in the command line, but also often used when using SSH terminals (such as Xshell).

Since all objects of Linux are files, and the configuration of various services is based on files, a handy text editor is very necessary. Common vi (vim is its enhanced version), nao, Emacs..., Mr. Yuan said not There is a sense of superiority revealed by the "God of Editors" such as vim and Emac. Don't be obsessed with tools, tools serve people, just follow your own habits. Here are the common ways to use vim

vimtutor #Enter vi directly to enter the tutorial of vi, it is recommended to view
vi filename.txt #If the file exists, it will open the file and start editing, and if it does not exist, create
# There are three commonly used modes of vi:
# General command mode: press ESC to enter from edit mode
# Command mode: press from the general command mode":"Enter, an input box appears at the end of the terminal line, press ESC to return to the normal mode
# Edit mode: In general command mode, press i (insert before the cursor), a (insert after the cursor), o (insert and edit the next line)
# Use K, J, H, L to move the cursor (up, down, left, and right),The arrow keys on the keypad can also be used

The function buttons of vi are a bit complicated for novices. What you need is to use it continuously, and check the manual for the forgotten commands. As you use it, you will become more comfortable.

# apart from"i、a、o", There are these commands in general command mode
# A:Add the text I at the end of the line:Add text O at the beginning of the line: insert a blank line in the previous line and edit
# D:Delete the cursor to the end of the line dd:Delete the content of the line where the cursor is ndd:Delete n lines from the cursor position (including the line) nyy:Copy down n lines from the line where the cursor is
# p:Paste
# " Ctrl + f":Page backward"Ctrl+B":Page forward
# In command mode
# /str:Search string forward? str:Search backward for the string setnu(number): Display line number wq:Save and exit
# setnonu(nonumber):Do not display line number q!:Force exit without saving f:Show current file name
# n:Jump to line n (if you start the service frequently, you will get an error on the first few lines of the file, so jump directly to this line to check the error)$:Move the cursor to the beginning of the last line
# 1, $s/A/B :Replace A from the first line to the last line to become B n filename:Edit the next file(Need to save changes first)

Get help

I mentioned an operation above. Check the manual. You can’t remember the command, but you need to know how to find it through the document. It would be embarrassing to check the manual during the entire work process. A general understanding of the help manual will let you You search more efficiently. Linux traditionally has a complete documentation system. Manual is the most important help manual. You can use "man+command" to see the corresponding command help.

man -k keyword #search keyword,Find a document containing this keyword
man {1~8} command
When viewing the man manual, there is an index number in the upper left corner, and each index number (1~8), which represents a type of manual. Note that some commands may only have part of the index number. The following explains the meaning of the index number
1 : User command 2: System call 3: Advanced Unix programming library document (commonly used by developers) 4: Device interface and driver information (rarely used)
5 : File description (system configuration file)6:Games 7:file format,Conventions, encoding (ASCII, UTF8, etc.) 8: System commands and servers

GNU (Own Software Foundation), the developer of the LInux kernel, released another help manual "info"

info command #If you cannot find the information you want in the man manual, you can try"info"Obtain
Many softwares do not have official man manuals. Developers often leave README files in the software packages to give users some guidance.

The "-h/--help" we have demonstrated many times before is also a frequently used help command. In the end, we really can't find help, so search engine is a good choice

Shell input and output

Earlier, in the chapter Basic Shell Commands, we briefly introduced the concepts of input stream, output stream, and output error stream in Shell. The default input for most processes is a file or a terminal. Note that I used the word default. Does it mean I can change it? The answer is yes, this is what I want to introduce. The file descriptors of the input stream, output stream, and error output stream are 0, 1, and 2. The file descriptor is the index created by the open file defined by the LInux kernel. You may still not understand this description. Look at the following example and you will understand Up.

"|"、">"、">>"、"<" # Use pipes and redirection symbols
ifconfig | grep "inet" #will"ifconfig"The output of the command is as"grep "inet" "input of
ls >file #Redirect the output of ls to the file file, use"set -c"Can prohibit overwriting redirection overwriting files
ls >>file #Redirect and append the output of ls to fiLe
ls <1.txt #Make 1.txt file content as"ls"Command input
ls osjdijf >stdout 2>stderr #Standard output to"stdout&quot; error output is output to"stderr"
Friends who use DocKer services often see such one-click codes
" sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'{"registry-mirrors":["https://15a26sup.mirror.aliyuncs.com"]}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docke"
The EOF used in this code is a custom terminator. Note that it can be customized. It is very easy to use in scripts combined with redirects.

Errors are often encountered when using commands. Common errors are explained as follows

The pipe character is used in the previous commands, and some notes indicate the relationship of or. Here are some symbols commonly used in shell commands with special functions. Note that pipe character and pipe are different concepts

Process Management

A process is a running file. Use "ps" to execute without any parameters. The result will display 4 columns of information by default. In order to distinguish between different processes, the process is assigned an ID, which is PID. TIme is the time when the process is running. Finally, the display name COMMAND.Linux is often used by multiple terminals at the same time, and TTY indicates which terminal it is. Processes have different states, common ones are "R" (Runnable), "S" (Sleeping) and so on.

ps #Used to display the status of the current process, there are many parameters, this time the value introduces commonly used
- x #The process started by the current user
- ax #All user processes
- u #Display detailed information, commonly used &quot;ps-aux"or"ps u pid"For use with
- w #Display the complete path of the process file
The initial process of Linux is PID 0,Then the structural relationship between the parent process and the parent process is similar to a tree structure,use"ps fajx"or"pstree"Can view this structure
kill pid #Kill process
kill -STOP pid #Pause the process
kill -CONT pid #Continue the process
kil -l #For example, the signal name. I don’t have a number here. The signal name and the number represented by the signal are the corresponding relationship."kill -9 pid"To terminate the process unconditionally

Archive packaging and compression

Useful packaging tools such as winrar or 7zip (there is a Linux version) are often used under windows, and there are similar packaging programs under Linux.

The Linux standard compression program is gzip file (gunzip decompression), which can only decompress a single file. The most commonly used for multiple files is"tar"command
tar zcvf file.tar.gz  file1  file2 #"z":Call gzip compression c":Create archive"v":Detailed diagnostic output"f": File package name
tar zxvf file.tar.gz #Unzip, note that the file name is just for easy identification
tar tvf file.tat.gz #Test decompression
tar jcvf file.bz2 file1 #Call bzip compression, bzip has a high compression ratio for text compression currency gzip

Earlier I said that "sudo" is used when encountering permission problems, then what is the role of "sudo", the highest permission under LInux is root, and the Ubuntu release makes users create ordinary users for security reasons, which need to be executed The key operation is to use "sudo" to allow users to execute as "root",

cat /etc/group |grep $USER 
# One of them"sudo"Help define that you can use"sudo"Command user
# If the system needs to be given sudo permission after installation
sudo vim /etc/sudoers #Modify the configuration file, this is one way
sudo adduser username
sudo usermod -aG sudo username #Use command, recommend this

In the end, I ordered a hand-knockout. I knocked out this note during the May 1st holiday. I originally planned to write it out one day. I overestimated myself. I wish all friends on the way to study safety to enjoy this process.

Reference link

Ubuntu Server from entry to master

Streams, pipes, and redirects

The 4 main parts of Linux: kernel, shell, file structure, and utilities

Really understand the inode of linux?

Understanding inode-Ruan Yifeng

Wildcards and regular expressions in Linux

Recommended Posts

Ubuntu Server Chapter 2 Command Line Basics
ubuntu command line search
Ubuntu command line displays Chinese
Ubuntu Server Chapter 8 DNS Service
Ubuntu Server Chapter 3 Package Management
Ubuntu Server Chapter 7 Remote Management
Use the command line to detect the Ubuntu version method
Ubuntu16.04 common command notes
ubuntu install nginx server
Deploy FTP server under ubuntu
[Linux] Build Samba server (ubuntu16.04)
Install OpenSSL 1.0.2 on Ubuntu Server 14.04
ubuntu 16.04 build pptpd V** server
Server upgrade Ubuntu 20.04 LTS record
Build Ubuntu 12.04 cross compilation server
Server upgrade Ubuntu 20.04 LTS record
Ubuntu server builds Java web server
Ubuntu deploys squid proxy server
Initial setup of Ubuntu 16.04 server
Ubuntu16.04 build GitLab server tutorial