Common Linux commands and basic knowledge

Preface

Starting from this chapter, we will officially enter our Linux learning. Before formal learning, we must first be familiar with the operation of Linux and some basic knowledge.

1. Basic instructions under Linux

1、ls

Syntax: ls [options][directory or file]
Function: For a directory, this command lists all subdirectories and files in the directory. For files, the file name is listed along with other information.
Common options:

ls -l 列出文件的详细信息
ls -a 列出目录下的所有文件,包括以 . 开头的隐含文件。
ls -i 输出文件的 i 节点的索引信息。 如 ls –ai 指定文件
ls -R 列出所有子目录下的文件。(递归)
ls -t 以时间排序。
ls -1 一行只输出一个文件。

Operation demonstration:
Insert image description here
ls options can be used in combination
. Other options:

-d 将目录象文件一样显示,而不是显示其下的文件。 如:ls –d 指定目录
-k 以 k 字节的形式表示文件的大小。ls –alk 指定文件
-n 用数字的 UID,GID 代替名称。 (介绍 UID, GID)
-F 在每个文件名后附上一个字符以说明该文件的类型,“*”表示可执行的普通文件;“/”表示目录;“@”表
示符号链接;“|”表示FIFOs;“=”表示套接字(sockets)。(目录类型识别)
-r 对目录反向排序。
-s 在l文件名后输出该文件的大小。(大小排序,如何找到目录下最大的文件)

2、pwd

Syntax: pwd
function: Display the directory where the user is currently located
. Operation demonstration:
Insert image description here

3、cd

Before understanding cd, let us first understand the absolute path and relative path.
Insert image description here
Take this picture as an example:
Absolute path: C\User\lenovo\Picture\Saved picture (starting from the root directory to our file location)
Relative path:. .\picture\saved picture
syntax: :cd directory name
function: change the working directory. Change the current working directory to the specified directory.
Common options:

cd .. : 返回上级目录
cd ~:进入用户家目
cd -:返回最近访问目录
cd 绝对路径
cd 相对路径

Operation demonstration:
Insert image description here

4、touch

Syntax: touch [option]... File...
Function: The touch command parameters can change the date and time of a document or directory, including access time and change time, or create a new file that does not exist.
Operation demonstration:
Insert image description here
Other options:

cd .. : 返回上级目录
cd ~:进入用户家目
cd -:返回最近访问目录
cd 绝对路径
cd 相对路径

Here we just need to learn how to create files at the primary level.

5、mkdir

Syntax: mkdir [options] Directory
function: Create a directory in the current directory
. Common options:

-p, --parents  可以是一个路径名称。此时若路径中的某些目录尚不存在,加上此选项后,系统将自动建立
好那些尚不存在的目录,即一次可以建立多个目录(递归创建文件)

Operation demonstration:
Insert image description here
Under Linux, the color of ordinary files and the color of directories are different.

6、is rm

rmdir is a command corresponding to mkdir. mkdir is to create a directory, and rmdir is a delete command.
Syntax: rmdir [-p][dirName]
Function: Delete empty directories
Common options:

p 当子目录被删除后如果父目录也变成空目录的话,就连带父目录一起删除

Operation demonstration:
Insert image description here

7、 rm

The rm command can delete files or directories at the same time.
Syntax: rm [-firv][dirName/dir]
Function: Delete files or directories
Common options:

-f 即使文件属性为只读(即写保护),亦直接删除
-i 删除前逐一询问确认
-r 删除目录及其下所有文件

Operation demonstration:
Insert image description here

8、man

Man allows us to check all parameters of Linux commands.
Syntax: man [options]
Common command options:
Operation demonstration:
Insert image description here
When we forget the options of the command, we can use man.
For example, check our ls command:
Insert image description here

9、cp

Syntax: cp [option] Source file or directory Target file or directory
Function: Copy files or directories
Note: The p command is used to copy files or directories. If more than two files or directories are specified at the same time, and the final destination is an already directory exists, it will copy all previously specified files or directories to this directory. If multiple files or directories are specified at the same time, and the final destination is not an existing directory, an error message will appear.
Common options:

-f 或 --force 强行复制文件或目录, 不论目的文件或目录是否已经存在
-i 或 --interactive 覆盖文件之前先询问用户
-r递归处理,将指定目录下的文件与子目录一并处理。若源文件或目录的形态,不属于目录或符号链接,则一律视为普通文件处理
-R 或 --recursive递归处理,将指定目录下的文件及子目录一并处理

Operation demonstration:
Insert image description here

10、mv

The mv command is the abbreviation of move. It can be used to move files or rename files (move (rename) files). It is a commonly used command in Linux systems and is often used to back up files or directories.
Syntax: mv [options] source file or directory target file or directory
Function:

  1. Depending on the type of the second parameter in the mv command (whether it is a target file or a target directory), the mv command renames the file or moves it to a new directory.
  2. When the second parameter type is a file, the mv command completes the file renaming. At this time, there can only be one source file (it can also be the source directory name). It will rename the given source file or directory to the given one. Target file name.
  3. When the second parameter is the name of an existing directory, there can be multiple source files or directory parameters, and the mv command will move all the source files specified by each parameter to the target directory.

