9 Practical Linux Operations - Practical Instructions
Article directory
- 9 Practical Linux Operations - Practical Instructions
-
- 9.1 Specify and modify the run level -```init```/```systemctl```
- 9.2 Retrieve the root password
- 9.3 Linux instructions
- 9.3 Help class - ```man```/```help```
- 9.4 File directory class - ```pwd```/```ls```/```cd```/```mkdir```/...
- 9.5 Date and time classes - ```date```/```cal```
- 9.6 Search and find class -```find```/```locate```/```which```/```grep```
- 9.7 Compression and decompression classes - ```gzip```/```gunzip```, ```zip```/```unzip```, ```tar```
- The learning video comes from Station B [Xiaobai Getting Started is easy to understand] 2021 Han Shunping learned Linux in a week .
- The materials that may be used are as follows, and the download link is at the end of the article:
This chapter will introduce some common instructions that are widely used in Linux.
9.1 Specifying and modifying runlevels - init
/systemctl
level | describe |
---|---|
0 | shutdown state. The default operation level of the system cannot be set to 0, otherwise the system cannot be started normally. |
1 | single user status. In this state, only the root user can log in, which is used for system maintenance, and remote login is prohibited, which is equivalent to the security mode under Windows. |
2 [rare] | Multi-user state (no NFS). There is no internet service. |
3 [most common] | Full multi-user state (with NFS). There are network services, and after logging in, enter the console command line mode. |
4 [rare] | It is not used by the system and is reserved for general use. In some special cases, it can be used to do some things. For example, when the battery of the laptop is exhausted, you can switch to this mode to do some settings. |
5 [Common] | Graphical mode, after logging in, enter the graphical GUI mode or GNOME, KDE graphical interface, such as the X Window system. |
6 | The system shuts down gracefully and restarts. The default run level cannot be set to 6, otherwise it will not start normally, but will always boot and restart. |
This section introduces how to "specify the current run level" and "modify the default run level". What about "modify the default runlevel"? As follows:
1. Specify the current run level
init 0/1/2/3/4/5/6 # 可以直接跳转到指定的运行级别
init 0 # 关机
Note that the above runlevel changes are one-time only, and will return to the original default runlevel after reboot. Moreover, after changing the operation level to 3 or 5, the Linux system will automatically log out and log in, and you need to log in again.
2. Modify the default run level
Before CentOS7, if you want to specify the default run level, you need to /etc/inittab
modify the corresponding statement in the file; but in CentOS7 and later versions, it is simplified, you can directly use the command to change:
# 运行级别代称
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
Note that after modifying the default run level, a restart is required to take effect. Runlevel 3 is commonly used at work.
Two application examples are shown below:
[Case 1] On the terminal in VMware, use init
to set the run level from 5 to 3, and then restart.
# 在图形界面打开终端
init 3
# 系统会变成终端模式,需要重新登录账户,如下
CentOS76 login: root
Password:
Laset Login: Sun Jul 16 17:42:05 on :0
[root@CentOS76 ~]# init 6
[Case 2] Modify the default level to 3, and restart to observe the effect:
[root@CentOS76 home]# systemctl set-default multi-user.target
[root@CentOS76 home]# init 6
# 重启后再将默认运行级别修改回5即可。
9.2 Retrieve the root password
This section mainly introduces an interview question: how to retrieve/reset the root password? Due to the long length, you can refer to the CSDN blog post " Linux Retrieve Root Password (CentOS7.6) ". Here are two points to note:
- There are some small differences in the ways of retrieving passwords in different versions, the CentOS 7.6 system demonstrated in the blog post.
- In the above process of retrieving the password, it enters the operation level 1 , that is, "single-user mode".
9.3 Linux instructions
This section takes instructions ls
as an example to introduce the general format of instructions under Linux. Generally speaking, commands under Linux have their own "options" to complete various complex functions, as follows ls
:
# 基本语法-ls
ls [选项] [路径] # 显示指定目录下的所有文件
ls # 默认显示当前路径下的文件
# 常用选项
-a 列出所有文件,包括以 “.”(点) 开头的隐含文件
-l 单列输出,信息是最详细的
Here are some notes on these "options":
- When introducing a Linux command, "basic syntax" and "common options" will be listed. Note that in "Basic Grammar", the content enclosed in square brackets "[]" can be omitted, and the rest must be written.
- Options can be combined, and the order of these options does not matter, e.g.
ls -la
orls -al
both. It is even possible to swap the order between paths and configuration options, egls [路径] -la
, but this is generally not done without a specific reason.- Under Linux, hidden files start with " . " (dot).
Note: These will not be repeated in the introduction below, but the instructions here apply to every single Linux instruction.
Four application examples are shown below:
[Case 1] List all files in the current folder, including hidden files starting with "." (dot).
[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 公共 文档
[Case 2] Use ls
the command "single column output" all files in the current folder.
[root@CentOS76 ~]# ls -l
总用量 40
-rw-------. 1 root root 1883 7月 13 17:12 anaconda-ks.cfg
-rw-r--r--. 1 root root 1931 7月 13 17:13 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 4096 7月 13 19:44 公共
drwxr-xr-x. 2 root root 4096 7月 13 19:44 模板
drwxr-xr-x. 2 root root 4096 7月 13 19:44 视频
drwxr-xr-x. 2 root root 4096 7月 13 19:44 图片
drwxr-xr-x. 2 root root 4096 7月 13 19:44 文档
drwxr-xr-x. 2 root root 4096 7月 13 19:44 下载
drwxr-xr-x. 2 root root 4096 7月 13 19:44 音乐
drwxr-xr-x. 2 root root 4096 7月 15 10:12 桌面
【Case 3】Practice ls
the options used in combination.
[root@CentOS76 ~]# ls -la # 这个指令等价于 ls -al
总用量 112
dr-xr-x---. 14 root root 4096 7月 17 11:51 .
dr-xr-xr-x. 18 root root 4096 7月 17 11:49 ..
-rw-------. 1 root root 1883 7月 13 17:12 anaconda-ks.cfg
-rw-------. 1 root root 3229 7月 17 11:51 .bash_history
-rw-r--r--. 1 root root 18 12月 29 2013 .bash_logout
-rw-r--r--. 1 root root 176 12月 29 2013 .bash_profile
-rw-r--r--. 1 root root 176 12月 29 2013 .bashrc
drwx------. 15 root root 4096 7月 17 11:50 .cache
drwxr-xr-x. 16 root root 4096 7月 14 10:03 .config
-rw-r--r--. 1 root root 100 12月 29 2013 .cshrc
drwx------. 3 root root 4096 7月 13 17:13 .dbus
-rw-------. 1 root root 16 7月 13 19:44 .esd_auth
-rw-------. 1 root root 4034 7月 17 11:50 .ICEauthority
-rw-r--r--. 1 root root 1931 7月 13 17:13 initial-setup-ks.cfg
-rw-------. 1 root root 473 7月 17 15:53 .lesshst
drwx------. 3 root root 4096 7月 13 19:44 .local
-rw-r--r--. 1 root root 129 12月 29 2013 .tcshrc
-rw-------. 1 root root 4631 7月 16 16:27 .viminfo
-rw-------. 1 root root 108 7月 17 11:51 .Xauthority
drwxr-xr-x. 2 root root 4096 7月 13 19:44 公共
drwxr-xr-x. 2 root root 4096 7月 13 19:44 模板
drwxr-xr-x. 2 root root 4096 7月 13 19:44 视频
drwxr-xr-x. 2 root root 4096 7月 13 19:44 图片
drwxr-xr-x. 2 root root 4096 7月 13 19:44 文档
drwxr-xr-x. 2 root root 4096 7月 13 19:44 下载
drwxr-xr-x. 2 root root 4096 7月 13 19:44 音乐
drwxr-xr-x. 2 root root 4096 7月 15 10:12 桌面
[Case 4] Specifies to display all files in the /root directory.
[root@CentOS76 home]# ls -al /root # 等价于 ls /root -al
总用量 120
dr-xr-x---. 14 root root 4096 7月 18 17:21 .
dr-xr-xr-x. 18 root root 4096 7月 17 11:49 ..
-rw-------. 1 root root 1883 7月 13 17:12 anaconda-ks.cfg
-rw-------. 1 root root 5922 11月 11 2021 .bash_history
-rw-r--r--. 1 root root 18 12月 29 2013 .bash_logout
-rw-r--r--. 1 root root 176 12月 29 2013 .bash_profile
-rw-r--r--. 1 root root 176 12月 29 2013 .bashrc
drwx------. 15 root root 4096 7月 17 11:50 .cache
drwxr-xr-x. 16 root root 4096 7月 14 10:03 .config
-rw-r--r--. 1 root root 100 12月 29 2013 .cshrc
drwx------. 3 root root 4096 7月 13 17:13 .dbus
-rw-------. 1 root root 16 7月 13 19:44 .esd_auth
-rw-------. 1 root root 4348 7月 18 11:26 .ICEauthority
-rw-r--r--. 1 root root 1931 7月 13 17:13 initial-setup-ks.cfg
-rw-------. 1 root root 481 7月 17 19:40 .lesshst
drwx------. 3 root root 4096 7月 13 19:44 .local
-rw-r--r--. 1 root root 129 12月 29 2013 .tcshrc
-rw-------. 1 root root 4825 7月 17 17:46 .viminfo
-rw-------. 1 root root 108 7月 18 15:26 .Xauthority
drwxr-xr-x. 2 root root 4096 7月 13 19:44 公共
drwxr-xr-x. 2 root root 4096 7月 13 19:44 模板
drwxr-xr-x. 2 root root 4096 7月 13 19:44 视频
drwxr-xr-x. 2 root root 4096 7月 13 19:44 图片
drwxr-xr-x. 2 root root 4096 7月 13 19:44 文档
drwxr-xr-x. 2 root root 4096 7月 13 19:44 下载
drwxr-xr-x. 2 root root 4096 7月 13 19:44 音乐
drwxr-xr-x. 2 root root 4096 7月 15 10:12 桌面
9.3 helper classes - man
/help
This section introduces the help commands under Linux, which mainly include the following two: man
, help
.
man 获得任意指令的帮助信息,部分做出了汉化。
help 只能获得shell内置命令的帮助信息,一般使用英文描述。
指令 --help # 个人发现使用此格式一般也可以获取指令的帮助文档
Generally, for documents such as help documents opened under Linux, there will be corresponding prompts in the interface on how to perform operations such as page turning and searching, which are basically the same as those learned before, and the general situation is as follows vi/vim
:
# 帮助文档的查看
空格键 下一页
上/下键 换行
回车键 换行
g 回到文档头
G 回到文档尾
q 退出文档
# 注:Linux文档内禁止使用鼠标滚轮,只有在Xshell窗口才能使用鼠标滚轮。
Of course, Linux has been developed in China for many years, and there are countless Chinese documents. If you really don't know it, you can " Baidu it ".
1. man
Instructions
# 基本语法-man
man 命令或配置文件 # 获得帮助信息
For example, if you want to query ls
the help documentation for the command man ls
.
2. help
Instructions
# 基本语法-help
help 命令 # 获得shell内置命令的帮助信息
Only the help information of the shell built-in commands can be obtained, and English descriptions are generally used. Note that the "Shell built-in command" here is the command provided by the Bash Shell itself, rather than the executable script file in the file system. You can use it to determine whether type
a command is a built-in command, such as:
[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.
For more descriptions about "Shell built-in commands", see the CSDN blog post " [Shell] Built-in commands ".
9.4 File directory class - pwd
/ ls
/ cd
/ mkdir
/…
This section introduces the related commands of the file directory class under Linux. Introduce the following 18 instructions in turn:
pwd ls cd mkdir rmdir touch cp rm mv
cat more less echo head tail >/>> ln history
1. pwd
Instructions
# 基本语法-pwd
pwd # 显示当前工作目录的绝对路径
Note that the absolute path is /
the path starting from the root directory, and the beginning of the relative path is ./
( ../
indicating the upper level path).
2. ls
Instructions
# 基本语法-ls
ls [选项] [目录或是文件] # 查看当前目录
ll [选项] [目录或是文件] # ll是“ls -l”的缩写
# 常用选项-ls
-a 显示当前目录所有的文件和目录,包括隐藏的。
-l 以列表的方式显示信息。
-h 以更加人性化(human)的方式显示信息,比如自动选择最合适的文件大小单位。
3. cd
Instructions
# 基本语法-cd
cd [参数] # 切换到指定目录
cd # 回到自己的家目录
cd ~ # 回到自己的家目录
cd .. # 回到当前目录的上一级目录,若已经是根目录则不会再跳转。
Four application examples are shown below:
# 【案例1】使用绝对路径切换到root目录。
cd /root
# 【案例2】使用相对路径到/root目录,假设现在是在/home/tom这个二级目录下。
cd ../../root
# 【案例3】回到当前目录的上一级目录。
cd ..
# 【案例4】回到家目录。
cd
4. mkdir
Instructions
# 基本语法-mkdir
mkdir [选项] 要创建的目录的路径 # 创建目录
# 常用选项-mkdir
-p 创建多级目录
Note that in the process of creating a directory, if the absolute path is not written clearly, the directory will be created under the current directory by default.
Two application examples are shown below:
# 【案例1】创建一个目录/home/dog
mkdir /home/dog
# 【案例2】创建多级目录/home/animal/tiger
mkdir -p /home/animal/tiger
5. rmdir
Instructions
# 基本语法-rmdir
rmdir [选项] [要删除的空目录] # 删除空目录
rm -rf [要删除的目录] # 删除非空目录
Note that the above rmdir
can only delete the empty directory, if there is content in the directory, it cannot be deleted. If you need to delete non-empty directories, you need to use rm -rf 要删除的目录
(r for recursive, f for force). Note that you must be very cautious when using it -rf
, or the whole system will crash if you delete some system files at once. Under Linux, it doesn't matter if you add files; you must be cautious about deleting files; you must be cautious about modifying files, and it is best to make a backup .
Two application examples are shown below:
# 【案例1】删除空目录/home/dog
rmdir /home/dog
# 【案例2】删除非空目录/home/animal
rm -rf /home/animal
6. touch
Instructions
# 基本语法-touch
touch 文件名称 # 创建空文件,并修改相应的时间索引。
An application example is shown below:
# 【案例1】在/home目录下,创建一个空文件hello.txt
touch /home/hello.txt
7. cp
Command (copy)
# 基本语法-cp
cp [选项] 待拷贝文件 指定目录 # 拷贝文件到指定目录。
# 常用选项-cp
-r 递归复制整个文件夹
Note that in the basic syntax, 指定目录
you can also specify a file at the end. If there is no such file, it will automatically create and copy the content. If the file exists, it will ask whether to overwrite y/n?
.
Two application examples are shown below:
# 【案例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
Command (Delete)
# 基本语法-rm
rm [选项] 要删除的文件或目录 # 移除文件或目录,注意会询问是否删除```y/n?```。
# 常用选项-rm
-r 递归删除整个文件夹
-f 强制删除不提示
Note that if you do not add options, rm
you will be asked whether to delete y/n?
. Therefore, according to the above content, it can be deduced that the meaning of the command to delete the database and run away rm -rf /*
is to directly delete all files in the root directory without prompting. Obviously, if there is no backup, the current Linux system will be directly abolished.
Two application examples are shown below:
# 【案例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
Instructions (cut, paste)
# 基本语法-mv
mv 旧文件/目录 新文件/目录 # 移动文件/目录并实现重命名
Note that in the above syntax, 新文件/目录
if it does not exist, it is moved and renamed; if 新文件/目录
it exists, it is simply moved. Note that if the two directories differ only in the last level, they are just renamed. If 新文件/目录
it has 旧文件/目录
the same name, it will ask whether to overwrite y/n?
.
Three application examples are shown below:
# 【案例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
Instructions (read only)
# 基本语法-cat
cat [选项] 要查看的文件 # 查看文件内容
# 常用选项-cat
-n 显示行号。
I used to view the file content before vim
, but since cat
the file content cannot be modified, it is recommended to use it to view some important filescat
. Note that cat
the command will directly display all the content of the file on the screen, so it is generally used in conjunction with more
the command (see the next section) to better manage the displayed content.
An application example is shown below:
# 【案例1】读取/etc/profile文件内容,并显示行号。
cat -n /etc/profile
cat -n /etc/profile | more # 见下方说明
If the file is too long, you can use the pipeline command +more command : | more
. Pipeline commands |
are somewhat similar to handing over the results obtained earlier to the next command for processing; while more
commands will display longer text in sections for easy reading. So the above "Application Example" can be entered cat -n /etc/profile | more
. At this time, only the first 43 lines of results will be displayed on the screen. Enter Enter to go to the next line, and enter the Space key to go to the next page...More operating instructions are in the next section, and more "Pipeline Commands" See " Section 9.6 - 4. The grep command and the pipe symbol |" for an explanation of .
11. more
Instructions
The command to be introduced in this section more
is a text filter based on the VI editor, which displays the content of the text file page by page in a full-screen manner. Note that it can be used in conjunction with the above cat
, or it can be used independently. more
The content displayed by the command can be operated with the following shortcut keys:
operate | Function Description |
---|---|
Space bar (space) | Turn down one page |
Enter | Represents turning down a line |
q | The representative leaves more immediately and no longer displays the content of the file |
Ctrl+F | scroll down one screen |
Ctrl+B | back to previous screen |
= | output the line number of the current line |
:f | output the filename and line number of the current line |
An application example is shown below:
# 【案例】使用more指令查看/etc/profile文件。
more /etc/profile
12. less
Instructions
# 基本语法-less
less 要查看的文件 # 分屏查看文件内容
The function of the above less
command more
is similar to the command but more powerful, and less
supports various display terminals. In addition, unlike vi
or vim
, the file is loaded first and then displayed, less
the command loads the content according to the display requirements when displaying the file content (similar to dynamic loading), which is more efficient for displaying large files . less
The content displayed by the command can be operated with the following shortcut keys:
operate | Function Description |
---|---|
space bar | scroll down one page |
[pageup] / [pagedown] | Turn up/down one page |
Arrow keys ↑ / ↓ | Scroll up/down a line |
/string | The function of searching [string] downward; n: search downward; N: search upward; |
?string | Function to search up [string]; n: search up; N: search down; |
q | leave the less program |
An application example is shown below:
[Material provided] A larger text file " Miscellaneous Text.txt ".
# 【案例】使用Xftp将“杂文.txt”上传到/opt目录下,并查看该文件。
less /opt/杂文.txt
13. echo
Instructions
# 基本语法
echo [选项] [输出内容] # 将输出内容展示到控制台
echo # 输出空行
The above echo
will often be encountered when writing Shell scripts later.
Two application examples are shown below:
# 【案例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
The environment variable includes the search path for the specified command, and the location of the executable program that the operating system needs to use. For a more detailed introduction, please refer to the CSDN blog post "[Linux] This article allows you to fully understand what is an environment variable " .
# 【案例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~
The above echo
cannot use double quotes to output an exclamation mark, because H - histexpand
the mode is turned on. Under the command line, if "!" is used in the double quotes, the Shell will think that it needs to execute the history expansion, which will cause an error (actually I don't understand this sentence, I will come back to make up after I learn it later). For details, please refer to the CSDN blog post " Linux -bash: !": event not found problem solving ".
14. head
Instructions
head
The command is used to display the content at the beginning of the file, and the first 10 lines of the file are displayed by default.
# 基本语法-head
head 文件 # 默认查看文件前10行内容
head -n x 文件 # 查看文件前x行内容
An application example is shown below:
# 【案例】查看/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
Instructions
On head
the contrary, tail
it is used to output the content at the end of the file, and the last 10 lines of the file are displayed by default.
# 基本语法-tail
tail 文件 # 默认查看文件尾10行内容
tail -n x 文件 # 查看文件尾x行内容
tail -f 文件 # 实时追踪该文档的所有更新
Two application examples are shown below:
[Case 1] View the last 5 lines of code in /etc/profile.
[root@CentOS76 ~]# tail -n 5 /etc/profile
fi
done
unset i
unset -f pathmunge
[Case 2] Create a hello.txt file under /home, and monitor it in real time during the process of adding "Hello!". To execute this case, two terminals are required, such as Xshell and VMware graphical interface terminals. To exit the real-time monitoring state, press the shortcut keyctrl+c
.
##########终端1########
# 1. 创建文件
touch /home/hello.txt
# 2. 输入实时监控指令
tail -f /home/hello.txt # 该指令输入完毕后,界面自动输出监控到的文件变化
##########终端1########
# 3. 添加内容到hello.txt末尾
echo 'Hello!' >> /home/hello.txt # 指令输入完,按下回车瞬间可在终端1查看文件变化情况
16. >
Directives and >>
Instructions
"Output redirection command >
" and "append command >>
" both output the content to be displayed on the terminal to a file, but in different ways and are usually used in conjunction with other commands . >
The file can be emptied and new content written to the file (overwrite); and >>
the content is added to the end of the document (append). Note that if the target file to be output above does not exist, it will be created automatically. >
Here is >>
the syntax for use with other directives:
# 基本语法->指令和>>指令
ls -l > 文件 # 将列表内容覆盖写入文件中
ls -al >> 文件 # 将列表内容追加到文件末尾
cat 文件1 > 文件2 # 将文件1的内容覆盖写入到文件2
cat 文件1 >> 文件2 # 将文件1的内容追加到文件2末尾
echo 内容 > 文件 # 将内容覆盖写入文件中
echo 内容 >> 文件 # 将内容追加到文件末尾
Two application examples are shown below: [Case 1] Overwrite
the file list in the /home directory to /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 7月 18 11:36 .
dr-xr-xr-x. 18 root root 4096 7月 17 11:49 ..
-rw-r--r--. 1 root root 7 7月 18 11:27 hello.txt
drwx------. 3 tom a 4096 7月 15 15:49 jack
drwx------. 5 jerry jerry 4096 7月 16 16:31 jerry
drwx------. 3 milan jerry 4096 7月 15 15:50 king
drwx------. 15 lyl lyl 4096 7月 15 10:59 lyl
drwx------. 5 zwj wudang 4096 7月 15 12:12 milan
-rw-r--r--. 1 root root 0 7月 18 11:36 myinfo.txt
drwx------. 3 zwj a 4096 7月 15 15:08 tom
drwx------. 3 jerry 1009 4096 7月 16 10:35 zwj
[Case 2] Append the current calendar information to the /home/myinfo.txt file.
Tip:
cal
The command displays the current date information.
[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 7月 18 11:36 .
dr-xr-xr-x. 18 root root 4096 7月 17 11:49 ..
-rw-r--r--. 1 root root 7 7月 18 11:27 hello.txt
drwx------. 3 tom a 4096 7月 15 15:49 jack
drwx------. 5 jerry jerry 4096 7月 16 16:31 jerry
drwx------. 3 milan jerry 4096 7月 15 15:50 king
drwx------. 15 lyl lyl 4096 7月 15 10:59 lyl
drwx------. 5 zwj wudang 4096 7月 15 12:12 milan
-rw-r--r--. 1 root root 0 7月 18 11:36 myinfo.txt
drwx------. 3 zwj a 4096 7月 15 15:08 tom
drwx------. 3 jerry 1009 4096 7月 16 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
Instructions
Soft links " ln
instructions" are also called symbolic links, which are similar to shortcuts in Windows, and mainly store the path to link other files. Visiting the link is equivalent to visiting its linked path. But need to pay attention to:
- When we use
pwd
the command to view the directory, we still see the directory where the soft link is located. Take/home/myroot -> /root
as an example, ifcd
it is located/home/myroot
, at this time, usingpwd
View Current Path still displays Yes/hoe/myroot
instead of/root
No.- The reason why the instruction is introduced
ln
is because there will be many such link files when you look at the "Dynamic Link Library" later, because many "Dynamic Link Libraries" are shared by many applications, and creating a link is the easiest way to call it.
# 基本语法-符号链接
ln -s [原文件或目录] [软链接名] # 给原文件创建一个软链接
Two application examples are shown below:
[Case 1] Create a soft link myroot in the /home directory and link to the /root directory.
[root@CentOS76 home]# ln -s /root /home/myroot
[root@CentOS76 home]# ls -l
总用量 36
-rw-r--r--. 1 root root 7 7月 18 11:27 hello.txt
drwx------. 3 tom a 4096 7月 15 15:49 jack
drwx------. 5 jerry jerry 4096 7月 16 16:31 jerry
drwx------. 3 milan jerry 4096 7月 15 15:50 king
drwx------. 15 lyl lyl 4096 7月 15 10:59 lyl
drwx------. 5 zwj wudang 4096 7月 15 12:12 milan
-rw-r--r--. 1 root root 2351 7月 18 11:43 myinfo.txt
lrwxrwxrwx. 1 root root 5 7月 18 11:50 myroot -> /root
drwx------. 3 zwj a 4096 7月 15 15:08 tom
drwx------. 3 jerry 1009 4096 7月 16 10:35 zwj
At this point, the directory /home/myroot
is equivalent to /root
.
[Case 2] Delete the soft link /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
Instructions
This section introduces history
commands, which are used to view historical commands that have been executed, and to execute historical commands.
# 基本语法-hostory
history # 显示所有历史命令
history x # 显示最近的x条指令
!x # 执行历史编号为x的指令
Three application examples are shown below:
[Case 1] Display all historical commands.
[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
[Case 2] Display the last 10 commands used.
[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
[Case 3] Execute the command whose history number is 451.
[root@CentOS76 myroot]# !451
pwd
/home/myroot
9.5 time and date class - date
/cal
This section introduces two date and time directives: date
, cal
.
date # 显示或修改当前日期、时间
cal # 显示日历
1. date
Command - display current date
date
The command has two functions: display date and set date.
# 基本语法-date显示日期
date # 显示当前时间
date +%Y # 显示当前年份
date +%m # 显示当前月份
date +%d # 显示当前日期
date "+%Y-%m-%d %H:%M:%S" # 显示年月日时分秒
date --help # 查看日期显示的更多用法
# 基本语法-date设置日期
date -s "字符串时间" # 设置系统时钟为字符串时间
Four application examples are shown below: the first three show the "display date" function, and the last one shows the "set date" function.
# 【案例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"
2021年 11月 11日 星期四 11:22:22 CST
[root@CentOS76 ~]# reboot # 重启并联网可以恢复时间
2. cal
Instructions
To view the calendar under Linux, use cal
the command (calendar, 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 # 显示所有选项信息
Two application examples are shown below:
# 【案例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 Search Find Class - find
/ locate
/ which
/grep
This section introduces four commands related to "searching for classes": find
, locate
, which
, grep
and pipe symbols |
.
find 实际搜寻硬盘查询文件名称。
locate 配合数据库查看文件位置。
which 查看可执行文件的位置。
grep 常配合管道符号搜索内容中的关键字。
# whereis 查看文件的位置。——老韩未介绍
1. find
Instructions
find
The instruction will recursively traverse each subdirectory from the specified directory, and display the files or directories that meet the conditions on the terminal. Obviously, it should be used in a small range of directories as much as possible find
to avoid too long search time.
# 基本语法-find
find [搜索范围] [选项] [具体要求] # 按照选型在指定范围查找文件
options | Function |
---|---|
-name<query method> | Find files according to the specified filename search pattern. |
-user<username> | Find all files belonging to the specified username. |
-size<file size> | Find the file size relationship according to the specified file size : +n means greater than n, -n means less than n, and n means equal to n. Units: k, M, G. |
The following shows three application examples in sequence according to the above command options:
[Case 1-By file name] Find the hello.txt file and all txt texts in the /home directory *.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
[Case 2 - By owner] Find the files in the /home directory with the user names nobody and 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
[Remaining questions] Why both jack and tom users exist and belong to the same group a, but can only find jack's files through tom? I remember that tom should be added to group a first, and their respective groups with the same name have been deleted.
[Case 3 - By file size] Find files larger than 100M in the entire Linux system.
[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 7月 8 2020 /usr/lib64/firefox/libxul.so
2. locate
Instructions
locate
Directives can quickly locate file paths . locate
The instruction does not need to traverse the entire file system, and the query speed is faster . The reason is that locate
the command uses the pre-established locate
database of all file names and paths in the system to quickly locate a given file, so the command must be used to create the database before the first runupdatedb
locate
, and in order to ensure the accuracy of the query results, the administrator must use it regularly updatedb
command to update locate
the database.
# 基本语法-locate
locate 搜索文件
An application example is shown below:
# 【案例1】请使用locate指令快速定位hello.txt文件所在目录。
[root@CentOS76 ~]# locate hello.txt # 显然不创建数据库什么也搜索不出来
[root@CentOS76 ~]# updatedb
[root@CentOS76 ~]# locate hello.txt
/home/hello.txt
3. which
Instruction
which
The function of the instruction is to search for the location of a certain system command in the path specified by the PATH variable, and return the first search result. In other words, which
you can check which directory a command is in.
# 基本语法-which
which 可执行文件名称 # 查找指令所在的目录
An application example is shown below:
# 【案例1】查看ls指令、reboot分别在哪个目录。
[root@CentOS76 ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
[root@CentOS76 ~]# which reboot
/usr/sbin/reboot
4. grep
Instructions and pipe symbols|
grep
The role of the instruction is to filter the search; the pipe symbol |
means to pass the processing result output of the previous command to the subsequent command processing. grep
Filter search is often used in combination with the pipe symbol |
, that is, the previously displayed content is |
passed to the following grep
command through the pipe symbol to find the corresponding content, such as cat a.txt | grep "hello"
searching for the line a.txt
where the string "hello"
is located in . Commands are often used grep
to check port usage.
# 基本语法-grep
grep [选项] 查找内容 源文件 # 在源文件中查找内容
# 常用选项-grep
-n 显示匹配行及行号
-i 忽略字母大小写
The following shows two ways to demonstrate the use grep
of search keywords through an application example:
Material: a large text file - Essay.txt .
# 【案例1】使用Xftp将“杂文.txt”上传到/opt目录下,并查找“见龙在田”所在的行号:
[root@CentOS76 ~]# cat /opt/杂文.txt | grep -n "见龙在田"
14711: 萧峰一招“见龙在田”,从旁拍击过去,判官笔为掌风所激,笔腰竟尔弯曲,从段誉脑后绕了个弯,向慕容复射了回去。
15416: 萧峰喝道:“你就想走?天下有这等便宜事?你父亲身上有病,大丈夫不屑乘人之危,且放了他过去。你可没病没痛!”慕容复气往上冲,喝道:“那我便接萧兄的高招。”萧峰更不打话,呼的一掌,一招降龙十八掌的“见龙在田”,向慕容复猛击过去。他见藏经阁地势险隘,高群集,不便久斗,是以使上了十成力,要在数掌之间便取了敌人性命。慕容复见他掌势凶恶,当即运起平生之力,要以“斗转星移”之术化解。
[root@CentOS76 ~]# grep -n "见龙在田" /opt/杂文.txt
14711: 萧峰一招“见龙在田”,从旁拍击过去,判官笔为掌风所激,笔腰竟尔弯曲,从段誉脑后绕了个弯,向慕容复射了回去。
15416: 萧峰喝道:“你就想走?天下有这等便宜事?你父亲身上有病,大丈夫不屑乘人之危,且放了他过去。你可没病没痛!”慕容复气往上冲,喝道:“那我便接萧兄的高招。”萧峰更不打话,呼的一掌,一招降龙十八掌的“见龙在田”,向慕容复猛击过去。他见藏经阁地势险隘,高群集,不便久斗,是以使上了十成力,要在数掌之间便取了敌人性命。慕容复见他掌势凶恶,当即运起平生之力,要以“斗转星移”之术化解。
9.7 Compression and decompression classes - gzip
/ gunzip
, zip
/ unzip
,tar
This section introduces three commands of the compression/decompression class: gzip
/ gunzip
, zip
/ unzip
, tar
.
gzip / gunzip gzip只能压缩文件成“.gz”文件,不能压缩目录,同时源文件会被删除。
# bzip2 / bunzip2 是gzip、gunzip命令的压缩比升级版,但也只能压缩文件成“.bz2”文件,不能压缩目录,可通过‘-k’保留源文件。
zip / unzip zip压缩文件或目录成“.zip”文件,源文件会被保留。
tar 将目录打包成一个“.tar”文件,同时结合其选项完成gzip、bzip2的目录压缩。源文件会被保留。
- For more information, please refer to the Zhihu article " zip, gzip, bzip2, tar are stupidly unclear ".
1. gzip
/ gunzip
command
gzip
It is used to compress files into ".gz" files, gunzip
and to decompress ".gz" files. The compression is relatively high (60% to 70% compression rate for text files). But it can only operate on a single file, and cannot compress a directory, and the source file will be deleted at the same time .
# 基本语法-gzip/gunzip
gzip 文件 # 压缩文件,只能将文件压缩为*.gz文件
gunzip .gz文件 # 解压缩.gz文件
An application example is shown below:
# 【案例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
command
zip
It is used to compress files into ".zip" files and unzip
to decompress ".zip" files, but the compression ratio is not very high . The advantage is that not only files can be compressed, but directories can also be compressed, and the source files will be preserved . When developing, the entire folder is often packaged and released, which zip
is very useful at this time.
# 基本语法-zip/unzip
zip [选项] 压缩后的文件路径及名称 要压缩的内容 # 压缩文件/目录成.zip文件
unzip [选项] 需要解压的.zip文件 # 解压缩文件
# zip常用选项
-r :递归压缩,即压缩整个目录
# unzip的常用选项
-d<目录> :指定解压后文件的存放目录
Two application examples are shown below:
# 【案例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
Instructions
tar
The instruction can pack a single file or multiple files or directories into a ".tar" file, or unpack the ".tar" file, so as to cooperate with the gzip/gunzip
instruction to complete the compression/decompression of the entire directory. However, the above process will not allow the user to call the command directly gzip/gunzip
, but directly use the option " -z
compress/decompress while packing". So when packing, tar
the instruction will compress the packed ".tar" file into a ".tar.gz" file, and when unpacking, it will first decompress ".tar.gz" into a ".tar" file and then unpack it. Bag. So tar
the command is the most commonly used compression/decompression command in Linux , and the source file will be preserved.
# 基本语法-tar
tar [选项] 打包后的名称XXX.tar.gz 要打包的内容 # 打包目录,压缩后的文件格式.tar.gz
# 注:压缩多个文件后面接着打空格即可。
# 选项说明-tar
-c 打包成.tar文件
-x 解包.tar文件
-v 显示详细信息
-f 指定文件名
-z 打包同时压缩/解压缩
Four application examples are shown below:
[Case 1] Compress multiple files, /home/pig.txt
and /home/cat.txt
compress them into 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
[Case 2] /home
Compress the directory into 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
[Case 3] pc.tar.gz
Unzip to the /home directory.
[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
【Case 4】myhome.tar.gz
Unzip it to /opt/tmp2
a directory.
[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