9 Linux实操篇-实用指令

9 Linux实操篇-实用指令

  1. 《鸟哥的Linux私房菜 基础学习篇 第四版》1
  2. 《鸟哥的Linux私房菜 服务器架设篇 第三版》2
  3. 《韩顺平_2021图解Linux全面升级》3

本章将介绍在Linux会大量使用的一些常见指令。

9.1 指定和修改运行级别-init/systemctl

表9-1 Linux系统的运行级别
级别 描述
0 停机状态。系统默认运行级别不能设置为0,否则系统不能正常启动。
1 单用户状态。此状态仅 root 用户可登录,用于系统维护,禁止远程登录,相当于 Windows 下的安全模式。
2【少见】 多用户状态(无NFS)。没有网络服务。
3【最常见】 完整的多用户状态(有NFS)。有网络服务,登录后进入控制台命令行模式。
4【少见】 系统未使用,保留一般不用,在一些特殊情况下可以用它来做一些事情。
例如在笔记本电脑的电池用尽时,可以切换到这个模式来做一些设置。
5【常见】 图形化模式,登陆后进入图形GUI模式或GNOME、KDE图形化界面,如X Window系统。
6 系统正常关闭并重启。默认运行级别不能设为6,否则不能正常启动,而是会一直开机重启开机重启。

  本节介绍如何“指定当前运行级别”、“修改默认运行级别”,在第7章最后已经简单的介绍了Linux系统的7种运行级别(如上表),那如何“指定当前运行级别”、甚至“修改默认运行级别”呢?如下:
1. 指定当前运行级别

init 0/1/2/3/4/5/6  # 可以直接跳转到指定的运行级别
init 0              # 关机

注意上述运行级别的更改只是一次性的,重启后便会回到最初默认的运行级别。并且,更改运行级别到3或5后,Linux系统会自动注销登录,需要重新登录账户。

2. 修改默认运行级别
在CentOS7以前,如果要指定默认的运行级别,就需要对/etc/inittab文件中相应的语句进行修改;而在CentOS7及其以后的版本则做出了简化,可以直接使用指令进行更改:

# 运行级别代称
multi-user.target       # 运行级别3代称
graphical.target        # 运行级别5代称

# 查看当前的默认运行级别
systemctl get-default

# 修改默认运行级别
systemctl set-default [TARGET].target
# 下面是两个例子
systemctl set-default multi-user.target # 修改默认运行级别为3
systemctl set-default graphical.target  # 修改默认运行级别为5

注意在修改默认运行级别之后,需要重启才能生效。工作中常用运行级别3。

下面展示两个应用实例:
【案例1】在VMware内的终端上,使用init将运行级别从5设置为3,然后重启。

# 在图形界面打开终端
init 3

# 系统会变成终端模式,需要重新登录账户,如下
CentOS76 login: root
Password:
Laset Login: Sun Jul 16 17:42:05 on :0
[root@CentOS76 ~]# init 6

【案例2】修改默认级别为3,并重启观察效果:

[root@CentOS76 home]# systemctl set-default multi-user.target
[root@CentOS76 home]# init 6
# 重启后再将默认运行级别修改回5即可。

9.2 找回root密码

  本节主要就介绍一个面试题:如何找回/重置root密码?由于篇幅较长,故可以参考CSDN博文“linux找回root密码(CentOS7.6)”。下面是两点需要注意的地方:

  1. 不同版本找回密码的方式有一些小区别,博文中演示的CentOS 7.6系统。
  2. 上述找回密码的过程中进入了运行级别1,也就是“单用户模式”。

9.3 Linux的指令说明

本节就以指令ls为例,介绍一下Linux下的指令的一般格式。一般来说,Linux下的指令都具有自己的“选项”,以完成各种各样的复杂功能,如下ls的指令格式:

# 基本语法-ls
ls [选项] [路径]   # 显示指定目录下的所有文件
ls                # 默认显示当前路径下的文件

# 常用选项
-a  列出所有文件,包括以 “.”() 开头的隐含文件
-l  单列输出,信息是最详细的

下面说一下这些“选项”的注意点:

  1. 在介绍一个Linux指令时会列出“基本语法”和“常用选项”。注意在“基本语法”中,用方括号“[]”括起来的内容可以省略不写,其余的必须要写。
  2. 选项可以组合使用,并且这些选项的顺序无所谓,比如ls -la或者ls -al均可。甚至路径和配置选项之间也可以交换顺序,如ls [路径] -la,但若没有特殊原因一般不这么做。
  3. 在Linux下,隐藏文件是以“ . ”(点)开头。

注:下面的介绍中将不会重复这些内容,但这里的说明适用于每一条Linux指令。

下面展示四个应用实例:
【案例1】列出当前文件夹下的所有文件,包括以 “.”(点) 开头的隐含文件。

[root@CentOS76 ~]# ls -a
.                .bash_logout   .config    .ICEauthority         .tcshrc      模板  下载
..               .bash_profile  .cshrc     initial-setup-ks.cfg  .viminfo     视频  音乐
anaconda-ks.cfg  .bashrc        .dbus      .lesshst              .Xauthority  图片  桌面
.bash_history    .cache         .esd_auth  .local                公共         文档

【案例2】使用ls指令“单列输出”当前文件夹下的所有文件。

[root@CentOS76 ~]# ls -l
总用量 40
-rw-------. 1 root root 1883 713 17:12 anaconda-ks.cfg
-rw-r--r--. 1 root root 1931 713 17:13 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 4096 713 19:44 公共
drwxr-xr-x. 2 root root 4096 713 19:44 模板
drwxr-xr-x. 2 root root 4096 713 19:44 视频
drwxr-xr-x. 2 root root 4096 713 19:44 图片
drwxr-xr-x. 2 root root 4096 713 19:44 文档
drwxr-xr-x. 2 root root 4096 713 19:44 下载
drwxr-xr-x. 2 root root 4096 713 19:44 音乐
drwxr-xr-x. 2 root root 4096 715 10:12 桌面

【案例3】练习组合使用ls的选项。

