Shell, Linux overview and command concise

Shell brief

Shell interprets the user's command, translates it into a voice that the system can understand, and hands it to the system kernel for processing.
Sheel is a special program running in the Linux system, acting as a "translator" between the user and the kernel. The
user logs in In the Linux system, automatically load a shell program
Bash is the shell program used by default in the Linux system (Bash is located in the file /Bin/bash) to
receive the command input by the user → interpret the command → pass it to the system kernel for execution

Linux command analysis

Internal command External instructions
Some special instructions integrated in the shell interpreter program, also called built-in or embedded instructions Script file or binary program that can complete specific functions in Linux system
Part of Shell Commands outside of the Shell interpreter program
There is no separate corresponding system file Each external command corresponds to a file in the system
Automatically load into memory and can be used directly The corresponding file location must be known, and it can be executed after being loaded by Shell. /bin;/usr/bin;/usr/local/bin; etc.

Linux command line format

General command line usage format
Command word [option] [parameter] (English characters are case sensitive)

Command word:
Function: confirm the operation
is the most critical part of the whole command; only confirm a command

Option
Function: Confirm the function.
Short format option: use "-" symbol to guide (letters), multiple single-character options can be combined for use.
Long format option: use "- -" symbol to guide (full word)

Parameter
Function: Determine the processing object of the command word. It
can be a file name, a directory (path) name, or a user name. The
number can be zero or more.

The auxiliary operation of editing Linux command line and the method of obtaining command help

command Description
Tab Autocomplete
Backslash "\" Force line break
Crrl + U Empty to the beginning of the line
Crrl + K Empty to the end of the line
Crrl + L Clear screen
Crrl+C Cancel this command edit
Crrl+a Switch to the beginning of the line
Crrl + e Switch to end of line

The internal command help
view the help information of the shell internal command

Command "-help" option is
used with most Linux external commands

Use the man command to read the manual page.
Use the "↑" and "↓" (Enter) arrow keys to scroll a line of text.
Use the Page Up and Page Down (space) keys to turn pages.
Press the Q or q key to exit the reading environment and
press the "/" key to find the content. N key to find down, N key to find up

Basic operation of Linux system

Command line prompt characters
[root@localhost ~]#
[the user currently logged in to the system@hostname current directory]#
# means login as an administrator
$ means login as an ordinary user

Switch user
su Username#The directory remains unchanged after the switch
su-Username#The directory after the switch becomes the user's home directory

View the full name of the current host
hostname

Temporarily set
hostname hostname hostname

Permanently set the
hostname hostnamectl set-hostname hostname
or
vi /etc/hostname (you need to restart the system after setting to take effect)
press i to delete the old hostname first, and then enter the new hostname (only the first line is valid)
press ESC , Then press SHIFT + ":" key combination, enter wq, and then press Enter to exit

