一般命令格式
命令 [
选项
] [
参数
....]
在
linux
中命令分为内部命令和外部命令。
查看帮助的命令
在使用命令之前,我们可以通过帮助来查看这个命令的格式和使用方式。在
Linux
中有两个帮助命令, help 和
man
help
例如
cd
这个命令我只知道命令的名称,不知道的使用方式。此时就可以使用
help
来查看,使用的格式为:
# help 命令名称
[root@bogon ~]# help cd
cd: cd [-L|[-P [-e]] [-@]] [dir]
Change the shell working directory.
Change the current directory to DIR. The default DIR is the value of the
HOME shell variable.
# 还可以使用 --help 的方式来查看帮助
[root@bogon ~]# ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.
man
man
是
manual
(手册)的简写。它的使用方式为:
man 命令名称
例如我们通过
man
来查看
ls
命令的使用:
# man ls
LS(1) User Commands
LS(1)
NAME
ls - list directory contents
SYNOPSIS
ls [OPTION]... [FILE]...
说明:进入这个使用的帮助信息后,按向下箭头可以向下滚动一行,按向上箭头可以往前滚动一
行。按空格键可以向下翻页,按
q
键退出帮助。
用户切换 su
用户切换需要使用 su 命令来实现它的使用格式为:
[root@bogon ~]# su --help
Usage:
su [options] [-] [<user> [<argument>...]]
由于我们在安装
Redhat
时已经添加了一个叫
redhat
的普通用户。我们再在就在
root
用户和
redhat
这 两个用户之间来进行切换。
1. 从root 用户切换到 redhat 普通用户
[root@bogon ~]# su -l redhat
[redhat@bogon ~]$
[root@bogon ~]# su redhat
[redhat@bogon root]$
在
Linux
中执行后没有信息就是最好的消息
@
前面表示当前登录用户的名称,而
@
后面表示主机名称
切换成功后,命令提示符发生以下的变化:
1.
由
[root@bogon]
变为了
[redhat@bogon]
2.
中括号后的
#
号变为了
$
符号(
#
表示超级用户的提示符,而
$
表示是普通用户的命令提示
符)
2. 再从普通用户 redhat 切换到超级用户 root 中
[redhat@bogon ~]$ su -l root
Password:
[root@bogon ~]#
当从普通用户切换到超级用户时,要求输入超级用户的登录密码,在输入密码过程中
不会有任何的提示信息,我们输入正确的密码即可。
注意:在切换用户时带有
-l
选项和不带
-l
选项的区别是带了后在切换用户的同时也会切换用户 的环境,否则不会切换用户环境只会切换用户
[redhat@bogon ~]$ su -l
Password:
[root@bogon ~]#
# 注意:如果 su 后面不带用户名,默认就会切换到 root 用户
exit 命令来退出当前用户的登录状态。
[root@bogon ~]# exit
logout
[redhat@bogon ~]$
获取当前路径 pwd
我们可以使用
pwd
命令来获取当前用户所在路径
[root@bogon ~]# pwd
/root
[root@bogon ~]# cd /home/redhat
[root@bogon redhat]# pwd
/home/redhat
查看文件 ls
命令格式:
[root@bogon redhat]# ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified
ls
命令是
List
的简写,它的作用是显示指定路径下的所有的文件信息。它常用的选项有如下:
-a 用于显示所有文件包括隐藏文件及目录-d 显示目录本身而不是目录下的内容-l 以长列表的方式来显示内容-h 以人的读取习惯显示内容-r 递归显示
-a 选项
[root@bogon redhat]# ls -a /
. afs bin dev home lib64 mnt proc run srv tmp var
.. .bash_history boot etc lib media opt root sbin sys usr
-d 选择
只显示目录本身,而不会显示目录内容
[root@bogon redhat]# ls /usr
bin games include lib lib64 libexec local sbin share src tmp
[root@bogon redhat]# ls -d /usr
/usr
-l 选项
它是以长列表的方式来显示内容
[root@bogon redhat]# ls /usr
bin games include lib lib64 libexec local sbin share src tmp
# 下面是带了 -l 选项的
[root@bogon redhat]# ls -l /usr
total 240
dr-xr-xr-x. 2 root root 45056 Oct 15 19:40 bin
drwxr-xr-x. 2 root root 6 Aug 10 2021 games
drwxr-xr-x. 3 root root 23 Oct 15 19:38 include
dr-xr-xr-x. 38 root root 4096 Oct 15 19:42 lib
dr-xr-xr-x. 123 root root 73728 Oct 15 19:42 lib64
drwxr-xr-x. 46 root root 12288 Oct 15 19:40 libexec
drwxr-xr-x. 12 root root 131 Oct 15 19:36 local
dr-xr-xr-x. 2 root root 20480 Oct 15 19:40 sbin
drwxr-xr-x. 213 root root 8192 Oct 15 19:40 share
drwxr-xr-x. 4 root root 34 Oct 15 19:36 src
lrwxrwxrwx. 1 root root 10 Aug 10 2021 tmp -> ../var/tmp
-h 选项
[root@bogon redhat]# ls -l -h /usr
total 240K
dr-xr-xr-x. 2 root root 44K Oct 15 19:40 bin
drwxr-xr-x. 2 root root 6 Aug 10 2021 games
drwxr-xr-x. 3 root root 23 Oct 15 19:38 include
dr-xr-xr-x. 38 root root 4.0K Oct 15 19:42 lib
dr-xr-xr-x. 123 root root 72K Oct 15 19:42 lib64
drwxr-xr-x. 46 root root 12K Oct 15 19:40 libexec
drwxr-xr-x. 12 root root 131 Oct 15 19:36 local
dr-xr-xr-x. 2 root root 20K Oct 15 19:40 sbin
drwxr-xr-x. 213 root root 8.0K Oct 15 19:40 share
drwxr-xr-x. 4 root root 34 Oct 15 19:36 src
lrwxrwxrwx. 1 root root 10 Aug 10 2021 tmp -> ../var/tmp
# 我们可以在命令后面跟多个选项,多个选项之间用空格分隔,当然也可以把选项进行组全,例如:
[root@bogon redhat]# ls -lh /usr
total 240K
dr-xr-xr-x. 2 root root 44K Oct 15 19:40 bin
drwxr-xr-x. 2 root root 6 Aug 10 2021 games
drwxr-xr-x. 3 root root 23 Oct 15 19:38 include
dr-xr-xr-x. 38 root root 4.0K Oct 15 19:42 lib
dr-xr-xr-x. 123 root root 72K Oct 15 19:42 lib64
drwxr-xr-x. 46 root root 12K Oct 15 19:40 libexec
drwxr-xr-x. 12 root root 131 Oct 15 19:36 local
dr-xr-xr-x. 2 root root 20K Oct 15 19:40 sbin
drwxr-xr-x. 213 root root 8.0K Oct 15 19:40 share
drwxr-xr-x. 4 root root 34 Oct 15 19:36 src
lrwxrwxrwx. 1 root root 10 Aug 10 2021 tmp -> ../var/tmp
创建目录 mkdir
在 linux 中创建目录需要使用 mkdir 命令。命令的格式为:
mkdir [OPTION]... DIRECTORY...
使用示例:
[root@bogon ~]# mkdir /root/dir1
[root@bogon ~]# mkdir dir2
[root@bogon ~]# mkdir dir3/dir4/dir5
mkdir: cannot create directory ‘dir3/dir4/dir5’: No such file or directory
[root@bogon ~]# mkdir -p dir3/dir4/dir5 # 使用 -p 选项可以创建多级目录
[root@bogon ~]# mkdir -v dir4 # 使用 -v 选项可以在创建时显示提示信息
mkdir: created directory 'dir4'
# 可以同时创建多个目录
[root@bogon ~]# mkdir -pv dir11 dir12 dir13 dir14
mkdir: created directory 'dir11'
mkdir: created directory 'dir12'
mkdir: created directory 'dir13'
mkdir: created directory 'dir14'
# 创建多个目录的另一种写法
[root@bogon ~]# mkdir -pv dir{40..45}
mkdir: created directory 'dir40'
mkdir: created directory 'dir41'
mkdir: created directory 'dir42'
mkdir: created directory 'dir43'
mkdir: created directory 'dir44'
mkdir: created directory 'dir45'
# 创建多个目录的再一种方式
[root@bogon ~]# mkdir -pv my{6,8,10}
mkdir: created directory 'my6'
mkdir: created directory 'my8'
mkdir: created directory 'my10'
删除目录 rm
删除目录要使用 rm 命令,它的语法格式为:
rm [OPTION]... [FILE]...
选项说明:1. -f :表示强制删除2. -r :表示递归删除,一般在删除目录时需要指定这个选项3. -d :表示删除空的目录4. -v :表示删除时显示详细的过程
使用示例:
# 当我们删除一个目录时,它会提示不能删除
[root@bogon ~]# rm show{s,
rm: cannot remove 'show{s,': Is a directory
# 使用 -d 选项来删除一个空目录
[root@bogon ~]# rm -d show{s,
rm: remove directory 'show{s,'? y # 输入 y 来确认删除,如果输入 n 表示不删除
# -f 选项在删除时不会再有确认提示,而是直接删除,所以在使用这个选项时要小心
[root@bogon ~]# rm -df y, # 注意这里的逗号是文件名称的一部分,不是输入的逗号
# 删除多个空目录,多个目录之间用空格隔开
[root@bogon ~]# rm -fd t} my10
# 删除多个目录的另一种写法
[root@bogon ~]# rm -d dir{11..14}
rm: remove directory 'dir11'? y
rm: remove directory 'dir12'? y
rm: remove directory 'dir13'? y
rm: remove directory 'dir14'? y
# 删除多个目录的再一种写法
[root@bogon ~]# rm -df dir[3-4]
rm: cannot remove 'dir3': Directory not empty
[root@bogon ~]# rm -df dir[1-2]
# 在上面执行删除 dir3 时,报不能删除非空目录的错误,我们来查看这个目录是否为非空目录
[root@bogon ~]# ls dir3
dir4
[root@bogon ~]# tree dir3
dir3
└── dir4
└── dir5
# 通过上面的命令,我们发现 dir3 这个目录确实不是空目录。对于 rm 这个命令来说,默认只能删除
空目录,不能删除非空目录,要想删除非空目录,我们需要带上 -r 选项来进行递归删除。
[root@bogon ~]# rm -rf dir3 # 强制递归删除 dir3 目录及其内容
# 批量删除,使用通配符 * 来表示删除所有能匹配上的目录
[root@bogon ~]# rm -df dir*
[root@bogon ~]# ls
anaconda-ks.cfg my8
data node_exporter-1.6.1.linuxamd64.tar.gz
Desktop nohup.out
Documents Pictures
Downloads prometheus-2.47.2.linux-amd64.tar.gz
grafana-enterprise-10.2.0-1.x86_64.rpm Public
Music Templates
my6 Videos
查看文件
cat
这个命令的作用是查看文件的所有内容。它的语法格式为:
cat [OPTION]... [FILE]...
选项说明:-b 作用显示没有空行的行号-n 显示行号
命令使用示例:
# cat 命令是把一个文件中的内容全部输出,一般使用这个命令来显示小内容的文件。
[root@bogon ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
# -A 选项,会在末尾显示 $ 符号
[root@bogon ~]# cat -A /etc/passwd
root:x:0:0:root:/root:/bin/bash$
bin:x:1:1:bin:/bin:/sbin/nologin$
daemon:x:2:2:daemon:/sbin:/sbin/nologin$
# -n 选项,为每一行添加行号
[root@bogon ~]# cat -n /etc/passwd
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
head
这个命令用于显示一个文件的前面
10
行数据,它的语法格式为:
head [OPTION]... [FILE]...
选项说明:-c :输出每行的第一个字符-n :输出指定的行数,默认为 10 行
使用示例:
# 使用 head 命令来查看文件前 10 行数据
[root@bogon ~]# head /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
# 使用 -c 选项来查看指定文件的前面指定的几个字符。注意:如果 -c 后面的数字是负数,那么会从
后向前截取。
[root@bogon ~]# head -c 5 /etc/passwd # 查看前面 5 个字符
root:
[root@bogon ~]# head -c 2 /etc/passwd # 查看前面 2 个字符
# 使用 -n 选项用于显示指定的行数,默认不指定时是 10 行。注意:如果 -n 选项后面的数字是负
数,则会把文件最后指定行去掉后再显示
[root@bogon ~]# head -n 2 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
tail
这个命令用于显示文件的最后 10 行数据。它的语法格式为:
tail [OPTION]... [FILE]...
选项说明:-c :显示指定字符-n :显示指定行数-f :显示追加内容
使用示例:
# 显示文件最后 10 行数据
[root@bogon ~]# tail /etc/passwd
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
cockpit-ws:x:983:982:User for cockpit web service:/nonexisting:/sbin/nologin
cockpit-wsinstance:x:982:981:User for cockpit-ws
instances:/nonexisting:/sbin/nologin
gnome-initial-setup:x:981:980::/run/gnome-initial-setup/:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/usr/share/empty.sshd:/sbin/nologin
chrony:x:980:978:chrony system user:/var/lib/chrony:/sbin/nologin
dnsmasq:x:979:977:Dnsmasq DHCP and DNS server:/var/lib/dnsmasq:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
redhat:x:1000:1000:redhat:/home/redhat:/bin/bash
# 显示文件最后 3 行数据,这就需要使用 -n 选项
[root@bogon ~]# tail -n 3 /etc/passwd
dnsmasq:x:979:977:Dnsmasq DHCP and DNS server:/var/lib/dnsmasq:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
redhat:x:1000:1000:redhat:/home/redhat:/bin/bash
# 在 tail 命令中,指定负数的行数无效,它仍然显示最后指定的行数
[root@bogon ~]# tail -n -3 /etc/passwd
dnsmasq:x:979:977:Dnsmasq DHCP and DNS server:/var/lib/dnsmasq:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
redhat:x:1000:1000:redhat:/home/redhat:/bin/bash
# -n 选项可以省略
[root@bogon ~]# tail -3 /etc/passwd
dnsmasq:x:979:977:Dnsmasq DHCP and DNS server:/var/lib/dnsmasq:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
redhat:x:1000:1000:redhat:/home/redhat:/bin/bash
# -f 选项,用于获取文件中最新的数据,当使用这个选项后,文件会处于阻塞状态,要退出按 ctrl +C
[root@bogon ~]# tail -f file1
注意:如果指定的行数是正数,则 -n 选项需要带上;如果指定的行数是负数,则 -n 选项可以省略,行数前面的负号就等价于 -n
more(了解)
这个命令用于分页显示文件中的内容。它的语法格式为:
more [options] <file>...
使用示例:
[root@bogon ~]# cp /etc/passwd .
[root@bogon ~]# more passwd
# 空格向下翻页,q 退出
注意:这个命令不能向前翻页。如果希望能够向前翻页,那么我们需要使用 less 命令
过滤文件内容 grep
要实现这个功能,我们需要使用 grep 命令。它的语法格式:
grep [OPTION]... PATTERNS [FILE]...
# 说明:PATTERNS 表示是的要查找的关键字,严格来说应该是要查找的匹配字符
这个命令的功能是在指定的普通文件中查找并显示含有特定字符的行,也可以与管道符一起使用。
选项说明:-c :仅显示找到的行数-i :忽略大小写-n :显示行号-v :反向选择,它只会列出没有关键字的行-A : -A 2 搜索时显示匹配到的那一行以及以下 2 行-B : -B 2 搜索时显示匹配到的那一行以及以上 2 行-C : -C 2 搜索时显示匹配到的那一行以及以上下 2 行
匹配模式 说明grep h 文件名 查找文件里有字符 h 的行grep ^[q] 文件名 匹配以 q 开头的行grep ^[qf] 文件名 匹配以 q 或者 f 开头行grep ^[^qf] 文件名 匹配以 q 或者 f 开头以外的行grep ^[0-9] 文件名 匹配以数字开头的行grep h$ 文件名称 匹配以 h 结尾的所有行grep ^h$ 文件名称 匹配只有 h 的行
使用示例:
文件内容统计 wc
如果希望统计一个文件中的字符个数,我们需要使用
wc
命令。它的语法格式:
wc [OPTION]... [FILE]...
# 或者
wc [OPTION]... --files0-from=F
选项说明:-c :统计文件大小-m :统计字符个数-w :统计单词个数-l :统计行数
切割显示 cut
切割显示要使用 cut 命令,它的作用是按列提取文本内容。它的语法为:
cut OPTION... [FILE]...
选项说明:-d :指定分隔符,如果是空格需要使用引号引起来-f :指定第几个字段-c :指定获取第几个字符
排序显示 sort
在
Linux
中可以使用
sort
命令来对文本内容进行排序显示,它的语法为:
sort [OPTION]... [FILE]...
# 或者
sort [OPTION]... --files0-from=F
选项说明:-b :忽略缩进与空格-f :忽略大小写-n :以数值型排序-r :反向排序-k :指定字段范围-t :指定间隔符-u :去除重复行
去重显示 uniq
使用 uniq 命令可以用于去除文本中连续的重复行,语法格式为:
uniq [OPTION]... [INPUT [OUTPUT]]
选项说明:-c :可以统计重复行出现的次数