Common options:

-f :force 强制的意思,如果目标文件已经存在,不会询问而直接覆盖
-i :若目标文件 (destination) 已经存在时,就会询问是否覆盖!

Operation demonstration:
Insert image description here

11、echo

Syntax: echo "content"
function: print content to standard output Output
redirection:
>: output redirection (overwrite)
>>: append redirection (append write)
Operation demonstration:
Insert image description here
input redirection:
<: should be from the keyboard Get content from file

12. cat and tac

Syntax: cat [option][file]
Function: View the contents of the target file
Common options:

-b 对非空输出行编号
-n 对输出的所有行编号
-s 不输出多行空行

Operation demonstration:
Insert image description here
Let's take a look at the function of tac.
Insert image description here
The function of tac is reverse output.

13、more

Syntax: more [option][file]
Function: more command, the function is similar to cat
common options:

-n 对输出的所有行编号
q 退出more 

Operation demonstration:
Insert image description here

14、less

Syntax: ess [parameter] File
function: less is similar to more, but you can use less to browse the file at will, while more can only move forward, but not backward, and less will not load the entire file before viewing.
Common options:

-i  忽略搜索时的大小写
-N  显示每行的行号
/字符串:向下搜索“字符串”的功能
?字符串:向上搜索“字符串”的功能
n:重复前一个搜索(与 /? 有关)
N:反向重复前一个搜索(与 /? 有关)
q:quit

The difference between less and more

  1. The less tool is also a tool for paging display of files or other output. It should be said that it is an orthodox tool for viewing file contents in Linux and is extremely powerful.
  2. The usage of less is more flexible than that of more. When using more, we have no way to turn forward and can only look back. But when using less, we can use the [pageup][pagedown] and other key functions to flip back and forth through the file, making it easier to view. The contents of a file!
  3. less can have more search functions, not only downward search, but also upward search.

15、head

Syntax: head [parameter][file]
Function: head is used to display the beginning of the file to the standard output. The default head command prints the first 10 lines of the corresponding file.
Common options:

-n<行数> 显示的行数

Operation demonstration:
Insert image description here
The default display of head is 10 lines.

16、tail

The functions of tail and head are similar.
Syntax: tail [required parameters] [selected parameters] [file]
Function: used to display the content at the end of the specified file. When no file is specified, it is processed as input information. Commonly used to view log files.
Common options:

-f 循环读取
-n<行数> 显示行数

Operation demonstration:
Insert image description here

17、find

Syntax: find pathname -options
Function: Used to find files in the file tree and process them accordingly (possibly accessing the disk)
Common options:

-name   按照文件名查找文件。
  1. The find command under Linux searches for files in the directory structure and performs the specified operation.
  2. As long as you have the corresponding permissions, even if the system contains a Network File System (NFS), the find command is also valid in the file system.

18、grep

Syntax: grep [option] Search string file
function: Search for a string in the file and print out the found lines. It is also used to find strings or regular expressions that meet the conditions in the file.
Common options:

-i :忽略大小写的不同,所以大小写视为相同
-n :顺便输出行号
-v :反向选择,亦即显示出没有 '搜寻字符串' 内容的那一行
-E选项可以用来扩展选项为 正则表达式 ;
$ 表示匹配文件末尾,字符需要在 $ 之前表示以字符结尾 a$表示以a结尾
^ 表示匹配文件 起始 ,字符需要在 ^之后表示以字符起始 ^a表示以a起始

Operation demonstration:
Insert image description here

19、zip/unzip

Syntax: zip compressed file.zip Directory or file
function: compress the directory or file into zip format
. Common options:

-r 递归处理,将指定目录下的所有文件和子目录一并处理

Operation demonstration:
Insert image description here
When we compress a directory, we must bring the -r option.

20、tar

Common options with the same functionality as zip/unzip :

-c :建立一个压缩文件的参数指令(create 的意思)-x :解开一个压缩文件的参数指令!
-z :是否同时具有 gzip 的属性?亦即是否需要用 gzip 压缩?
-t :查看 tarfile 里面的文件!
-f :使用档名,请留意,在 f 之后要立即接档名喔!不要再加参数!
-C : 解压到指定目录

Operation demonstration:
Insert image description here

21. Commonly used hot keys

[Tab]按键---具有『命令补全』和『档案补齐』的功能
[Ctrl]-c按键---让当前的程序『停掉』
[Ctrl]-d按键---通常代表着:『键盘输入结束(End Of File, EOF 戒 End OfInput)』的意思;另外,他也可以用来取代exit

2. Expansion instructions under Linux

1、date

Syntax: date [OPTION]… [+FORMAT]
Function: Display time
Common options:

%H : 小时(00..23) 
%M : 分钟(00..59) 
%S :(00..61) 
%X : 相当于 %H:%M:%S 
%d :(01..31) 
%m : 月份 (01..12) 
%Y : 完整年份 (0000..9999) 
%F : 相当于 %Y-%m-%d
%s : 时间戳 