View current system version information
cat /etc/redhat-release
or
cat /etc/*release

View the current kernel version
uname -r

Temporarily modify the network card IP
ifconfig ens33 192.168.80.3/24
or
ifconfig ens33 192.168.80.3 netmask 255.255.255.0

Permanently modify the network card IP
vi /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet #Set the network card type, "Ethernet" means Ethernet
DEVICE=ens33 #Set the name of the network card
ONBOOT=yes #Set whether the network card is in the Linux operating system Activate
BOOTPROTO=static at startup #Set the configuration method of the network card, "static" means to use a static IP address, "dhcp" means to obtain the address dynamically
IPADDR=192.168.80.3 #Set the IP address of the network card
NETMASK=255.255.255.0 #Set the network card Subnet mask
GATEWAY=192.168.80.2
#Set the default gateway address of the network card DNS1=192.168.80.2 #Set the IP address of the DNS server

Restart the network card
systemctl restart network
#Restart all network cards ifdown ens33; ifup ens33
ifdown ens33 #Close a network card
ifup ens33 #Start a network card

ifconfig ens33 down #temporarily disable a network card
ifconfig ens33 up #reactivate a network card (the IP address will not be updated)

Check the network card address
ifconfig
or
ip a

Add a static route entry
Temporarily add a route (restart the network service to fail)
Method 1:
route add -net 192.168.3.0/24 gw 192.168.8.2 [dev ens33]
-net: specify the address of the target network segment
gw: specify the next hop router IP address
dev: output interface designated for routing

View routing table entries
route -n

Delete the static route entry
route del -net 192.168.3.0/24

Method 2:
ip route add 192.168.15.0/24 via 192.168.80.2 [dev ens33]

Permanently add a route (restart the network service to take effect)
Method 1:
vi /etc/sysconfig/static-routes
any net any gw 192.168.80.2
any net 192.168.3.0/24 gw 192.168.80.2
any net 10.0.0.0 netmask 255.0.0.0 gw 192.168 .80.2
any host 192.168.100.100 gw 192.168.80.2

systemctl restart network

Method 2:
vim /etc/sysconfig/network-scripts/route-ens33
default via 192.168.80.2 dev ens33 #default route, another format 0.0.0.0/0 192.168.14.254 dev ens33
10.211.6.0/24 via 192.168.80.2 dev ens33
192.168.100.200 via 192.168.14.254 dev ens33

systemctl restart network

Test network connection
ping
traceroute (static tracking)
mtr (dynamic tracking)

Reboot the system
reboot
the init 6
the shutdown -r now

Shutdown
poweroff
the shutdown -h now
the init 0

Tips:
Add color to command line prompt characters
PS1="[\e[1;34m][\u@\h \W]\$[\e[0m]"

/etc/resolv.conf
is the DNS client configuration file, which is used to set the IP address and DNS domain name of the DNS server. Up to 3 different DNS server addresses can be specified, and the first DNS server will be used first. The changes made to this file will take effect immediately.
nameserver 192.168.80.2 #Define the IP address of the DNS server

Note: This configuration file has the same function as the DNS1 parameter of the network card configuration file, whichever configuration is last modified or restarted.

View and switch directories (pwd, cd, ls, du and - max-depth=n)

View the current working directory-pwd

Role: Display the current working directory location of the user

Switch working directory -cd

Role: change the user's working directory to another location

format Description
cd target location Switch to target location
cd ~ If you do not specify the target location, switch to the current user's home directory
cd If you do not specify the target location, switch to the current user's home directory
cd - Switch to the directory where you were before the last cd command was executed

Absolute, relative path

Absolute path: the path starting from the root directory
Relative path: the path starting from the current directory
Relative path expression

Manifestations Description Example
Use the directory name or file name directly Indicates the location of subdirectories and files in the current working directory grub.conf
Start with a period "." Indicates that the current working directory is used as the starting point ./grub.conf
Start with two periods "..." Indicates to take the upper level directory (parent directory) of the current directory as the starting point …/vmlinuz
Start with "~Username" Indicates that the home directory of the specified user is used as the starting point ~teacher
Start with "~" Indicates that the current user's home directory is used as the starting point ~

List the contents of the directory -ls

Function: Display the contents of the directory, including subdirectories and file-related attribute information
ls [Option] [File or directory...]

Common options

Options Description
-l Display the list of files and directories in long format (long), including detailed information such as permissions, sizes, and last update time. The ll command has the same function as ls-l
-a Display information about all (all) subdirectories and files, including hidden directories and hidden files whose names start with a dot "."
-A It is basically similar to the function selected by -a, but there are two special hidden directories that will not be displayed, that is, "." for the current directory and "..." for the parent directory.
-d Display the attributes of the directory (dircctory) itself, instead of displaying the contents of the directory
-h Display the size of directories or files in a more human way. The default size unit is (B), and it will be displayed as KB, MB, etc. after use. This option needs to be used in conjunction with the -l option
-R Display all contents in the specified directory and its subdirectories in a recursive manner
- -color In the character mode, different files are distinguished by color. Turned on by default
-i Display the inode numbers of files and directories

用ls-l命令查看某一个目录会得到一个7个字段的列表
第1字段:文件属性字
第一个字符代表文件的类型:
“-”表示该文件是一个普通文件
“d”表示该文件是一个目录
“l”表示该文件是一个软链接文件。类似windows的快捷方式

第一字符的后面9个字母表示该文件或目录的权限位:
r:读(查看、下载)
w:写(添加、更改、删除、上传)
x:执行(运行文件、切换目录)
前三个表示文件拥有者(属主)的权限
中间三个表示文件所属组拥有的权限
最后三个表示其他用户拥有的权限

第2字段:文件硬连接数
如果一个文件不是目录,此时这一字段表示这个文件所具有的硬链接数
如果是一个目录,则第2字段表示该目录所含子目录的个数
如果新建一个空目录,这个目录的第二字段就是2,表示该目录下有两个子目录

第3字段:文件、目录拥有者

第4字段:文件、目录拥有者所在的组

第5字段:文件所占用的空间(默认以字节为单位)
如果是一个目录,则表示该目录的大小,而不是目录以及它下面的文件总大小

第6段:文件、目录最近访问或修改时间

第7段:文件名
如果是一个符号链接(软链接文件),那么会有一个“->”符号,后面跟一个它指向的文件名

结合通配符

“?”:代表任意一个字符,有且一个字符
“*”:代表任意数个字符,可以是0个或者1个或者多个
“[]”:代表可以匹配字符组中的任意一个字符

设置别名-alias

作用:通过别名机制简化常用的、比较长的命令
alias命令别名=‘命令’。“=”两边不能有空格
例:alias myls=‘ls-alh’ 别名是myls
unalias取消别名

统计目录及文件空间占用情况-du

作用:统计指定目录(或文件)所占用磁盘空间的大小
du [选项] [文件或目录…]

常用选项

选项 说明
-a 统计磁盘空间占用时包括所有的文件,而 不仅仅只统计目录
-h 以更人性化的方式(默认以KB计数,但不显示单位)显示出统计结果
-s 只统计所占用空间总的大小,而不是统计每个子目录、文件的大小
- -max-depth=n 统计所有小于等于第n层的目录的总大小

例:统计出/var/log目录中子目录的大小
du -h - -max-depth=1/var/log

创建目录和文件(mkdir、touch、ln)

创建新目录-mkdir

作用:创建新的空目录
mdir [选项] 目录位置及名称…
-p:一次性创建嵌套的多层目录
例:
在当前目录下创建名为public_html的子目录
mkdir public_html
创建目录/aa,在/aa目录下创建子目录bb,在/aa/bb目录下创建子目录cc
mkdir -p/aa/bb/cc

创建空文件-touch命令

作用:更新文件的时间标记
常用于创建多个新的空文件
touch 文件…
示例
touch aa bb
touch {a,b}.txt

创建链接文件-ln

作用:为文件或目录建立链接文件,类似于windows系统的快捷方式

链接文件类型

软链接(又称为符合链接)
硬链接
in [-s]源文件或目录… 链接文件或目标位置

示例
ln -s/bin/bzip2/bin/wx/

软、硬链接示例图

在这里插入图片描述
在linux中,文件名和文件的数据是分开存储的
在linux中,只有文件的硬链接数=0时才会被删除
在linux中,可以使用ls-l查看一个文件的硬链接数量
在linux中,当一个文件拥有多个硬链接时,对文件内容修改,会影响到所有文件名。但是删除一个文件名,不影响别的文件名访问

软、硬链接对比

文件类型 软链接文件 硬链接文件
删除源文件后 失效 任然可用(文件占用空间与源文件相同)
使用范围 适用于文件或目录 仅适用于文件
保存位置 与源文件可在不同的文件系统中 必须与源文件在同一个文件系统(同一个分区)中
与源文件的关系 相当于快捷方式 相当于给文件起个别名
inode号码 inode号码不同 inode号码相同

复制、删除、移动目录和文件(cp、rm、mv)

复制文件或目录-cp

作用:将需要复制的文件或目录(源)重建一份,并保存为新的文件或目录
cp [选项] 源文件或源目录…目标文件或目录

常用选项

选项 说明
-f 覆盖目标同名文件或目录时不进行提现,而直接强制复制
-i 覆盖目标同名文件或目录时提醒用户确认
-p 复制时保持源文件的权限、属主及时间标记等属性不变
-r 复制目录时必须使用此选项,表示递归复制所有文件及目录

当复制多个文件或目录时,目标位置必须是目录,且目标目录必须已存在

删除文件或目录-rm

作用:删除指定的文件或目录

常用选项

选项 说明
-f 删除文件或目录时不进行提醒,而直接强制删除
-i 删除文件或目录时提醒用户确认。
-r 删除目录时必须使用此选项,表示递归删除整个目录树

不能直接删除系统中已有的目录或配置文件,避免出现意外故障

移动文件或目录-mv

作用:将指定的文件或目录转移位置
如果目标位置与原位置相同,则相当于执行重命名操作
mv [选项] …源文件或目录…目标文件或目录

mv命令只能对单个文件重命名,可使用rename命令批量修改文件名
rename 旧字符 新字符 目标文件

查找目录和文件(which、find)

查找命令文件存放目录-which

搜索范围由环境变量PATH决定(echo $PATH)
which 命令|程序名(默认当找到第一个目标后不再继续查找)
which -a 命令|程序名(在所有搜索路径中查找)

当使用which查找内部命令时,将找不到对应的程序

查找文件或目录-find

作用:采用递归方式,根据目标的名称、类型、大小等不同属性进行精细查找
find [查找范围] [查找条件表达式]
[查找范围]:查找文件或子目录的目录位置
[查找条件表达式]:查找条件类型

常用查找条件类型

查找类型 关键字 说明
按名称查找 -name 根据目标文件的名称精细查找,允许使用"*“及”?"通配符
按文件大小查找 -size 根据目标文件的大小进行查找
一般使用"+"、"-"号设置超过或小于指定的大小作为查找条件
常用的容量单位包括kB(k为小写)、MB、GB
按文件属主查找 -user 根据文件是否属于目标用户进行查找
按文件类型查找 -type 根据文件的类型进行查找
文件类型包括普通文件(f)、目录(d)、块设备文件(b)、字符设备文件(c)等
块设备文件(b):成块读取数据的设备(硬盘,内存,光驱)
字符设备文件(c):按单个字符读取数据的设备(键盘、鼠标)

使用find命令实现多个查找条件

各表达式之间使用逻辑运算符
"-a"表示而且(and)
“-o”表示或者(or)
例:
find/boot -size +1k -a -name “g*”
find/boot -size +1k -o -name “g*”

find的exec用法

-exec参数后面跟的是linux命令,它是以分号“;”为结束标志,由于各个系统中分号会有不同的意义,因此在分号前面加上反斜杠转义符“\”
{}代表前面find查找出来的文件名
例:
find ./-type f -exec ls -l {} ;
find命令匹配到了当前目录下的所有普通文件,并在-exec选项中使用了ls -l命令将他们列出

命令执行的优先级

第一优先级:指定路径的命令。绝对路径/root/pwd.sh或者相对路径 ./pwd.sh
第二优先级:别名指定的命令 alias pwd=/root/pwd.sh
第三优先级:内部命令
第四优先级:hash命令
第五优先级:通过PATH定义的查找顺序查找
如果以上顺序都找不到,则会包“会找到命令…”的错误

hash(哈希表)

linux系统下会有一个hash表,当你刚开机时这个hash表为空,每当你执行过一条命令时,hash表记录下这条命令的路径,就相当于缓存一样。第一次执行命令shell解释器默认的会从PATH路径下寻找该命令的路径,当你第二次使用该命令时,shell解释器首先会查看hash表,没有该命令才会去PATH路径下寻找。
作用:hash表能提高命令的调用速率

Guess you like

Origin blog.csdn.net/TaKe___Easy/article/details/113089629