[root@CentOS76 ~]# ls -la       # 这个指令等价于 ls -al
总用量 112
dr-xr-x---. 14 root root 4096 717 11:51 .
dr-xr-xr-x. 18 root root 4096 717 11:49 ..
-rw-------.  1 root root 1883 713 17:12 anaconda-ks.cfg
-rw-------.  1 root root 3229 717 11:51 .bash_history
-rw-r--r--.  1 root root   18 1229 2013 .bash_logout
-rw-r--r--.  1 root root  176 1229 2013 .bash_profile
-rw-r--r--.  1 root root  176 1229 2013 .bashrc
drwx------. 15 root root 4096 717 11:50 .cache
drwxr-xr-x. 16 root root 4096 714 10:03 .config
-rw-r--r--.  1 root root  100 1229 2013 .cshrc
drwx------.  3 root root 4096 713 17:13 .dbus
-rw-------.  1 root root   16 713 19:44 .esd_auth
-rw-------.  1 root root 4034 717 11:50 .ICEauthority
-rw-r--r--.  1 root root 1931 713 17:13 initial-setup-ks.cfg
-rw-------.  1 root root  473 717 15:53 .lesshst
drwx------.  3 root root 4096 713 19:44 .local
-rw-r--r--.  1 root root  129 1229 2013 .tcshrc
-rw-------.  1 root root 4631 716 16:27 .viminfo
-rw-------.  1 root root  108 717 11:51 .Xauthority
drwxr-xr-x.  2 root root 4096 713 19:44 公共
drwxr-xr-x.  2 root root 4096 713 19:44 模板
drwxr-xr-x.  2 root root 4096 713 19:44 视频
drwxr-xr-x.  2 root root 4096 713 19:44 图片
drwxr-xr-x.  2 root root 4096 713 19:44 文档
drwxr-xr-x.  2 root root 4096 713 19:44 下载
drwxr-xr-x.  2 root root 4096 713 19:44 音乐
drwxr-xr-x.  2 root root 4096 715 10:12 桌面

【案例4】指定显示/root目录下的所有文件。

[root@CentOS76 home]# ls -al /root      # 等价于 ls /root -al
总用量 120
dr-xr-x---. 14 root root 4096 718 17:21 .
dr-xr-xr-x. 18 root root 4096 717 11:49 ..
-rw-------.  1 root root 1883 713 17:12 anaconda-ks.cfg
-rw-------.  1 root root 5922 1111 2021 .bash_history
-rw-r--r--.  1 root root   18 1229 2013 .bash_logout
-rw-r--r--.  1 root root  176 1229 2013 .bash_profile
-rw-r--r--.  1 root root  176 1229 2013 .bashrc
drwx------. 15 root root 4096 717 11:50 .cache
drwxr-xr-x. 16 root root 4096 714 10:03 .config
-rw-r--r--.  1 root root  100 1229 2013 .cshrc
drwx------.  3 root root 4096 713 17:13 .dbus
-rw-------.  1 root root   16 713 19:44 .esd_auth
-rw-------.  1 root root 4348 718 11:26 .ICEauthority
-rw-r--r--.  1 root root 1931 713 17:13 initial-setup-ks.cfg
-rw-------.  1 root root  481 717 19:40 .lesshst
drwx------.  3 root root 4096 713 19:44 .local
-rw-r--r--.  1 root root  129 1229 2013 .tcshrc
-rw-------.  1 root root 4825 717 17:46 .viminfo
-rw-------.  1 root root  108 718 15:26 .Xauthority
drwxr-xr-x.  2 root root 4096 713 19:44 公共
drwxr-xr-x.  2 root root 4096 713 19:44 模板
drwxr-xr-x.  2 root root 4096 713 19:44 视频
drwxr-xr-x.  2 root root 4096 713 19:44 图片
drwxr-xr-x.  2 root root 4096 713 19:44 文档
drwxr-xr-x.  2 root root 4096 713 19:44 下载
drwxr-xr-x.  2 root root 4096 713 19:44 音乐
drwxr-xr-x.  2 root root 4096 715 10:12 桌面

9.3 帮助类-man/help

本节来介绍Linux下的帮助指令,主要有以下两个:manhelp

man             获得任意指令的帮助信息,部分做出了汉化。
help            只能获得shell内置命令的帮助信息,一般使用英文描述。
指令 --help     # 个人发现使用此格式一般也可以获取指令的帮助文档

一般在Linux下打开的帮助文档等文档,在界面内都会有相应的提示该如何执行翻页、查找等操作,基本上和之前学习的vi/vim相同,一般情况下如下:

# 帮助文档的查看
空格键      下一页
上/下键     换行
回车键      换行
g          回到文档头
G          回到文档尾
q          退出文档
# 注:Linux文档内禁止使用鼠标滚轮,只有在Xshell窗口才能使用鼠标滚轮。

当然Linux在中国发展多年,中文文档不计其数,实在不会了也可以“百度一下”。

1. man指令

# 基本语法-man
man 命令或配置文件    # 获得帮助信息

比如要查询ls指令的帮助文档就是man ls

2. help指令

# 基本语法-help
help 命令       # 获得shell内置命令的帮助信息

只能获得shell内置命令的帮助信息,一般使用英文描述。注意这里的“Shell 内置命令”,就是由 Bash Shell 自身提供的命令,而不是文件系统中的可执行脚本文件,使用type可以确定一个命令是否是内置命令,如:

[root@CentOS76 ~]# type cd
cd 是 shell 内嵌
[root@CentOS76 ~]# type ls
ls`ls --color=auto' 的别名
[root@CentOS76 ~]# type ifconfig
ifconfig 是 /usr/sbin/ifconfig
[root@CentOS76 ~]# 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.
    
    The variable CDPATH defines the search path for the directory containing
    DIR.  Alternative directory names in CDPATH are separated by a colon (:).
    A null directory name is the same as the current directory.  If DIR begins
    with a slash (/), then CDPATH is not used.
    
    If the directory is not found, and the shell option `cdable_vars' is set,
    the word is assumed to be  a variable name.  If that variable has a value,
    its value is used for DIR.
    
    Options:
        -L	force symbolic links to be followed
        -P	use the physical directory structure without following symbolic
    	links
        -e	if the -P option is supplied, and the current working directory
    	cannot be determined successfully, exit with a non-zero status
    
    The default is to follow symbolic links, as if `-L' were specified.
    
    Exit Status:
    Returns 0 if the directory is changed, and if $PWD is set successfully when
    -P is used; non-zero otherwise.

更多描述关于“Shell内置命令”的介绍见CSDN博文“【Shell】内置命令”。

9.4 文件目录类-pwd/ls/cd/mkdir/…

本节介绍Linux下文件目录类的相关指令。依次介绍如下所示18条指令:

pwd  ls    cd    mkdir  rmdir  touch  cp    rm  mv
cat  more  less  echo   head   tail   >/>>  ln  history

1. pwd指令

# 基本语法-pwd
pwd # 显示当前工作目录的绝对路径

注意绝对路径是从根目录/开始的路径,相对路径的开头是./../表示上一级路径)。

2. ls指令

# 基本语法-ls
ls [选项] [目录或是文件]  # 查看当前目录
ll [选项] [目录或是文件]  # ll是“ls -l”的缩写

# 常用选项-ls
-a  显示当前目录所有的文件和目录,包括隐藏的。
-l  以列表的方式显示信息。
-h  以更加人性化(human)的方式显示信息,比如自动选择最合适的文件大小单位。

3. cd指令

# 基本语法-cd
cd [参数]   # 切换到指定目录
cd          # 回到自己的家目录
cd ~        # 回到自己的家目录
cd ..       # 回到当前目录的上一级目录,若已经是根目录则不会再跳转。

下面展示四个应用实例:

# 【案例1】使用绝对路径切换到root目录。
cd /root

# 【案例2】使用相对路径到/root目录,假设现在是在/home/tom这个二级目录下。
cd ../../root

# 【案例3】回到当前目录的上一级目录。
cd ..

# 【案例4】回到家目录。
cd

4. mkdir指令

# 基本语法-mkdir
mkdir [选项] 要创建的目录的路径   # 创建目录

# 常用选项-mkdir
-p  创建多级目录

注意创建目录的过程中,若不写清楚绝对路径,则默认会在当前目录下创建目录。

下面展示两个应用实例:

# 【案例1】创建一个目录/home/dog
mkdir /home/dog

# 【案例2】创建多级目录/home/animal/tiger
mkdir -p /home/animal/tiger

5. rmdir指令

# 基本语法-rmdir
rmdir [选项] [要删除的空目录]       # 删除空目录
rm -rf [要删除的目录]               # 删除非空目录

注意上述rmdir只能删除空目录,如果目录下有内容时无法删除的。如果需要删除非空目录,需要使用rm -rf 要删除的目录(r代表递归,f代表强制)。注意使用-rf一定要非常谨慎,要不然一下子删掉什么系统文件整个系统就崩溃了。在Linux下,添加文件无所谓;删除文件一定要谨慎;修改文件也要谨慎,最好备份一份

下面展示两个应用实例:

# 【案例1】删除空目录/home/dog
rmdir /home/dog

# 【案例2】删除非空目录/home/animal
rm -rf /home/animal

6. touch指令

# 基本语法-touch
touch 文件名称  # 创建空文件,并修改相应的时间索引。

下面展示一个应用实例:

# 【案例1】在/home目录下,创建一个空文件hello.txt
touch /home/hello.txt

7. cp指令(拷贝)

# 基本语法-cp
cp [选项] 待拷贝文件 指定目录   # 拷贝文件到指定目录。

# 常用选项-cp
-r  递归复制整个文件夹

注意基本语法中的指定目录也可以最后指定到一个文件,若没有该文件则会自动创建并复制内容,若存在该文件则会询问是否覆盖y/n?

下面展示两个应用实例:

# 【案例1(文件)】将/home/hello.txt拷贝到/home/bbb目录下。
mkdir /home/bbb     # 先创建bbb目录
cp /home/hello.txt /home/bbb/

# 【案例2(文件夹)】将整个/home/bbb文件夹递归复制到文件夹/opt。
cp -r /home/bbb /opt        # 注意这个过程如果有同名文件就会依次询问
\cp -r /home/bbb /opt       # 强制覆盖文件并且不提示

8. rm指令(删除)

# 基本语法-rm
rm [选项] 要删除的文件或目录        # 移除文件或目录,注意会询问是否删除```y/n?```。

# 常用选项-rm
-r  递归删除整个文件夹
-f  强制删除不提示

注意不加选项的rm会询问是否删除y/n?。于是根据上述内容,就可以推断出删库跑路rm -rf /*指令的意思就是直接强制删除根目录下所有的文件并且不提示,显然若没有备份,当前的Linux系统就直接废了。

下面展示两个应用实例:

# 【案例1】将/home/hello.txt删除。
[root@CentOS76 home]# rm /home/hello.txt 
rm:是否删除普通文件 "/home/hello.txt"?y

# 【案例2】递归删除整个文件夹/home/bbb。
rm -rf /home/bbb

9. mv指令(剪切、粘贴)

# 基本语法-mv
mv 旧文件/目录 新文件/目录      # 移动文件/目录并实现重命名

注意在上述语法中,若新文件/目录不存在就是移动并重命名;若新文件/目录存在就是单纯的移动。注意若两个目录只有最后一级不同,那就只是重命名。若新文件/目录旧文件/目录同名,就会询问是否覆盖y/n?

下面展示三个应用实例:

# 【案例1】将/home/cat.txt文件重新命名为pig.txt。
mv cat.txt pig.txt

# 【案例2】将/home/pig.txt文件移动到/root目录下并重命名为cow.txt
mv /home/pig.txt /root/cow.txt

# 【案例3】将/opt/bbb文件移动到/home目录下
mv /opt/bbb /home

10. cat指令(只读)

# 基本语法-cat
cat [选项] 要查看的文件     # 查看文件内容

# 常用选项-cat
-n  显示行号。

之前使用vim查看文件内容,但是由于cat不能修改文件内容,所以查看一些重要文件建议使用cat。注意cat指令会直接将文件内容全部显示在屏幕上,所以一般会配合more指令(见下一节)来更好的管理显示的内容。

下面展示一个应用实例:

# 【案例1】读取/etc/profile文件内容,并显示行号。
cat -n /etc/profile
cat -n /etc/profile | more      # 见下方说明

假如文件太长,可以使用管道命令+more指令| more。管道命令|有点类似于将前面得到的结果再交给下一个指令进行处理;而more指令则会将较长的文本分段展示,便于阅读。所以上面的“应用实例”可以输入cat -n /etc/profile | more,此时屏幕上就只显示前43行结果,输入Enter下一行、输入Space键下一页……更多操作说明见下一小节,更多“管道命令”的说明见“9.6节 - 4. grep指令和管道符号|”。

11. more指令

本节要介绍的more指令是一个基于VI编辑器的文本过滤器,它以全屏幕的方式按页显示文本文件的内容。注意可以与上述的cat配合使用,也可以独立使用。more指令所显示的内容可以使用下面的快捷键操作:

表9-2 more指令显示操作说明
操作 功能说明
空格键(space) 代表向下翻一页
Enter 代表向下翻一行
q 代表立刻离开more,不再显示该文件内容
Ctrl+F 向下滚动一屏
Ctrl+B 返回上一屏
= 输出当前行的行号
:f 输出文件名和当前行的行号

下面展示一个应用实例:

# 【案例】使用more指令查看/etc/profile文件。
more /etc/profile

12. less指令

# 基本语法-less
less 要查看的文件       # 分屏查看文件内容

上述less指令的功能与more指令类似但更强大,less支持各种显示终端。并且,不像vi或者vim都是将文件先加载完毕再显示,less指令在显示文件内容时是根据显示需要加载内容(类似于动态加载),对于显示大型文件具有较高的效率less指令所显示的内容可以使用下面的快捷键操作:

表9-2 less指令操作说明
操作 功能说明
空格键 向下翻动一页
[pageup] / [pagedown] 向上/下翻动一页
方向键↑ / ↓ 向上/下翻动一行
/字串 向下搜寻[字串]的功能;n:向下查找;N:向上查找;
?字串 向上搜寻[字串]的功能;n:向上查找;N:向下查找;
q 离开less这个程序

下面展示一个应用实例:
【素材提供】一个较大的文本文件“杂文.txt”。

# 【案例】使用Xftp将“杂文.txt”上传到/opt目录下,并查看该文件。
less /opt/杂文.txt

13. echo指令

# 基本语法
echo [选项] [输出内容]   # 将输出内容展示到控制台
echo                    # 输出空行

上述echo在后期编写Shell脚本的时候会经常遇到。

下面展示两个应用实例:

# 【案例1】使用echo指令输出环境变量$PATH、主机名$HOSTNAME。
[root@CentOS76 ~]# echo $PATH 
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@CentOS76 ~]# echo $HOSTNAME
CentOS76

环境变量包含了指定命令的搜索路径,指定操作系统需要使用到的可执行程序的位置,更详细的介绍可以参考CSDN博文“【Linux】这篇文章让你彻底搞懂什么是环境变量”。

# 【案例2】使用echo指令输出字符串“hello,world!”。
[root@CentOS76 ~]# echo "hello,world!"
-bash: !": event not found              # echo不能使用双引号输出感叹号
[root@CentOS76 ~]# echo 'hello,world!'
hello,world!
[root@CentOS76 ~]# echo "hello,world~"
hello,world~

上述echo不能使用双引号输出感叹号,原因是开启了 H - histexpand模式。命令行下,双引号里面用了 “!” 的话,Shell会以为要执行历史展开,从而导致报错(这句话其实没懂,后面学到了再回来补吧)。具体可以参考CSDN博文“Linux -bash: !“: event not found 问题解决”。

14. head指令

head指令用于显示文件的开头部分内容,默认显示文件的前10行内容。

# 基本语法-head
head 文件       # 默认查看文件前10行内容
head -n x 文件  # 查看文件前x行内容

下面展示一个应用实例:

# 【案例】查看/etc/profile的前面6行代码。
[root@CentOS76 ~]# head -n 6 /etc/profile
# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you

15. tail指令

head相反,tail用于输出文件中尾部的内容,默认显示文件的最后10行内容。

# 基本语法-tail
tail 文件         # 默认查看文件尾10行内容
tail -n x 文件    # 查看文件尾x行内容
tail -f 文件      # 实时追踪该文档的所有更新

下面展示两个应用实例:
【案例1】查看/etc/profile最后5行的代码。

[root@CentOS76 ~]# tail -n 5 /etc/profile
    fi
done

unset i
unset -f pathmunge

【案例2】在/home下创建一个hello.txt文件,在添加"Hello!"的过程中,对其进行实时监控。执行该案例需要两个终端,比如Xshell和VMware图形界面的终端。要退出实时监控状态按下快捷键ctrl+c

##########终端1########
# 1. 创建文件
touch /home/hello.txt
# 2. 输入实时监控指令
tail -f /home/hello.txt           # 该指令输入完毕后,界面自动输出监控到的文件变化

##########终端1########
# 3. 添加内容到hello.txt末尾
echo 'Hello!' >> /home/hello.txt  # 指令输入完,按下回车瞬间可在终端1查看文件变化情况

16. >指令和>>指令

“输出重定向指令>” 和 “追加指令>>”都是将要在终端显示的内容输出到文件中去,但方式不同并且通常要搭配其他指令使用>可以将文件清空并将新内容写入到文件中去(覆盖写);而>>则是将内容添加到文档的末尾(追加)。注意上述要输出的目标文件若不存在,则会自动创建。下面是>>>和其他指令配合使用的语法:

# 基本语法->指令和>>指令
ls -l > 文件        # 将列表内容覆盖写入文件中
ls -al >> 文件      # 将列表内容追加到文件末尾
cat 文件1 > 文件2   # 将文件1的内容覆盖写入到文件2
cat 文件1 >> 文件2  # 将文件1的内容追加到文件2末尾
echo 内容 > 文件    # 将内容覆盖写入文件中
echo 内容 >> 文件   # 将内容追加到文件末尾

下面展示两个应用实例:
【案例1】将/home目录下的文件列表覆写到/home/myinfo.txt中。

[root@CentOS76 home]# ls -al /home > /home/myinfo.txt
[root@CentOS76 home]# cat myinfo.txt 
总用量 40
drwxr-xr-x.  9 root  root   4096 718 11:36 .
dr-xr-xr-x. 18 root  root   4096 717 11:49 ..
-rw-r--r--.  1 root  root      7 718 11:27 hello.txt
drwx------.  3 tom   a      4096 715 15:49 jack
drwx------.  5 jerry jerry  4096 716 16:31 jerry
drwx------.  3 milan jerry  4096 715 15:50 king
drwx------. 15 lyl   lyl    4096 715 10:59 lyl
drwx------.  5 zwj   wudang 4096 715 12:12 milan
-rw-r--r--.  1 root  root      0 718 11:36 myinfo.txt
drwx------.  3 zwj   a      4096 715 15:08 tom
drwx------.  3 jerry   1009 4096 716 10:35 zwj

【案例2】将当前日历信息追加到/home/myinfo.txt文件中。

提示:cal指令显示当前的日期信息。

[root@CentOS76 home]# cal
      七月 2023     
日 一 二 三 四 五 六
                   1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
[root@CentOS76 home]# cal >> /home/myinfo.txt
[root@CentOS76 home]# cat myinfo.txt 
总用量 40
drwxr-xr-x.  9 root  root   4096 718 11:36 .
dr-xr-xr-x. 18 root  root   4096 717 11:49 ..
-rw-r--r--.  1 root  root      7 718 11:27 hello.txt
drwx------.  3 tom   a      4096 715 15:49 jack
drwx------.  5 jerry jerry  4096 716 16:31 jerry
drwx------.  3 milan jerry  4096 715 15:50 king
drwx------. 15 lyl   lyl    4096 715 10:59 lyl
drwx------.  5 zwj   wudang 4096 715 12:12 milan
-rw-r--r--.  1 root  root      0 718 11:36 myinfo.txt
drwx------.  3 zwj   a      4096 715 15:08 tom
drwx------.  3 jerry   1009 4096 716 10:35 zwj
      七月 2023     
日 一 二 三 四 五 六
                   1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31

17. ln指令
软链接“ln指令”也称为符号链接,类似于Windows里的快捷方式,主要存放了链接其他文件的路径。访问该链接就等价于访问其链接的路径。但需要注意:

  1. 当我们使用pwd指令查看目录时,仍然看到的是软链接所在目录。以/home/myroot -> /root为例,若cd定位到/home/myroot,此时使用pwd查看当前路径仍然显示的是/hoe/myroot而不是/root
  2. 之所以介绍ln指令,是因为后续看“动态链接库”的时候会有很多这样的链接文件,因为很多“动态链接库”是很多应用程序共享的,创建链接是最简单的调用方法。
# 基本语法-符号链接
ln -s [原文件或目录] [软链接名]     # 给原文件创建一个软链接

下面展示两个应用实例:
【案例1】在/home目录下创建一个软连接myroot,链接到/root目录。

[root@CentOS76 home]# ln -s /root /home/myroot
[root@CentOS76 home]# ls -l
总用量 36
-rw-r--r--.  1 root  root      7 718 11:27 hello.txt
drwx------.  3 tom   a      4096 715 15:49 jack
drwx------.  5 jerry jerry  4096 716 16:31 jerry
drwx------.  3 milan jerry  4096 715 15:50 king
drwx------. 15 lyl   lyl    4096 715 10:59 lyl
drwx------.  5 zwj   wudang 4096 715 12:12 milan
-rw-r--r--.  1 root  root   2351 718 11:43 myinfo.txt
lrwxrwxrwx.  1 root  root      5 718 11:50 myroot -> /root
drwx------.  3 zwj   a      4096 715 15:08 tom
drwx------.  3 jerry   1009 4096 716 10:35 zwj

此时,目录/home/myroot就等价于/root

【案例2】删除软连接/home/myroot:

[root@CentOS76 home]# ls /home
hello.txt  jack  jerry  king  lyl  milan  myinfo.txt  myroot  tom  zwj      # 注:其实软链接的颜色和其他不同
[root@CentOS76 home]# rm /home/myroot
rm:是否删除符号链接 "/home/myroot"?y
[root@CentOS76 home]# ls /home
hello.txt  jack  jerry  king  lyl  milan  myinfo.txt  tom  zwj

18. history指令

本小节介绍history指令,其作用是查看已经执行过历史命令,也可以执行历史指令。

# 基本语法-hostory
history     # 显示所有历史命令
history x   # 显示最近的x条指令
!x          # 执行历史编号为x的指令

下面展示三个应用实例:
【案例1】显示所有的历史命令。

[root@CentOS76 myroot]# history
    1  gcc -v
    2  tar VMwareTools-10.3.25-20206839.tar.gz 
    3  tar -zxvf VMwareTools-10.3.25-20206839.tar.gz 
    4  cd vmware-tools-distrib/
    5  ./vmware-install.pl 
    6  ipconfig
    7  ifconfig
    8  ifconfig
    9  cd /root
   10  ls
   11  cd /root/桌面
   #...这里省略了中间的指令
  448  ls
  449  pwd
  450  cd /home/myroot
  451  pwd
  452  history

【案例2】显示最近使用过的10个指令。

[root@CentOS76 myroot]# history 10
  444  ll
  445  rm /home/myroot
  446  ls /home
  447  ln -s /root /home/myroot
  448  ls
  449  pwd
  450  cd /home/myroot
  451  pwd
  452  history
  453  history 10

【案例3】执行历史编号为451的指令。

[root@CentOS76 myroot]# !451
pwd
/home/myroot

9.5 时间日期类-date/cal

本节介绍两个时间日期类的指令:datecal

date    # 显示或修改当前日期、时间
cal     # 显示日历

1. date指令-显示当前日期

date指令有两个功能:显示日期、设置日期。

# 基本语法-date显示日期
date                        # 显示当前时间
date +%Y                    # 显示当前年份
date +%m                    # 显示当前月份
date +%d                    # 显示当前日期
date "+%Y-%m-%d %H:%M:%S"   # 显示年月日时分秒
date --help                 # 查看日期显示的更多用法

# 基本语法-date设置日期
date -s "字符串时间"   # 设置系统时钟为字符串时间

下面展示四个应用实例:前三个展示“显示日期”功能、最后一个展示“设置日期”功能。

# 【案例1】显示当前时间信息。
[root@CentOS76 myroot]# date
2023年 07月 18日 星期二 15:01:45 CST

# 【案例2】显示当前年月日。
[root@CentOS76 myroot]# date +%Y-%m-%d
2023-07-18

# 【案例3】显示当前时间年月日时分秒。
[root@CentOS76 myroot]# date "+%Y-%m-%d %H:%M:%S"
2023-07-18 15:03:23

# 【案例4】设置系统当前时间,比如设置成2021-11-11 11:22:22。
[root@CentOS76 ~]# date -s "2021-11-11 11:22:22"
20211111日 星期四 11:22:22 CST
[root@CentOS76 ~]# reboot # 重启并联网可以恢复时间

2. cal指令

Linux下要想查看日历就使用cal指令(calendar, 日历)。

# 基本语法-cal指令
cal [选项] [[[]]] # 通用格式
cal          # 不加选项,默认显示本月日历
cal -1       # 显示本月日历
cal -3       # 显示上月、本月、下月日历
cal -s       # 显示时,周日作为第一列
cal -m       # 显示时,周一作为第一列
cal a        # 显示a年的整年日历
cal b a      # 显示a年b月的日历
cal c b a    # 显示a年b月的日历,并将c日标出来
cal --help   # 显示所有选项信息

下面展示两个应用实例:

# 【案例1】显示当前日历。
[root@CentOS76 home]# cal
      七月 2023     
日 一 二 三 四 五 六
                   1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22        # 18号那里有背景颜色反转强调
23 24 25 26 27 28 29
30 31

# 【案例2】显示2020年日历。
[root@CentOS76 ~]# cal 2020
                               2020                               

        一月                   二月                   三月        
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六
          1  2  3  4                      1    1  2  3  4  5  6  7
 5  6  7  8  9 10 11    2  3  4  5  6  7  8    8  9 10 11 12 13 14
12 13 14 15 16 17 18    9 10 11 12 13 14 15   15 16 17 18 19 20 21
19 20 21 22 23 24 25   16 17 18 19 20 21 22   22 23 24 25 26 27 28
26 27 28 29 30 31      23 24 25 26 27 28 29   29 30 31

        四月                   五月                   六月        
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六
          1  2  3  4                   1  2       1  2  3  4  5  6
 5  6  7  8  9 10 11    3  4  5  6  7  8  9    7  8  9 10 11 12 13
12 13 14 15 16 17 18   10 11 12 13 14 15 16   14 15 16 17 18 19 20
19 20 21 22 23 24 25   17 18 19 20 21 22 23   21 22 23 24 25 26 27
26 27 28 29 30         24 25 26 27 28 29 30   28 29 30
                       31
        七月                   八月                   九月        
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六
          1  2  3  4                      1          1  2  3  4  5
 5  6  7  8  9 10 11    2  3  4  5  6  7  8    6  7  8  9 10 11 12
12 13 14 15 16 17 18    9 10 11 12 13 14 15   13 14 15 16 17 18 19
19 20 21 22 23 24 25   16 17 18 19 20 21 22   20 21 22 23 24 25 26
26 27 28 29 30 31      23 24 25 26 27 28 29   27 28 29 30
                       30 31
        十月                  十一月                 十二月       
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六
             1  2  3    1  2  3  4  5  6  7          1  2  3  4  5
 4  5  6  7  8  9 10    8  9 10 11 12 13 14    6  7  8  9 10 11 12
11 12 13 14 15 16 17   15 16 17 18 19 20 21   13 14 15 16 17 18 19
18 19 20 21 22 23 24   22 23 24 25 26 27 28   20 21 22 23 24 25 26
25 26 27 28 29 30 31   29 30                  27 28 29 30 31

9.6 搜索查找类-find/locate/which/grep

本节介绍“搜索查找类”相关的四个指令:findlocatewhichgrep和管道符号|

find    实际搜寻硬盘查询文件名称。
locate   配合数据库查看文件位置。
which   查看可执行文件的位置。
grep     常配合管道符号搜索内容中的关键字。
# whereis  查看文件的位置。——老韩未介绍

1. find指令

find指令将从指定目录向下递归地遍历其各个子目录,将满足条件的文件或者目录显示在终端。显然应该尽量在小范围目录下使用find,以避免搜索时间过长。

# 基本语法-find
find [搜索范围] [选项] [具体要求]     # 按照选型在指定范围查找文件
表9-3 find指令选项说明
选项 功能
-name<查询方式> 按照指定的文件名查找模式查找文件。
-user<用户名> 查找属于指定用户名的所有文件。
-size<文件大小> 按照指定的文件大小查找文件
大小关系:+n表示大于n、-n表示小于n、n表示等于n。
单位:k、M、G。

下面按照上述指令选项,依次展示三个应用实例:
【案例1-按文件名】查找/home目录下的hello.txt文件、所有的txt文本*.txt

[root@CentOS76 ~]# find /home -name hello.txt
/home/hello.txt
[root@CentOS76 ~]# find /home -name *.txt
/home/lyl/.cache/tracker/first-index.txt
/home/lyl/.cache/tracker/last-crawl.txt
/home/lyl/.cache/tracker/parser-sha1.txt
/home/lyl/.cache/tracker/locale-for-miner-user-guides.txt
/home/lyl/.cache/tracker/db-version.txt
/home/lyl/.cache/tracker/locale-for-miner-apps.txt
/home/lyl/.cache/tracker/db-locale.txt
/home/lyl/.mozilla/firefox/g2lzrpra.default-default/AlternateServices.txt
/home/lyl/.mozilla/firefox/g2lzrpra.default-default/pkcs11.txt
/home/lyl/.mozilla/firefox/g2lzrpra.default-default/TRRBlacklist.txt
/home/lyl/.mozilla/firefox/g2lzrpra.default-default/SiteSecurityServiceState.txt
/home/lyl/.mozilla/firefox/g2lzrpra.default-default/SecurityPreloadState.txt
/home/myinfo.txt
/home/hello.txt

【案例2-按拥有者】查找/home目录下,用户名为nobody、tom的文件。

[root@CentOS76 ~]# find /home -user nobody      # 什么都不显示就是没找到
[root@CentOS76 ~]# find /home -user jack
[root@CentOS76 ~]# find /home -user tom
/home/jack
/home/jack/.bash_logout
/home/jack/.bash_profile
/home/jack/.bashrc
/home/jack/.mozilla
/home/jack/.mozilla/extensions
/home/jack/.mozilla/plugins

【遗留问题】为什么jack和tom两个用户都存在且都在同一个组a下,却只能通过tom找到jack的文件呢?我记得应该是tom先被加入到组a中,并且他们各自的同名组都被删除了。

【案例3-按文件大小】查找整个linux系统下大于100M的文件。

[root@CentOS76 ~]# find / -size +100M
/var/cache/yum/x86_64/7/updates/packages/firefox-102.12.0-1.el7.centos.x86_64.rpm
/var/cache/yum/x86_64/7/updates/gen/primary_db.sqlite
/var/cache/yum/x86_64/7/updates/gen/filelists_db.sqlite
/var/lib/rpm/Packages
/proc/kcore
find: ‘/proc/2870/task/2870/fd/5’: 没有那个文件或目录
find: ‘/proc/2870/task/2870/fdinfo/5’: 没有那个文件或目录
find: ‘/proc/2870/fd/6’: 没有那个文件或目录
find: ‘/proc/2870/fdinfo/6’: 没有那个文件或目录
/usr/lib64/firefox/libxul.so
/usr/lib/locale/locale-archive
/sys/devices/pci0000:00/0000:00:0f.0/resource1_wc
/sys/devices/pci0000:00/0000:00:0f.0/resource1
[root@CentOS76 ~]# ls -lh /usr/lib64/firefox/libxul.so  #ls的-h选项是为了显示文件大小更符合人类阅读习惯
-rwxr-xr-x. 1 root root 116M 78 2020 /usr/lib64/firefox/libxul.so

2. locate指令

locate指令可以快速定位文件路径locate指令无需遍历整个文件系统,查询速度较快。原因是locate指令利用事先建立的系统中所有文件名称及路径的locate数据库实现快速定位给定的文件,所以第一次运行前必须使用updatedb指令创建locate数据库,同时为了保证查询结果的准确度,管理员必须定期使用updatedb指令更新locate数据库。

# 基本语法-locate
locate 搜索文件

下面展示一个应用实例:

# 【案例1】请使用locate指令快速定位hello.txt文件所在目录。
[root@CentOS76 ~]# locate hello.txt     # 显然不创建数据库什么也搜索不出来
[root@CentOS76 ~]# updatedb
[root@CentOS76 ~]# locate hello.txt
/home/hello.txt

3. which指令
which指令的作用是,在PATH变量指定的路径中,搜索某个系统命令的位置,并且返回第一个搜索结果。也就是说,which可以查看某个指令在哪个目录下。

# 基本语法-which
which 可执行文件名称      # 查找指令所在的目录

下面展示一个应用实例:

# 【案例1】查看ls指令、reboot分别在哪个目录。
[root@CentOS76 ~]# which ls
alias ls='ls --color=auto'
	/usr/bin/ls
[root@CentOS76 ~]# which reboot
/usr/sbin/reboot

4. grep指令和管道符号|

grep指令的作用是过滤查找;管道符号|则表示将前一个命令的处理结果输出传递给后面的命令处理。grep过滤查找常常和管道符号|结合使用,也就是将前面显示的内容通过管道符号|交给后面的grep指令来查找相应内容,如cat a.txt | grep "hello"就是在a.txt中查找字符串"hello"所在的行。经常使用grep指令来查端口的使用情况。

# 基本语法-grep
grep [选项] 查找内容 源文件     # 在源文件中查找内容

# 常用选项-grep
-n  显示匹配行及行号
-i  忽略字母大小写

下面展示通过一个应用实例展示使用grep查找的关键字的两种方式:

素材:一个较大的文本文件—— 杂文.txt

# 【案例1】使用Xftp将“杂文.txt”上传到/opt目录下,并查找“见龙在田”所在的行号:
[root@CentOS76 ~]# cat /opt/杂文.txt | grep -n "见龙在田"
14711:    萧峰一招“见龙在田”,从旁拍击过去,判官笔为掌风所激,笔腰竟尔弯曲,从段誉脑后绕了个弯,向慕容复射了回去。
15416:    萧峰喝道:“你就想走?天下有这等便宜事?你父亲身上有病,大丈夫不屑乘人之危,且放了他过去。你可没病没痛!”慕容复气往上冲,喝道:“那我便接萧兄的高招。”萧峰更不打话,呼的一掌,一招降龙十八掌的“见龙在田”,向慕容复猛击过去。他见藏经阁地势险隘,高群集,不便久斗,是以使上了十成力,要在数掌之间便取了敌人性命。慕容复见他掌势凶恶,当即运起平生之力,要以“斗转星移”之术化解。
[root@CentOS76 ~]# grep -n "见龙在田" /opt/杂文.txt 
14711:    萧峰一招“见龙在田”,从旁拍击过去,判官笔为掌风所激,笔腰竟尔弯曲,从段誉脑后绕了个弯,向慕容复射了回去。
15416:    萧峰喝道:“你就想走?天下有这等便宜事?你父亲身上有病,大丈夫不屑乘人之危,且放了他过去。你可没病没痛!”慕容复气往上冲,喝道:“那我便接萧兄的高招。”萧峰更不打话,呼的一掌,一招降龙十八掌的“见龙在田”,向慕容复猛击过去。他见藏经阁地势险隘,高群集,不便久斗,是以使上了十成力,要在数掌之间便取了敌人性命。慕容复见他掌势凶恶,当即运起平生之力,要以“斗转星移”之术化解。

9.7 压缩和解压类-gzip/gunzipzip/unziptar

本节介绍压缩/解压类的三个指令:gzip/gunzipzip/unziptar

gzip / gunzip   gzip只能压缩文件成“.gz”文件,不能压缩目录,同时源文件会被删除。
# bzip2 / bunzip2 是gzip、gunzip命令的压缩比升级版,但也只能压缩文件成“.bz2”文件,不能压缩目录,可通过‘-k’保留源文件。
zip / unzip     zip压缩文件或目录成“.zip”文件,源文件会被保留。
tar             将目录打包成一个“.tar”文件,同时结合其选项完成gzip、bzip2的目录压缩。源文件会被保留。

1. gzip/gunzip指令

gzip用于压缩文件成“.gz”文件,gunzip用于解压“.gz”文件,压缩比较高(对文本文件有60%~70%的压缩率)。但只能对单个文件进行操作,不能压缩目录,同时源文件会被删除

# 基本语法-gzip/gunzip
gzip 文件       # 压缩文件,只能将文件压缩为*.gz文件
gunzip .gz文件  # 解压缩.gz文件

下面展示一个应用实例:

# 【案例1】将/home下的hello.txt文件进行压缩,再将/home下的hello.txt.gz文件进行解压缩。
[root@CentOS76 ~]# ls /home
hello.txt  jack  jerry  king  lyl  milan  myinfo.txt  myroot  tom  zwj
[root@CentOS76 ~]# gzip /home/hello.txt 
[root@CentOS76 ~]# ls /home
hello.txt.gz  jack  jerry  king  lyl  milan  myinfo.txt  myroot  tom  zwj
[root@CentOS76 ~]# gunzip /home/hello.txt.gz 
[root@CentOS76 ~]# ls /home
hello.txt  jack  jerry  king  lyl  milan  myinfo.txt  myroot  tom  zwj

2. zip/unzip指令

zip用于压缩文件成“.zip”文件,unzip用于解压“.zip”文件,但是压缩比不是很高。优点是不仅可以压缩文件,也可以压缩目录,源文件会被保留。在进行开发的时候,往往会对整个文件夹进行打包和发布,此时zip就很有用。

# 基本语法-zip/unzip
zip [选项] 压缩后的文件路径及名称 要压缩的内容  # 压缩文件/目录成.zip文件
unzip [选项] 需要解压的.zip文件               # 解压缩文件

# zip常用选项
-r :递归压缩,即压缩整个目录
# unzip的常用选项
-d<目录> :指定解压后文件的存放目录

下面展示两个应用实例:

# 【案例1】将/home下的所有文件进行压缩成myhome.zip。
[root@CentOS76 ~]# zip -r /home/myhome.zip /home/
  adding: home/ (stored 0%)
  adding: home/jack/ (stored 0%)
# ...中间省略了很多行
  adding: home/tom/.mozilla/extensions/ (stored 0%)
  adding: home/tom/.mozilla/plugins/ (stored 0%)
[root@CentOS76 ~]# ls /home
hello.txt  jack  jerry  king  lyl  milan  myhome.zip  myinfo.txt  myroot  tom  zwj


# 【案例2】将上个案例的myhome.zip解压到/opt/tmp目录下。
[root@CentOS76 ~]# mkdir /opt/tmp
[root@CentOS76 ~]# ls /opt/tmp
[root@CentOS76 ~]# unzip -d /opt/tmp /home/myhome.zip 
Archive:  /home/myhome.zip
   creating: /opt/tmp/home/
   creating: /opt/tmp/home/jack/
# ...中间省略了很多行
   creating: /opt/tmp/home/tom/.mozilla/extensions/
   creating: /opt/tmp/home/tom/.mozilla/plugins/
[root@CentOS76 ~]# ls /opt/tmp
home
[root@CentOS76 ~]# ls /opt/tmp/home
hello.txt  jack  jerry  king  lyl  milan  myinfo.txt  myroot  tom  zwj

3. tar指令

tar指令可以将 单个文件多个文件目录 打包成“.tar”文件,或者将“.tar”文件解包,以此来配合gzip/gunzip指令来完成对于整个目录的压缩/解压缩。但是上述过程不会直接让用户再调用gzip/gunzip指令,而是直接使用选项“-z打包同时压缩/解压缩”。于是在打包时tar指令就会将打包好的“.tar”文件再压缩成“.tar.gz”文件,解包时就会将“.tar.gz”先解压缩成“.tar”文件再解包。所以 tar指令是Linux中最常用的压缩/解压缩指令,源文件会被保留。

# 基本语法-tar
tar [选项] 打包后的名称XXX.tar.gz 要打包的内容    # 打包目录,压缩后的文件格式.tar.gz
# 注:压缩多个文件后面接着打空格即可。

# 选项说明-tar
-c  打包成.tar文件
-x  解包.tar文件
-v  显示详细信息
-f  指定文件名
-z  打包同时压缩/解压缩

下面展示四个应用实例:
【案例1】压缩多个文件,将/home/pig.txt/home/cat.txt压缩成pc.tar.gz

[root@CentOS76 ~]# touch /home/pig.txt
[root@CentOS76 ~]# touch /home/cat.txt
[root@CentOS76 ~]# ls /home
cat.txt  hello.txt  jack  jerry  king  lyl  milan  myinfo.txt  myroot  pig.txt  tom  zwj
[root@CentOS76 ~]# tar -zcvf /home/pc.tar.gz /home/pig.txt /home/cat.txt 
tar: 从成员名中删除开头的“/”
/home/pig.txt
/home/cat.txt
[root@CentOS76 ~]# ls /home
cat.txt    jack   king  milan       myroot     pig.txt  zwj
hello.txt  jerry  lyl   myinfo.txt  pc.tar.gz  tom

【案例2】将/home目录压缩成myhome.tar.gz

[root@CentOS76 ~]# tar -zcvf /home/myhome.tar.gz /home/
tar: 从成员名中删除开头的“/”
/home/
/home/jack/
# ...省略了很多行
/home/tom/.mozilla/plugins/
/home/pc.tar.gz
tar: /home: 在我们读入文件时文件发生了变化
[root@CentOS76 ~]# ls /home
cat.txt    jack   king  milan          myinfo.txt  pc.tar.gz  tom
hello.txt  jerry  lyl   myhome.tar.gz  myroot      pig.txt    zwj

【案例3】将pc.tar.gz解压到/home目录。

[root@CentOS76 ~]# cd /home
[root@CentOS76 home]# tar -zxvf /home/pc.tar.gz
home/pig.txt
home/cat.txt
[root@CentOS76 home]# ls
hello.txt  jack   king  milan          myinfo.txt  pc.tar.gz  zwj
home       jerry  lyl   myhome.tar.gz  myroot      tom
[root@CentOS76 home]# ls /home/home
cat.txt  pig.txt

【案例4】将myhome.tar.gz解压到/opt/tmp2目录下。

[root@CentOS76 home]# mkdir /opt/tmp2
[root@CentOS76 home]# ls /opt
rh  tmp2  VMwareTools-10.3.25-20206839.tar.gz  vmware-tools-distrib  杂文.txt
[root@CentOS76 home]# tar -zxvf /home/myhome.tar.gz -C /opt/tmp2
home/
home/jack/
home/jack/.bash_logout
# ...这里省略了很多
home/tom/.mozilla/extensions/
home/tom/.mozilla/plugins/
home/pc.tar.gz
[root@CentOS76 home]# ls /opt/tmp2
home
[root@CentOS76 home]# ls /opt/tmp2/home
cat.txt    jack   king  milan       myroot     pig.txt  zwj
hello.txt  jerry  lyl   myinfo.txt  pc.tar.gz  tom

  1. 《鸟哥的Linux私房菜 基础学习篇 第四版》 ↩︎

  2. 《鸟哥的Linux私房菜 服务器架设篇 第三版》 ↩︎

  3. 《韩顺平_2021图解Linux全面升级》 ↩︎

猜你喜欢

转载自blog.csdn.net/weixin_46258766/article/details/131949161
今日推荐