Operation demonstration:
Insert image description here

2、Cal

Syntax: cal [parameter][month][year]
Function: used to view time information such as calendar. If there is only one parameter, it represents the year (1-9999). If there are two parameters, it represents the month and year.
Common options:

-3 显示系统前一个月,当前月,下一个月的月历
-j  显示在当年中的第几天(一年日期按天算,从11号算起,默认显示当前月在一年中的天数)
-y  显示当前年份的日历

Operation demonstration:
Insert image description here

3、bc

The bc command can perform floating point operations
. Operation demonstration:
Insert image description here

4、name

Syntax: uname [option]
Function: uname is used to obtain information about the computer and operating system
. Common options:

-a或–all 详细输出所有信息,依次为内核名称,主机名,内核版本号,内核版本,硬件名,处理器类型,硬件平台类型,操作系统名称

Operation demonstration:
Insert image description here

5、top

Syntax: top [parameter]
Function: Display relevant information about the process currently being executed by the system, including
common options such as process ID, memory usage, CPU usage, etc.:

-b 批处理
-c 显示完整的治命令
-I 忽略失效过程
-s 保密模式
-S 累积模式
-i<时间> 设置间隔时间
-u<用户名> 指定用户名
-p<进程号> 指定进程
-n<次数> 循环显示的次数

Operation demonstration:
Insert image description here

6. Shut down

Syntax: shutdown [options]
Common options:

-h : 将系统的服务停掉后,立即关机。
-r : 在将系统的服务停掉之后就重新启动
 -t(-tsec):后面加秒数,亦即『过几秒后关机』的意思

3. Permissions

1. User classification

There are two types of users under Linux: super user (root) and ordinary users.
The differences are:

  1. Super user: can do anything under the Linux system without restrictions
  2. Ordinary users: Do limited things under Linux.
  3. The command prompt of the super user is "#", and the command prompt of the ordinary user is "$"

Syntax: su [username]
Function: switch users.
Common operations:

whoami : 查看我是谁
su - : 切换root用户
su 名:切换其他用户
logout:退出root用户

Operation demonstration:
Insert image description here

2.Permission management

1. Classification of file visitors

Owners of files and file directories: u—User
Users of the group to which the owners of files and file directories belong: g—Group
Other users: o—Others

2. File type and access permissions (thing attributes)

Insert image description here

1. Type of file
d:文件夹
-:普通文件
l:软链接(类似Windows的快捷方式)
b:块设备文件(例如硬盘、光驱等)
p:管道文件
c:字符设备文件(例如屏幕等串口设备)
s:套接口文件
2. File permissions
r(读):Read对文件而言,具有读取文件内容的权限;对目录来说,具有浏览该目录信息的权限
w(写):Write对文件而言,具有修改文件内容的权限;对目录来说具有删除移动目录内文件的权限
x(执行):execute对文件而言,具有执行文件的权限;对目录来说,具有进入目录的权限
“—”表示不具有该项权限

3. Representation method of file permission value

1.Character representation
express illustrate
r - - read only
- w - just write
- - x Execute only
r w - Readable and writable
r - x Readable and executable
- w x Writable and executable
r w x Readable, writable and executable
- - - No permission
2. Octal numerical representation method
express Octal binary
r - - 4 1 0 0
- w - 2 0 1 0
- - x 1 0 0 1
r w - 6 1 1 0
r - x 5 1 0 1
- w x 3 0 1 1
r w x 7 1 1 1
- - - 0 0 0 0

4. Related setting methods for file access permissions

Syntax: chmod [parameter] permission file name
Function: set the access permission of the file
Common options:

R -> 递归修改目录文件的权限
+:向权限范围增加权限代号所表示的权限
-:向权限范围取消权限代号所表示的权限
=:向权限范围赋予权限代号所表示的权限
用户符号:   
u:拥有者
g:拥有者同组用
o:其它用户
a:所有用户

Note: Only the owner of the file and root can change the permissions of the file
. Operation demonstration:
Insert image description here

umask

Function: View or modify the file mask.
The default permission of our new folder is 0666, and the default permission of the new directory is 0777.
But in fact, the permissions you see for the files and directories you create are often not the above values. The reason is that when creating files or directories, they are also affected
by umask. Assuming that the default permission is mask, the actually created file permissions are: mask & ~umask
Syntax: umask permission value
Description: Final permission = default permission & (umask)
operation demonstration:
Insert image description here

3. Directory permissions

  1. Executable permissions: If the directory does not have executable permissions, you cannot cd into the directory
  2. Readable permissions: If the directory does not have readable permissions, you cannot use commands such as ls to view the contents of the files in the directory.
  3. Writable permissions: If a directory does not have writable permissions, files cannot be created in the directory and files cannot be deleted in the directory.

4. Sticky bit

Sticky bit (use chmod +t)
The directory created by the sticky bit can only be deleted by the super administrator or the owner of the directory and the owner of the file.
The sticky bit is to solve the problem when multiple users share a directory and can be deleted. The situation of other people’s files.

Guess you like

Origin blog.csdn.net/2301_76986069/article/details/132952233