Linux就该这么学——Day2

2.1 Shell

通常来讲,计算机硬件是由运算器、控制器、存储器、输入/输出设备等共同组成的,Linux 系统的内核负责完成对硬件资源的分配、调度等管理任务,让各种硬件设备各司其职且又能协同运行。因此,系统内核对计算机的正常运行是至关重要的,一般不建议直接去编辑内核中的参数,而是让用户通过基于系统调用接口开发出的程序或服务来管理计算机,以满足日常工作的需要。如下图所示。

用户与Linux系统的交互
Shell 就是这样的一个命令行工具。用户把一些命令“告诉”Shell(也称为终端或壳),它就会调用相应的程序服务去完成某些工作,充当的是人与内核(硬件)之间的翻译官。现在包括红帽系统在内的许多主流 Linux 系统默认使用的终端是 Bash(Bourne-Again SHell)解释器。

用户 ⇒ Shell(Bash)⇒ 程序或服务 ⇒ 系统调用接口 ⇒ 内核 ⇒ 硬件

2.2 帮助命令——man

常见执行 Linux 命令的格式是这样的:

命令名称 [命令参数] [命令对象]

命令参数的长格式与短格式示例:

格式示例
长格式man --help
短格式man -h

在 man 命令帮助信息的界面中,所包含的常用操作按键及其用途,见下表。

按键用途
空格键向下翻一页
PaGe down向下翻一页
PaGe up向上翻一页
home直接前往首页
end直接前往尾页
/从上至下搜索某个关键词,如“/linux”
?从下至上搜索某个关键词,如“?linux”
n定位到下一个搜索到的关键词
N定位到上一个搜索到的关键词
q退出帮助文档

man 命令帮助信息的结构以及意义,见下表。

结构名称代表意义
NAME命令的名称
SYNOPSIS参数的大致使用方法
DESCRIPTION介绍说明
EXAMPLES演示(附带简单说明)
OVERVIEW概述
DEFAULTS默认的功能
OPTIONS具体的可用选项(带介绍)
ENVIRONMENT环境变量
FILES用到的文件
SEE ALSO相关的资料
HISTORY维护历史与联系方式

2.3 常用系统工作命令

2.3.1 echo 命令

用途:输出字符串或提取Shell变量的值。

语法:echo [参数] [字符串]

参数描述
-n不输出结尾的换行符
-e “\a”发出警告音
-e “\b”删除前面的一个字符
-e “\c”结尾不加换行符
-e “\f”换行,光标扔停留在原来的坐标位置
-e “\n”换行,光标移至行首
-e “\r”光标移至行首,但不换行
-E禁止反斜杠转义,与-e参数功能相反
–version查看版本信息
–help查看帮助信息

实例:
1)输出字符串:

miya@asus-x550ld:~$ echo "hello world"
hello world

2)输出变量值:

miya@asus-x550ld:~$ echo $SHELL 
/bin/bash

3)使用反斜杠\$进行转义:

miya@asus-x550ld:~$ echo \$SHELL
$SHELL

4)使用反引号符执行命令,并输出其结果到终端:

miya@asus-x550ld:~$ echo `date`
2021年 2月 7日 日曜日 11:06:30 JST
miya@asus-x550ld:~$ 
miya@asus-x550ld:~$ echo `date "+%Y-%m-%d %H:%M:%S"`
2021-02-07 11:07:18

5)使用参数-e+\n,输出字符串并实现换行:

miya@asus-x550ld:~$ echo -e "a\nb\nc"
a
b
c

6)使用参数-e+\b,删除前一个字符:

miya@asus-x550ld:~$ echo -e "123\b456" 
12456

2.3.2 date 命令

用途:显示或设定系统的日期与时间。

语法:echo [参数] [+输出格式]

参数描述
-d datestr显示 datestr 中所设定的时间 (非系统时间)
-s datestr将系统时间设为 datestr 中所设定的时间
-u显示目前的格林威治时间
–help显示帮助信息
–version显示版本编号
输出格式作用
%t跳格[Tab 键]
%H小时(00~23)
%I小时(00~12)
%M分钟(00~59)
%S秒(00~59)
%j今年中的第几天

示例:
1)显示当前时间:

miya@asus-x550ld:~$ date
2021年  2月  7日 日曜日 11:32:23 JST
miya@asus-x550ld:~$ 
miya@asus-x550ld:~$ date -u
2021年  2月  7日 日曜日 02:32:27 UTC

2)按照特定格式输出时间:

miya@asus-x550ld:~$ date '+%c'
2021年02月07日 11時32分55秒

3)按自己的格式输出:

miya@asus-x550ld:~$ date '+usr_time: $1:%M %P -hey'
usr_time: $1:33 午前 -hey

4)显示时间后跳行,再显示目前日期:

miya@asus-x550ld:~$ date '+%T%n%D'
11:33:50
02/07/21

5)显示月份与日数:

miya@asus-x550ld:~$ date '+%B %d'
2月 07

6)显示日期与设定时间(12:34:56):

miya@asus-x550ld:~$ date --date '12:34:56'
2021年  2月  7日 日曜日 12:34:56 JST

2.3.3 reboot 命令

reboot 命令用于重启系统。由于重启计算机这种操作会涉及硬件资源的管理权限,因此默认只能使用 root 管理员来重启。

2.3.4 poweroff 命令

poweroff 命令用于关闭系统。该命令与 reboot 命令相同,都会涉及硬件资源的管理权限,因此默认只有 root 管理员才可以关闭电脑。

2.3.5 wget 命令

用途:从指定的URL下载文件。

特点:wget非常稳定,它在带宽很窄的情况下和不稳定网络中有很强的适应性,如果是由于网络的原因下载失败,wget会不断的尝试,直到整个文件下载完毕。如果是服务器打断下载过程,它会再次联到服务器上从停止的地方继续下载。这对从那些限定了链接时间的服务器上下载大文件非常有用。
wget支持HTTP,HTTPS和FTP协议,可以使用HTTP代理。所谓的自动下载是指,wget可以在用户退出系统的之后在后台执行。这意味这你可以登录系统,启动一个wget下载任务,然后退出系统,wget将在后台执行直到任务完成,相对于其它大部分浏览器在下载大量数据时需要用户一直的参与,这省去了极大的麻烦。

语法:

参数作用
-b后台下载模式
-P下载到指定目录
-t最大尝试次数
-c断点续传
-p下载页面内所有资源,包括图片、视频等
-r递归下载

示例:
1)使用wget从网上下载文档、软件、音乐、视频:

miya@asus-x550ld:~$ wget http://www.linuxprobe.com/docs/LinuxProbe.pdf
--2021-02-07 22:42:38--  http://www.linuxprobe.com/docs/LinuxProbe.pdf
Resolving www.linuxprobe.com (www.linuxprobe.com)... 47.89.66.205, 47.89.66.200, 47.89.66.203, ...
Connecting to www.linuxprobe.com (www.linuxprobe.com)|47.89.66.205|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://www.linuxprobe.com/docs/LinuxProbe.pdf [following]
--2021-02-07 22:42:38--  https://www.linuxprobe.com/docs/LinuxProbe.pdf
Connecting to www.linuxprobe.com (www.linuxprobe.com)|47.89.66.205|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 17676281 (17M) [application/pdf]
Saving to: ‘LinuxProbe.pdf’

LinuxProbe.pdf                    100%[============================================================>]  16.86M  3.57MB/s    in 5.2s    

2021-02-07 22:42:45 (3.24 MB/s) - ‘LinuxProbe.pdf’ saved [17676281/17676281]

2)下载文件并以指定的文件名保存文件:

miya@asus-x550ld:~$ wget -O test.zip http://www.Linuxcool.com

3)使用wget后台下载:

miya@asus-x550ld:~$ wget -b http://www.linuxcool.com/test.zip

Continuing in background, pid 1840.
Output will be written to `wget-log'.

4)递归下载 www.linuxprobe.com 网站内的所有页面数据以及文件:

miya@asus-x550ld:~$ wget -r -p http://www.linuxprobe.com 
--2017-08-24 19:31:41-- http://www.linuxprobe.com/ 
Resolving www.linuxprobe.com... 106.185.25.197 
Connecting to www.linuxprobe.com|106.185.25.197|:80... connected. 
HTTP request sent, awaiting response... 200 OK 
Length: unspecified [text/html] 
Saving to: 'www.linuxprobe.com/index.html' 
………………省略下载过程……………… 

2.3.6 ps 命令

用途:显示进程状态。

Linux 系统中时刻运行着许多进程,如果能够合理地管理它们,则可以优化系统的性能。在Linux 系统中,有 5 种常见的进程状态,分别为运行、中断、不可中断、僵死与停止,其各自含义如下所示。

  • R(运行):进程正在运行或在运行队列中等待。
  • S(中断):进程处于休眠中,当某个条件形成后或者接收到信号时,则脱离该状态。
  • D(不可中断):进程不响应系统异步信号,即便用 kill 命令也不能将其中断。
  • Z(僵死):进程已经终止,但进程描述符依然存在, 直到父进程调用 wait4()系统函数后将进程释放。
  • T(停止):进程收到停止信号后停止

语法:ps [参数]

参数作用
-a显示所有进程(包括其他用户的进程)
-A显示所有程序
-e此选项的效果和指定”A”选项相同
-f显示UID,PPIP,C与STIME栏位
-u用户以及其他详细信息
-x显示没有控制终端的进程

示例:
1)显示所有程序:

miya@asus-x550ld:~$ ps -aux
miya@asus-x550ld:~$ ps -A    

2)查找特定进程信息:

miya@asus-x550ld:~$ ps -ef | grep sshd

2.3.7 top 命令

用途:实时显示系统中各个进程的资源占用状况,常用于服务端性能分析。

语法:top [参数]

参数描述
-d改变显示的更新速度,或是在交谈式指令列( interactive command)按 s
-q没有任何延迟的显示速度,如果使用者是有 superuser 的权限,则 top 将会以最高的优先序执行
-c切换显示模式,共有两种模式,一是只显示执行档的名称,另一种是显示完整的路径与名称
-s安全模式,将交谈式指令取消, 避免潜在的危机
-S累积模式,会将己完成或消失的子行程 ( dead child process ) 的 CPU time 累积起来
-i不显示任何闲置 (idle) 或无用 (zombie) 的行程
-n更新的次数,完成后将会退出 top
-b批次档模式,搭配 “n” 参数一起使用,可以用来将 top 的结果输出到档案内

示例:
1)显示进程信息:

miya@asus-x550ld:~$ top
top - 23:10:38 up 12:40,  1 user,  load average: 1.64, 1.53, 1.29
Tasks: 266 total,   1 running, 264 sleeping,   0 stopped,   1 zombie
%Cpu(s): 19.3 us,  2.7 sy,  0.0 ni, 76.6 id,  0.3 wa,  0.0 hi,  1.1 si,  0.0 st
MiB Mem :   3819.4 total,    188.8 free,   2097.9 used,   1532.6 buff/cache
MiB Swap:   2048.0 total,   2019.7 free,     28.2 used.   1009.5 avail Mem 

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND                                                         
  11452 miya      20   0 5221116 598844 294856 S  49.7  15.3  11:48.61 chrome                                                          
   3832 miya      20   0  607492 193580 110704 S   7.0   4.9  94:19.39 chrome                                                          
   5716 miya      20   0 4694804 160404  93324 S   5.3   4.1   0:51.95 chrome                                                          
   4106 miya      20   0  651796  61664  49212 S   5.0   1.6   9:03.71 chrome                                                          
   3875 miya      20   0 4876256 281380 108200 S   4.0   7.2  17:34.80 chrome                                                          
   6915 miya       9 -11 1934544  16932  12756 S   4.0   0.4   9:43.94 pulseaudio                                                      
   2468 miya      20   0 1159268  86388  46796 S   3.3   2.2  28:40.27 Xorg                                                            
   2600 miya      20   0 4214188 259868  83644 S   3.0   6.6  77:16.78 gnome-shell                                                     
   3793 miya      20   0  923500 245712 131124 S   3.0   6.3  12:44.43 chrome                                                          
   2623 miya      20   0  323192  19636   6732 S   2.7   0.5   1:10.60 ibus-daemon                                                     
   3837 miya      20   0  362880  90488  62268 S   2.3   2.3   4:28.93 chrome                                                          
  11547 miya      20   0 4707076 160296 105444 S   2.3   4.1   0:21.39 chrome                                                          
   2628 miya      20   0  276768  31468  17320 S   1.3   0.8   0:25.32 ibus-extension-                                                 
   2887 miya      20   0  277260  34944  15472 S   1.3   0.9   0:22.25 ibus-engine-lib                                                 
   5061 miya      20   0  935096  54944  41064 S   1.3   1.4   0:13.20 gnome-terminal-                                                 
   4439 miya      20   0  533808 287408 106944 S   0.7   7.3   8:09.97 wpspdf                                                          
     11 root      20   0       0      0      0 I   0.3   0.0   0:49.71 rcu_sched                                                       
   2644 miya      20   0  162868   7496   6720 S   0.3   0.2   0:03.33 at-spi2-registr                                                 
   3958 miya      20   0 4600332  98976  68084 S   0.3   2.5   2:56.30 chrome                                                          
  12438 root      20   0   16332   4356   3632 R   0.3   0.1   0:01.14 top                                                             
      1 root      20   0  167844  10836   7632 S   0.0   0.3   0:33.94 systemd                                                         
      2 root      20   0       0      0      0 S   0.0   0.0   0:00.01 kthreadd                                                        
      3 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_gp                                                          
      4 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_par_gp                                                      
      6 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 kworker/0:0H-kblockd                                            
      9 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 mm_percpu_wq                                                    
     10 root      20   0       0      0      0 S   0.0   0.0   0:00.61 ksoftirqd/0                                                     
     12 root      rt   0       0      0      0 S   0.0   0.0   0:00.17 migration/0                                                     
     13 root     -51   0       0      0      0 S   0.0   0.0   0:00.00 idle_inject/0
  • 第 1 行:系统时间、运行时间、登录终端数、系统负载(三个数值分别为 1 分钟、5
    分钟、15 分钟内的平均值,数值越小意味着负载越低)。
  • 第 2 行:进程总数、运行中的进程数、睡眠中的进程数、停止的进程数、僵死的进
    数。
  • 第 3 行:用户占用资源百分比、系统内核占用资源百分比、改变过优先级的进程资源
    百分比、空闲的资源百分。

2)显示完整的进程信息:

miya@asus-x550ld:~$ top -c

3)以批处理模式显示程序信息:

miya@asus-x550ld:~$ top -b

4)设置信息更新次数:

miya@asus-x550ld:~$ top -n 2  # 表示更新两次后终止更新显示

5)设置信息更新时间:

miya@asus-x550ld:~$ top -d 3   # 表示更新周期为3秒

6)显示指定的进程信息:

miya@asus-x550ld:~$ top -p 139  # 显示进程号为139的进程信息,CPU、内存占用率等

7)显示更新10次后退出:

miya@asus-x550ld:~$ top -n 10

8)使用者将不能利用交谈式指令来对行程下命令:

miya@asus-x550ld:~$ top -s

3.3.8 pidof 命令

用途:pidof 命令用于查询某个指定服务进程的 PID 值。

格式:pidof [参数] [服务名称]

示例:

miya@asus-x550ld:~$ pidof sshd
1091

3.3.9 kill 命令

用途:终止某个指定 PID 的服务进程。

格式:kill [参数] [进程 PID]

示例:

miya@asus-x550ld:~$ kill -9 1091

3.3.10 killall 命令

用途:通常来讲,复杂软件的服务程序会有多个进程协同为用户提供服务,如果逐个去结束这些进程会比较麻烦,此时可以使用 killall 命令来批量结束某个服务程序带有的全部进程。

格式:killall [参数] [进程名称]

示例:

miya@asus-x550ld:~$ pidof httpd 
13581 13580 13579 13578 13577 13576 
miya@asus-x550ld:~$ killall httpd 
miya@asus-x550ld:~$ pidof httpd 
miya@asus-x550ld:~$

2.4 系统状态检测命令

2.4.1 ifconfig 命令

用途:于获取网卡配置与网络状态等信息

格式:ifconfig [网络设备] [参数]

参数描述
add<地址>设置网络设备IPv6的IP地址
del<地址>删除网络设备IPv6的IP地址
down关闭指定的网络设备
up启动指定的网络设备
IP地址指定网络设备的IP地址

示例:
1)显示网络设备信息:

miya@asus-x550ld:~$ ifconfig
br-a0935eee0ee9: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.18.0.1  netmask 255.255.0.0  broadcast 172.18.255.255
        ether 02:42:52:a4:04:a3  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:a1:e3:22:43  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp2s0f1: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether 10:c3:7b:23:44:82  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 12805  bytes 1238508 (1.2 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 12805  bytes 1238508 (1.2 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vmnet1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.159.1  netmask 255.255.255.0  broadcast 172.16.159.255
        inet6 fe80::250:56ff:fec0:1  prefixlen 64  scopeid 0x20<link>
        ether 00:50:56:c0:00:01  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2396  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vmnet8: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.6.1  netmask 255.255.255.0  broadcast 192.168.6.255
        inet6 fe80::250:56ff:fec0:8  prefixlen 64  scopeid 0x20<link>
        ether 00:50:56:c0:00:08  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2394  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlp3s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.8  netmask 255.255.255.0  broadcast 192.168.0.255
        inet6 2408:210:b08f:3400:49f2:bb36:8af3:d6b9  prefixlen 64  scopeid 0x0<global>
        inet6 2408:210:b08f:3400:39c3:b542:e3df:646d  prefixlen 64  scopeid 0x0<global>
        inet6 fe80::8ed3:8d02:ce93:ba65  prefixlen 64  scopeid 0x20<link>
        ether b8:ee:65:63:d9:84  txqueuelen 1000  (Ethernet)
        RX packets 6290569  bytes 8575430943 (8.5 GB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1686544  bytes 193904518 (193.9 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

2)启动关闭指定网卡:

ifconfig eth0 down
ifconfig eth0 up 

3)为网卡配置和删除IPv6地址:

ifconfig eth0 add 33ffe:3240:800:1005::2/64
ifconfig eth0 del 33ffe:3240:800:1005::2/64

4)用ifconfig修改MAC地址:

ifconfig eth0 down
ifconfig eth0 hw ether 00:AA:BB:CC:DD:EE
ifconfig eth0 up
ifconfig eth1 hw ether 00:1D:1C:1D:1E 
ifconfig eth1 up

5)配置IP地址:

ifconfig eth0 192.168.1.56 
ifconfig eth0 192.168.1.56 netmask 255.255.255.0
ifconfig eth0 192.168.1.56 netmask 255.255.255.0 broadcast 192.168.1.255

2.4.2 uname 命令

用途:查看系统内核与系统版本等信息。

格式:uname [-a]
-a:查看完整信息

miya@asus-x550ld:~$ uname -a
Linux asus-x550ld 5.4.0-65-generic #73-Ubuntu SMP Mon Jan 18 17:25:17 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

顺带一提,如果要查看当前系统版本的详细信息,则需要查看 redhat-release 文件,其命令以及相应的结果如下:

miya@asus-x550ld:~$ cat /etc/redhat-release
Red Hat Enterprise Linux Server release 7.0 (Maipo)

2.4.3 uptime 命令

用途:查看系统的负载信息。可以显示当前系统时间、系统已运行时间、启用终端数量以
及平均负载值等信息。平均负载值指的是系统在最近 1 分钟、5 分钟、15 分钟内的压力情
况(下面加粗的信息部分);负载值越低越好,尽量不要长期超过 1,在生产环境中不要
超过 5。

miya@asus-x550ld:~$ uptime 
22:49:55 up 10 min, 2 users, load average: 0.01, 0.19, 0.18

2.4.4 free 命令

用途:显示当前系统中内存的使用量信息。

格式:free [-h]
-h:以更人性化的方式输出当前内存的实时使用量信息。

miya@asus-x550ld:~$ free -h
              total        used        free      shared  buff/cache   available
Mem:          3.7Gi       1.9Gi       135Mi       436Mi       1.7Gi       1.2Gi
Swap:         2.0Gi        76Mi       1.9Gi

2.4.5 who 命令

用途:查看当前登入主机的用户终端信息。

格式:who [参数]

示例:

miya@asus-x550ld:~$ who
miya     :0           2021-02-13 21:03 (:0)

2.4.6 last 命令

用途:查看所有系统的登录记录。
但是,由于这些信息都是以日志文件的形式保
存在系统中,因此黑客可以很容易地对内容进行篡改。千万不要单纯以该命令的输出信息而判断系统有无被恶意入侵!

格式:last [参数]

示例:

miya@asus-x550ld:~$ last
miya     :0           :0               Sat Feb 13 21:03   still logged in
reboot   system boot  5.4.0-65-generic Sat Feb 13 20:56   still running
miya     :0           :0               Mon Feb  8 07:37 - down  (3+03:03)
reboot   system boot  5.4.0-65-generic Mon Feb  8 07:35 - 10:40 (3+03:05)
miya     :0           :0               Sun Feb  7 10:32 - 23:26  (12:53)
reboot   system boot  5.4.0-65-generic Sun Feb  7 10:30 - 23:26  (12:56)
miya     :0           :0               Sun Feb  7 09:00 - crash  (01:30)
reboot   system boot  5.4.0-65-generic Sun Feb  7 08:58 - 23:26  (14:28)
miya     :0           :0               Wed Feb  3 20:59 - down  (3+02:11)
reboot   system boot  5.4.0-65-generic Wed Feb  3 20:58 - 23:11 (3+02:12)
miya     :0           :0               Wed Feb  3 08:07 - down   (00:24)
reboot   system boot  5.4.0-65-generic Wed Feb  3 08:06 - 08:32  (00:26)
miya     :0           :0               Tue Feb  2 20:48 - down   (02:38)
reboot   system boot  5.4.0-65-generic Tue Feb  2 20:46 - 23:26  (02:40)
miya     :0           :0               Mon Feb  1 07:21 - down  (1+01:20)
reboot   system boot  5.4.0-65-generic Mon Feb  1 07:19 - 08:41 (1+01:22)
………………省略部分登录信息……………… 
miya@asus-x550ld:~$ 

2.4.7 history 命令

用途:显示历史执行过的命令。

格式:history [-c]
-c:清空所有的命令历史记录
还可以使用“!编码数字”的方式来重复执行某一次的命令。

历史命令会被保存到用户家目录中的.bash_history文件中。Linux 系统中以点(.)开头的文件均代表隐藏文件,这些文件大多数为系统服务文件,可以用 cat 命令查看其文件内容。

miya@asus-x550ld:~$ cat ~/.bash_history 

示例:

miya@asus-x550ld:~$ history 
1 tar xzvf VMwareTools-9.9.0-2304977.tar.gz 
2 cd vmware-tools-distrib/ 
3 ls 
4 ./vmware-install.pl -d 
5 reboot 
6 df -h 
7 cd /run/media/ 
8 ls 
9 cd root/ 
10 ls 
11 cd VMware\ Tools/ 
12 ls 
13 cp VMwareTools-9.9.0-2304977.tar.gz /home 
14 cd /home 
15 ls
16 tar xzvf VMwareTools-9.9.0-2304977.tar.gz 
17 cd vmware-tools-distrib/ 
18 ls 
19 ./vmware-install.pl -d 
20 reboot 
21 history 
miya@asus-x550ld:~$ !15 
anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates 
Desktop Downloads Music Public Videos 

2.4.8 sosreport 命令

用途:收集系统配置及架构信息并输出诊断文档,格式为 sosreport。
当 Linux 系统出现故障需要联系技术支持人员时,大多数时候都要先使用这个命令来简单收集系统的运行状态和服务配置信息,以便让技术支持人员能够远程解决一些小问题,亦或让他们能提前了解某些复杂问题。在下面的输出信息中,将收集好的资料压缩文件以及校验码发送给技术支持人员即可:

miya@asus-x550ld:~$ sosreport 
sosreport (version 3.0) 
This command will collect diagnostic and configuration information from 
this Red Hat Enterprise Linux system and installed applications. 
An archive containing the collected information will be generated in 
/var/tmp and may be provided to a Red Hat support representative. 
Any information provided to Red Hat will be treated in accordance with 
the published support policies at: 
https://access.redhat.com/support/ 
The generated archive may contain data considered sensitive and its 
content should be reviewed by the originating organization before being 
passed to any third party. 
No changes will be made to system configuration. 
Press ENTER to continue, or CTRL-C to quit. 此处敲击回车来确认收集信息 
Please enter your first initial and last name [linuxprobe.com]: 此处敲击回车来确认主机编号
Please enter the case number that you are generating this report for: 此处敲击回车来确认主机编号
Running plugins. Please wait ... 
Running 70/70: yum... 
Creating compressed archive... 
Your sosreport has been generated and saved in: 
/var/tmp/sosreport-linuxprobe.com-20170905230631.tar.xz
The checksum is: 79436cdf791327040efde48c452c6322
Please send this file to your support representative. 

2.5 工作目录切换命令

2.5.1 pwd 命令

print working directory的缩写,显示当前工作目录的绝对路径。

2.5.2 cd 命令

用途:切换工作路径

格式:cd [目录名称]

参数描述
~切换至用户的家目录
-返回之前所在目录
返回上一级目录

2.5.3 ls 命令

用途:显示目录中的文件信息

格式:ls [选项] [文件]

参数描述
-a看到全部文件(包括隐藏文件)
-d查看目录属性信息
-l查看文件的属性、大小等详细信息
miya@asus-x550ld:~$ ls -al
total 17452
drwx------  26 root root     4096  2月 15 22:02  .
drwxr-xr-x  24 root root     4096  7月 11  2020  ..
drwx------   4 root root     4096  2月 17  2020  .ansible
-rw-------   1 root root    32388  2月 15 22:02  .bash_history
-rw-r--r--   1 root root     3106  4月  9  2018  .bashrc
drwx------  19 root root     4096  7月 20  2020  .cache
drwx------  16 root root     4096  2月  7 23:05  .config
drwx------   3 root root     4096 12月 18  2019  .dbus
drwxr-xr-x   2 root root     4096 11月  3  2019  Desktop
-rw-r--r--   1 root root      481  3月  5  2020  docker-compose.yml
drwxr-xr-x   2 root root     4096 10月  2  2019  Documents
drwxr-xr-x   2 root root     4096 10月  2  2019  Downloads
drwx------   3 root root     4096 10月  2  2019  .gnupg
-rw-------   1 root root     1014 10月  2  2019  .ICEauthority
-rw-r--r--   1 root root 17676281  9月 23 00:13  LinuxProbe.pdf
drwx------   3 root root     4096 10月  2  2019  .local
drwx------   2 root root     4096 10月  2  2019  .mozc
drwx------   5 root root     4096 10月  2  2019  .mozilla
drwxr-xr-x   2 root root     4096 10月  2  2019  Music
drwxr-xr-x 117 root root     4096 11月 23  2019  .npm
drwxr-xr-x   2 root root     4096 10月  2  2019  Pictures
-rw-r--r--   1 root root      161 12月  5  2019  .profile
drwxr-xr-x   2 root root     4096 10月  2  2019  Public
-rw-------   1 root root        0  3月 21  2020  .python_history
drwxr-xr-x   2 root root     4096  2月 25  2020  .rpmdb
-rw-r--r--   1 root root       66 10月  6  2019  .selected_editor
drwxr-xr-x   4 root root     4096  2月 13 20:59  snap
drwx------   2 root root     4096  7月 12  2020  .ssh
drwxr-xr-x   2 root root     4096  1月 18  2020  .swt
drwxr-xr-x   2 root root     4096 11月  3  2019  Templates
drwxr-xr-x   2 root root     4096 10月  2  2019  Videos
drwxr-xr-x   2 root root     4096  3月  7  2020  .vim
-rw-------   1 root root    24938  7月 12  2020  .viminfo
drwxr-xr-x   2 root root     4096  2月 17  2020 'VirtualBox VMs'
drwxr-xr-x   2 root root     4096  9月 14 19:12  .vmware
-rw-r--r--   1 root root      213  3月 28  2020  .wget-hsts

2.6 文本文件编辑命令

2.6.1 cat 命令

用途:查看纯文本文件(内容较少的)。

格式:cat [选项] [文件]

参数描述
-n显示行数(空行也编号)
-s显示行数(多个空行算一个编号)
-b显示行数(空行不编号)

示例:
1)持续写入文件内容,碰到EOF符后结束并保存:

miya@asus-x550ld:~/Desktop$ cat > catfile.txt <<EOF
> hello
> world
> EOF
miya@asus-x550ld:~/Desktop$ cat catfile.txt 
hello
world

2)清空文件的内容:

miya@asus-x550ld:~/Desktop$ cat /dev/null >catfile.txt 
miya@asus-x550ld:~/Desktop$ cat catfile.txt 
miya@asus-x550ld:~/Desktop$ 

2.6.2 more 命令

用途:于查看纯文本文件(内容较多的)。

格式:more [参数] [文件]

参数描述
-num指定每屏显示的行数
-lmore在通常情况下把 ^L 当作特殊字符, 遇到这个字符就会暂停,-l选项可以阻止这种特性
-f计算实际的行数,而非自动换行的行数
-p先清除屏幕再显示文本文件的剩余内容
-c与-p相似,不滚屏,先显示内容再清除旧内容
-s多个空行压缩成一行显示
-u禁止下划线
+/pattern在每个文档显示前搜寻该字(pattern),然后从该字串之后开始显示
+num从第 num 行开始显示

命令内部操作:
Space键:显示文本的下一屏内容
Enter键:向下n行,需要定义,默认为1行
\键:接着输入一个模式,可以在文本中寻找下一个相匹配的模式
H键:显示帮助屏
B键:显示上一屏内容
Q键:退出more命令
Ctrl+F空格键:向下滚动一屏
Ctrl+B:返回上一屏
=: 输出当前的行号
:f:输出文件名和当前的行号
V:调用vi编辑器
!:调用Shell,并执行命令

2.6.3 head 命令

用途:查看纯文本文档的前 N 行。

格式:head [选项] [文件]

参数描述
-n后面接数字,代表显示几行的意思
-c指定显示头部内容的字符数
-v总是显示文件名的头信息
-q不显示文件名的头信息

2.6.4 tail 命令

用途:查看纯文本文档的后 N 行或持续刷新内容

格式:tail [选项] [文件]

参数描述
--retrytail命令启动时,文件不可访问或者文件稍后变得不可访问,始终尝试打开文件。使用此选项时需要与选项“--follow=name”连用
-c<N>或--bytes=<N>输出文件尾部的N(N为整数)个字节内容
-f<name/descriptor>或--follow<name/descript>显示文件最新追加的内容
-F与选项“--follow=name”和“--retry”连用时功能相同
-n<N>或--line=<N>输出文件的尾部N(N为数字)行内容
--pid=<进程号>与“-f”选项连用,当指定的进程号的进程终止后,自动退出tail命令

示例:
1)显示文件file的最后10行:

miya@asus-x550ld:~$ tail file

2)显示文件file的内容,从第20行至文件末尾:

miya@asus-x550ld:~$ tail +20 file 

3)显示文件file的最后10个字符:

miya@asus-x550ld:~$ tail -c 10 file 

4)一直变化的文件总是显示后10行:

miya@asus-x550ld:~$ tail -f 10 file

2.6.5 tr 命令

用途:于替换文本文件中的字符。

格式:tr [原始字符] [目标字符]

示例:
1)把某个文本内容中的英文全部替换为大写:

cat anaconda-ks.cfg | tr [a-z] [A-Z] 

2.6.6 wc 命令

用途:于统计指定文本的行数、字数、字节数。

格式:wc [参数] 文本

参数描述
-w统计字数,或--words:只显示字数(仅英文,以空格为分割)。
-c统计字节数,或--bytes或--chars:只显示Bytes数(中文除以2得到字数)。
-l统计行数,或--lines:只显示列数
-m统计字符数
-L打印最长行的长度

示例:
1)统计字数:

miya@asus-x550ld:~$ cat test.txt 
hello world
hello world
hello world
hello world hello world
miya@asus-x550ld:~$ wc -w test.txt 
10 test.txt

2)统计字节数:

miya@asus-x550ld:~$ wc -c test.txt 
60 test.txt

3)统计字符数:

miya@asus-x550ld:~$ wc -m test.txt 
60 test.txt

4)统计行数:

miya@asus-x550ld:~$ wc -l test.txt 
4 test.txt

5)打印最长行的长度:

miya@asus-x550ld:~$ wc -L test.txt 
23 test.txt

2.6.7 stat 命令

用途:查看文件的具体存储信息和时间等信息。

  • ATIME(Access Time):最后一次查看文件内容的时间
  • MTIME(Modify Time):最后一次修改文件内容的时间
  • CTIME(Change Time):最后一次更改文件属性的时间

格式:stat [参数] 文件名称

参数描述
-f显示文件系统的信息
-t以简洁的方式输出

示例:
1)查看文件 anaconda-ks.cfg 的三种时间状态:ATIME,MTIME,CTIME:

miya@asus-x550ld:~$ stat docker-compose.yml
  File: docker-compose.yml
  Size: 481       	Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d	Inode: 7603755     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-03-05 21:50:39.804602221 +0900
Modify: 2020-03-05 21:46:14.811808975 +0900
Change: 2020-03-05 21:46:15.019804012 +0900
 Birth: -

2)以简洁的方式输出信息:

miya@asus-x550ld:~$ stat -t docker-compose.yml
docker-compose.yml 481 8 81a4 0 0 802 7603755 1 0 0 1583412639 1583412374 1583412375 0 4096

3)查看文件系统信息:

miya@asus-x550ld:~$ stat -f docker-compose.yml
  File: "docker-compose.yml"
    ID: 2c5011aaa4761394 Namelen: 255     Type: ext2/ext3
Block size: 4096       Fundamental block size: 4096
Blocks: Total: 119788210  Free: 92826257   Available: 86723909
Inodes: Total: 30498816   Free: 30045940

touch -d "xx:xx" FILE_NAME命令可以修改文件的时间

miya@asus-x550ld:~$ stat test.txt 
  File: test.txt
  Size: 10        	Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d	Inode: 7603811     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2021-02-21 10:14:53.656287637 +0900
Modify: 2021-02-21 10:14:53.656287637 +0900
Change: 2021-02-21 10:14:53.656287637 +0900
 Birth: -
miya@asus-x550ld:~$ 
miya@asus-x550ld:~$ touch -d "10:00" test.txt 
miya@asus-x550ld:~$ 
miya@asus-x550ld:~$ ls -l test.txt 
-rw-r--r-- 1 root root 10  2月 21 10:00 test.txt
miya@asus-x550ld:~$ 
miya@asus-x550ld:~$ stat test.txt 
  File: test.txt
  Size: 10        	Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d	Inode: 7603811     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2021-02-21 10:00:00.000000000 +0900
Modify: 2021-02-21 10:00:00.000000000 +0900
Change: 2021-02-21 10:15:38.085554856 +0900
 Birth: -

2.6.7 cut 命令

用途:

  1. 显示文件内容
  2. 连接多个或多个文件

语法:cut [参数] [文件]
若不指定file参数,该命令将读取标准输入。 必须指定 -b、-c 或 -f 标志之一。

参数描述
-b以字节为单位进行分割 ,仅显示行中指定直接范围的内容
-c以字符为单位进行分割 , 仅显示行中指定范围的字符
-d自定义分隔符,默认为制表符”TAB”
-f显示指定字段的内容 , 与-d一起使用
-n取消分割多字节字符
–complement补足被选择的字节、字符或字段
–out-delimiter指定输出内容是的字段分割符

示例:
1)使用 -f 选项提取指定字段(这里的 f 参数可以简单记忆为 --fields的缩写):

miya@asus-x550ld:~$ cat student.txt 
No Name Mark Percent 
01 tom   69   91 
02 jack  71   87 
03 alex  68   98  
miya@asus-x550ld:~$ cut -f 2 student.txt 
Name
tom 
jack
alex 

2)–complement 选项提取指定字段之外的列(打印除了第二列之外的列):

miya@asus-x550ld:~$ cut -f2 --complement student.txt 
No Mark Percent 
01  69   91 
02  71   87 
03  68   98 

3)使用 -d 选项指定字段分隔符:

miya@asus-x550ld:~$ cat student2.txt 
No;Name;Mark;Percent 
01;tom;69;91 
02;jack;71;87 
03;alex;68;98 
miya@asus-x550ld:~$ cut -f2 -d";" student2.txt 
Name 
tom 
jack 
alex 

4)打印第 1 个到第 3 个字符:

miya@asus-x550ld:~$ cat test.txt 
abcdefghijklmnopqrstuvwxyz 
abcdefghijklmnopqrstuvwxyz 
abcdefghijklmnopqrstuvwxyz 
abcdefghijklmnopqrstuvwxyz 
abcdefghijklmnopqrstuvwxyz 
miya@asus-x550ld:~$ cut -c1-3 test.txt 
abc 
abc 
abc 
abc 
abc 

注意:-b 表示字节;-c 表示字符;-f 表示定义字段。
N- :从第 N 个字节、字符、字段到结尾;
N-M :从第 N 个字节、字符、字段到第 M 个(包括 M 在内)字节、字符、字段;
-M :从第 1 个字节、字符、字段到第 M 个(包括 M 在内)字节、字符、字段。

2.6.8 diff 命令

用途:单个文件或者目录内容。
如果指定比较的是文件,则只有当输入为文本文件时才有效。以逐行的方式,比较文本文件的异同处。
如果指定比较的是目录的的时候,diff 命令会比较两个目录下名字相同的文本文件。列出不同的二进制文件、公共子目录和只在一个目录出现的文件。

格式:diff [参数] 文件/目录

参数描述
-adiff预设只会逐行比较文本文件
-b或--ignore-space-change不检查空格字符的不同
-B或--ignore-blank-lines不检查空白行
-c显示全部内文,并标出不同之处
-i或--ignore-case不检查大小写的不同
-q或--brief仅显示有无差异,不显示详细的信息
-r或--recursive比较子目录中的文件
-s或--report-identical-files若没有发现任何差异,仍然显示信息
-u,-U或--unified=以合并的方式来显示文件内容的不同
-w或--ignore-all-space忽略全部的空格字符
-W或--width在使用-y参数时,指定栏宽
-x不比较选项中所指定的文件或目录
-X将文件或目录类型存成文本文件,然后在=<文件>中指定此文本文件
-y或--side-by-side以并列的方式显示文件的异同之处
--help查看帮助信息
--left-column在使用-y参数时,若两个文件某一行内容相同,则仅在左侧的栏位显示该行内容
--suppress-common-lines在使用-y参数时,仅显示不同之处

示例:
1)比较文件

miya@asus-x550ld:~$ diff log_new.log log_old.log 
3c3
< 2014-03
---
> 2013-03
8c8
< 2013-07
---
> 2013-08
11,12d10
< 2013-11
< 2013-12
miya@asus-x550ld:~$ 
miya@asus-x550ld:~$ diff log_new.log log_old.log -y
2013-01								2013-01
2013-02								2013-02
2014-03							  |	2013-03
2013-04								2013-04
2013-05								2013-05
2013-06								2013-06
2013-07								2013-07
2013-07							  |	2013-08
2013-09								2013-09
2013-10								2013-10
2013-11							  <
2013-12						      <
miya@asus-x550ld:~$ 
miya@asus-x550ld:~$ diff log_new.log log_old.log -y -W 50
2013-01			2013-01
2013-02			2013-02
2014-03		  |	2013-03
2013-04			2013-04
2013-05			2013-05
2013-06			2013-06
2013-07			2013-07
2013-07		  |	2013-08
2013-09			2013-09
2013-10			2013-10
2013-11		  <
2013-12		  <
miya@asus-x550ld:~$ 
miya@asus-x550ld:~$ diff log_old.log log_new.log -y -W 50
2013-01			2013-01
2013-02			2013-02
2013-03		  |	2014-03
2013-04			2013-04
2013-05			2013-05
2013-06			2013-06
2013-07			2013-07
2013-08		  |	2013-07
2013-09			2013-09
2013-10			2013-10
		      >	2013-11
		      >	2013-12
miya@asus-x550ld:~$
miya@asus-x550ld:~$ diff log_old.log log_new.log -y -W 50 --left-column 
2013-01		      (
2013-02		      (
2013-03		      |	2014-03
2013-04		      (
2013-05		      (
2013-06		      (
2013-07		      (
2013-08		      |	2013-07
2013-09		      (
2013-10		      (
		          >	2013-11
		          >	2013-12
miya@asus-x550ld:~$ 
miya@asus-x550ld:~$ diff log_old.log log_new.log -y -W 50 --suppress-common-lines 
2013-03		      |	2014-03
2013-08		      |	2013-07
		          >	2013-11
		          >	2013-12
miya@asus-x550ld:~$

2)比较两个文件不同,并生产补丁

miya@asus-x550ld:~$ diff log2013.log log2014.log -y -W 50
2013-01			2013-01
2013-02			2013-02
2013-03		  |	2014-03
2013-04			2013-04
2013-05			2013-05
2013-06			2013-06
2013-07			2013-07
2013-08		  |	2013-07
2013-09			2013-09
2013-10			2013-10
		      >	2013-11
		      >	2013-12
miya@asus-x550ld:~$ 
miya@asus-x550ld:~$ diff -ruN log_old.log log_new.log > patch.log
miya@asus-x550ld:~$ 
miya@asus-x550ld:~$ cat patch.log 
--- log_old.log	2021-02-21 10:38:10.151837057 +0900
+++ log_new.log	2021-02-21 10:39:58.519019542 +0900
@@ -1,10 +1,12 @@
 2013-01
 2013-02
-2013-03
+2014-03
 2013-04
 2013-05
 2013-06
 2013-07
-2013-08
+2013-07
 2013-09
 2013-10
+2013-11
+2013-12
miya@asus-x550ld:~$ 
miya@asus-x550ld:~$ patch log_old.log patch.log
patching file log_old.log
miya@asus-x550ld:~$ 
miya@asus-x550ld:~$ cat log_old.log 
2013-01
2013-02
2014-03
2013-04
2013-05
2013-06
2013-07
2013-07
2013-09
2013-10
2013-11
2013-12
miya@asus-x550ld:~$ diff -q log_old.log log_new.log 
miya@asus-x550ld:~$ 

2.7 文件目录管理命令

2.7.1 touch 命令

用途:

  1. 创建新的空文件
  2. 改变已有文件的时间戳属性

格式:touch [参数] [文件]

参数描述
-a或--time=atime或--time=access或--time=use改变文件的查看时间记录
-c或--no-create不创建新文件
-d使用指定的日期时间,改变文件的查看时间记录和修改时间记录
-m或--time=mtime或--time=modify改变文件的修改时间记录
-r把指定文档或目录的日期时间,统统设成和参考文档或目录的日期时间
-c不创建新文件
-d设定时间与日期,可以使用各种不同的格式
-t设定文件的时间记录,格式与 date 命令相同

示例:
1)批量创建文件:

miya@asus-x550ld:~$ touch file{1..5}.txt
miya@asus-x550ld:~$ 
miya@asus-x550ld:~$ ls -l | grep file
-rw-r--r-- 1 root root        0  2月 21 11:28 file1.txt
-rw-r--r-- 1 root root        0  2月 21 11:28 file2.txt
-rw-r--r-- 1 root root        0  2月 21 11:28 file3.txt
-rw-r--r-- 1 root root        0  2月 21 11:28 file4.txt
-rw-r--r-- 1 root root        0  2月 21 11:28 file5.txt

2)如果file6.txt不存在,则不创建文件

miya@asus-x550ld:~$ touch -c file6.txt
miya@asus-x550ld:~$ 
miya@asus-x550ld:~$ ls -l | grep file
-rw-r--r-- 1 root root        0  2月 21 11:28 file1.txt
-rw-r--r-- 1 root root        0  2月 21 11:28 file2.txt
-rw-r--r-- 1 root root        0  2月 21 11:28 file3.txt
-rw-r--r-- 1 root root        0  2月 21 11:28 file4.txt
-rw-r--r-- 1 root root        0  2月 21 11:28 file5.txt

3)更新file5.txt的时间戳,和file1.txt的时间戳相同

miya@asus-x550ld:~$ ls -l | grep file
-rw-r--r-- 1 root root       12  2月 21 11:35 file1.txt
-rw-r--r-- 1 root root        0  2月 21 11:28 file2.txt
-rw-r--r-- 1 root root        0  2月 21 11:28 file3.txt
-rw-r--r-- 1 root root        0  2月 21 11:28 file4.txt
-rw-r--r-- 1 root root        0  2月 21 11:28 file5.txt
miya@asus-x550ld:~$ 
miya@asus-x550ld:~$ touch -r file1.txt file5.txt 
miya@asus-x550ld:~$ 
miya@asus-x550ld:~$ ls -l | grep file
-rw-r--r-- 1 root root       12  2月 21 11:35 file1.txt
-rw-r--r-- 1 root root        0  2月 21 11:28 file2.txt
-rw-r--r-- 1 root root        0  2月 21 11:28 file3.txt
-rw-r--r-- 1 root root        0  2月 21 11:28 file4.txt
-rw-r--r-- 1 root root        0  2月 21 11:35 file5.txt

4)设定文件的时间戳

miya@asus-x550ld:~$ ls -l | grep file2
-rw-r--r-- 1 root root        0  2月 21 11:28 file2.txt
miya@asus-x550ld:~$ 
miya@asus-x550ld:~$ touch -t 201211142234.50 file2.txt 
miya@asus-x550ld:~$ 
miya@asus-x550ld:~$ ls -l | grep file2
-rw-r--r-- 1 root root        0 11月 14  2012 file2.txt

-t time 使用指定的时间值 time 作为指定文件相应时间戳记的新值.此处的 time规定为如下形式的十进制数:[[CC]YY]MMDDhhmm[.SS]

5)修改文件的时间属性:

miya@asus-x550ld:~$ ls -l | grep file3
-rw-r--r-- 1 root root        0  2月 21 11:28 file3.txt
miya@asus-x550ld:~$ 
miya@asus-x550ld:~$ touch -d "2011-07-16 12:00" file3.txt 
miya@asus-x550ld:~$ 
miya@asus-x550ld:~$ ls -l | grep file3
-rw-r--r-- 1 root root        0  7月 16  2011 file3.txt
miya@asus-x550ld:~$ 
miya@asus-x550ld:~$ stat file3.txt 
  File: file3.txt
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 802h/2050d	Inode: 7603811     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2011-07-16 12:00:00.000000000 +0900
Modify: 2011-07-16 12:00:00.000000000 +0900
Change: 2021-02-21 14:02:16.928587395 +0900
 Birth: -

2.7.2 mkdir 命令

用途:创建空白的目录

格式:mkdir [选项] 目录

参数描述
-p, --parents递归创建多级目录
-m, --mode=模式建立目录的同时设置目录的权限
-z设置安全上下文
-v, --verbose每次创建新目录都显示信息

示例:
1)递归创建多个目录

miya@asus-x550ld:~$ mkdir -p test1/test11/test111
miya@asus-x550ld:~$ ls -dl test1/
drwxr-xr-x 3 root root 4096  2月 21 14:12 test1/
miya@asus-x550ld:~$ ls -dl test1/test11/
drwxr-xr-x 3 root root 4096  2月 21 14:12 test1/test11/
miya@asus-x550ld:~$ ls -dl test1/test11/test111/
drwxr-xr-x 2 root root 4096  2月 21 14:12 test1/test11/test111/

2)创建权限为740的目录

miya@asus-x550ld:~$ mkdir -m 740 test2
miya@asus-x550ld:~$ ls -dl test2
drwxr----- 2 root root 4096  2月 21 14:15 test2

3)创建新目录都显示信息:

miya@asus-x550ld:~$ mkdir -v test3
mkdir: created directory 'test3'
miya@asus-x550ld:~$ 
miya@asus-x550ld:~$ ls -dl test3/
drwxr-xr-x 2 root root 4096  2月 21 14:20 test3/
miya@asus-x550ld:~$ 
miya@asus-x550ld:~$ mkdir -pv test3/test33/test333
mkdir: created directory 'test3/test33'
mkdir: created directory 'test3/test33/test333'
miya@asus-x550ld:~$ 
miya@asus-x550ld:~$ ls -dl test3/test33/
drwxr-xr-x 3 root root 4096  2月 21 14:20 test3/test33/
miya@asus-x550ld:~$ 
miya@asus-x550ld:~$ ls -dl test3/test33/test333/
drwxr-xr-x 2 root root 4096  2月 21 14:20 test3/test33/test333/

4)同时创建子目录dir1,dir2,dir3:

miya@asus-x550ld:~$ mkdir dir{1..3}
miya@asus-x550ld:~$ ls -l | grep dir
drwxr-xr-x 2 root root     4096  2月 21 14:16 dir1
drwxr-xr-x 2 root root     4096  2月 21 14:16 dir2
drwxr-xr-x 2 root root     4096  2月 21 14:16 dir3

2.7.3 cp 命令

用途:复制文件或目录

格式:cp [选项] 源文件 目标文件

参数描述
-f若目标文件已存在,则会直接覆盖原文件
-i若目标文件已存在,则会询问是否覆盖
-d当复制符号连接时,把目标文件或目录也建立为符号连接,并指向与源文件或目录连接的原始文件或目录
-p保留源文件或目录的所有属性
-r递归复制文件和目录
-l对源文件建立硬连接,而非复制文件
-s对源文件建立符号连接,而非复制文件
-b覆盖已存在的文件目标前将目标文件备份
-v详细显示cp命令执行的操作过程
-a等价于“dpr”选项

示例:
1)复制目录:

miya@asus-x550ld:~$ cp -R dir1 dir2/

2)将文件test1改名为test2:

miya@asus-x550ld:~$ cp -f test1 test2

3)复制多个文件:

miya@asus-x550ld:~$ cp -r file1 file2 file3 dir

4)交互式地将目录 /usr/linuxcool 中的所有.c文件复制到目录 dir 中:

miya@asus-x550ld:~$ cp -r /usr/linuxcool/*.c dir

2.7.4 mv 命令

用途:剪切文件或将文件重命名

格式:mv [选项] 源文件 [目标路径|目标文件名]

参数描述
-i若存在同名文件,则向用户询问是否覆盖
-f覆盖已有文件时,不进行任何提示
-b当文件存在时,覆盖前为其创建一个备份
-u当源文件比目标文件新,或者目标文件不存在时,才执行移动此操作

示例:
1)将文件file_1重命名为file_2:

mv file_1 file_2

2)将文件file移动到目录dir中 :

mv file /dir

3)将目录dir1移动目录dir2中(前提是目录dir2已存在,若不存在则改名):

mv /dir1 /dir2

4)将目录dir1下的文件移动到当前目录下:

mv /dir1/* .

2.7.5 rm 命令

用途:删除文件或目录

格式:rm [参数] [文件]

参数描述
-f忽略不存在的文件,不会出现警告信息
-i删除前会询问用户是否操作
-r/R递归删除
-v显示指令的详细执行过程
if输入文件(input file)的名称
of输出文件(output file)的名称
bs设置每个“块”的大小(字节)
count设置要复制“块”的个数

2.7.6 dd 命令

用途:按照指定大小和个数的数据块来复制文件或转换文件

格式:dd [参数]
Linux系统中有一个名为/dev/zero 的设备文件,每次在课堂上解释它时都充满哲学理论的色彩。因为这个文件不会占用系统存储空间,但却可以提供无穷无尽的数据,因此可以使用它作为 dd 命令的输入文件,来生成一个指定大小的文件。

示例:
1)将本地的/dev/hdb整盘备份到/dev/hdd:

dd if=/dev/hdb of=/dev/hdd

2)将压缩的备份文件恢复到指定盘:

gzip -dc /root/image.gz | dd of=/dev/hdb

3)由标准输入设备读入字符串,并将字符串转换成大写后,再输出到标准输出设备:

dd conv=ucase

4)将testfile文件中的所有英文字母转换为大写,然后转成为testfile_1文件:

dd if=testfile_2 of=testfile_1 conv=ucase

5)修复硬盘:

dd if=/dev/sda of=/dev/sda

6)从/dev/zero 设备文件中取出一个大小为 560MB 的数据块,保存到文件560_file

[root@linuxprobe ~]# dd if=/dev/zero of=560_file count=1 1+0 records in 
1+0 records out 
587202560 btes (587 MB) coied 27.1755 s 21.6 MB/s 

7)使用 dd 命令来压制出光盘镜像文件,将它编程一个可立即使用的 iso 镜像:

[root@linuxprobe ~]# dd if=/dev/cdrom of=RHEL-server-7.0-x86_64-LinuxProbe.Com.iso 
7311360+0 records in 
7311360+0 records out 
3743416320 bytes (3.7 GB) copied, 370.758 s, 10.1 MB/s 

2.7.7 file 命令

用途:查看文件的类型

格式:file [参数] [文件]

参数描述
-b显示文件类型,不显示文件名称
-c详细显示指令执行过程,便于排错或分析程序执行的情形
常与 -m 一起使用,用来在安装幻数文件之前调试它
-f指定名称文件,其内容有一个或多个文件名称时,让file依序辨识这些文件,格式为每列一个文件名称
-i显示MIME类别
-L直接显示符号连接所指向的文件类别
-m指定魔法数字文件
-v显示版本信息
-z尝试去解读压缩文件的内容

示例:

miya@asus-x550ld:~$ ls
 Desktop              Documents   LinuxProbe.pdf   Pictures   snap        Videos
 docker-compose.yml   Downloads   Music            Public     Templates  'VirtualBox VMs'
miya@asus-x550ld:~$ 
miya@asus-x550ld:~$ file docker-compose.yml 
docker-compose.yml: ASCII text
miya@asus-x550ld:~$
miya@asus-x550ld:~$ file LinuxProbe.pdf 
LinuxProbe.pdf: PDF document, version 1.6
miya@asus-x550ld:~$
miya@asus-x550ld:~$ file Desktop/
Desktop/: directory
miya@asus-x550ld:~$ 
miya@asus-x550ld:~$ file /dev/sda
/dev/sda: block special (8/0)

2.8 打包压缩与搜索命令

2.8.1 tar 命令

用途:对文件进行打包压缩或解压

格式:tar [选项] [文件|目录]

参数描述
-c创建压缩文件
-x解开压缩文件
-z用 Gzip 压缩或解压(xxx.tar.gz)
-j用 bzip2 压缩或解压(xxx.tar.bz2)
-v显示压缩或解压的过程
-f目标文件名(必须放到参数的最后一位)
-C指定解压到的目录
-p保留原始的权限与属性
-P使用绝对路径来压缩
-t查看压缩包内有哪些文件

示例:
1)将所有.jpg的文件打包到pic.tar文件:

tar -cf pic.tar *.jpg

2)将linuxcool.log打包后,以 gzip 压缩:

tar -zcvf log.tar.gz linuxcool.log

3)将打包后的压缩包文件指定解压到/root/etc 目录中( /root/etc 已存在):

tar -zxvf etc.tar.gz -C /root/etc

2.8.2 grep 命令

global search regular expression and print out the line

用途:文本搜索匹配

格式:grep [参数]

参数描述
-A<显示行数>除了显示符合样式的那一列之外,并显示该行之后的内容
-B<显示行数>除了显示符合样式的那一行之外,并显示该行之前的内容
-C<显示行数>除了显示符合样式的那一行之外,并显示该行前后的内容
-c只输出匹配行的数量
-E或--extended-regexp将样式为延伸的普通表示法来使用
-f<规则文件>或--file=<规则文件>指定规则文件,其内容含有一个或多个规则样式,让grep查找符合规则条件的文件内容,格式为每行一个规则样式
-F或--fixed-regexp将样式视为固定字符串的列表
-h在显示符合样式的那一行之前,不标示该行所属的文件名称
-H在显示符合样式的那一行之前,表示该行所属的文件名称
-i忽略大小写进行搜索
-l列出文件内容符合指定的样式的文件名称
-L列出文件内容不符合指定的样式的文件名称
-n列出所有的匹配行,显示行号
-q或--quiet或--silent禁止输出任何结果,已退出状态表示搜索是否成功
-r递归搜索
-s不显示不存在、没有匹配文本的错误信息
-v显示不包含匹配文本的所有行
-w匹配整词
-x匹配整行

示例:
1)查找指定进程

ps -ef | grep sshd

2)查找指定进程个数

ps -ef | grep sshd | grep -v grep -c

3)从文件中读取关键词进行搜索

miya@asus-x550ld:~$ cat key_word.txt 
nginx
service
miya@asus-x550ld:~$ cat docker-compose.yml | grep -f key_word.txt 
services:
  nginx:
    image: nginx:v2
      service: nginx
    # insert "labels.service" key into logging
        labels: "service"
    image: nginx:latest
      service: db
    # insert "labels.service" key into logging
        labels: "service"

4)显示包含nginx或者service字符的内容行

miya@asus-x550ld:~$ grep -E "nginx|service" docker-compose.yml 
services:
  nginx:
    image: nginx:v2
      service: nginx
    # insert "labels.service" key into logging
        labels: "service"
    image: nginx:latest
      service: db
    # insert "labels.service" key into logging
        labels: "service"

2.8.3 find 命令

用途:按照指定条件来查找文件

格式:find 查找路径 [参数] [查找条件]

参数描述
-name按名称查找
-perm匹配权限(mode 为完全匹配,-mode 为包含即可)
-user匹配所有者
-group匹配所有组
-mtime -n OR +n匹配修改内容的时间(-n 指 n 天以内,+n 指 n 天以前)
-atime -n OR +n匹配访问文件的时间(-n 指 n 天以内,+n 指 n 天以前)
-ctime -n OR +n匹配修改文件权限的时间(-n 指 n 天以内,+n 指 n 天以前)
-nouser匹配无所有者的文件
-nogroup匹配无所有组的文件
-newer f1 !f2匹配比文件 f1 新但比 f2 旧的文件
–type b/d/c/p/l/f匹配文件类型(后面的字幕参数依次表示块设备、目录、字符设备、管道、链接文件、文本文件)
-size匹配文件的大小(+50KB 为查找超过 50KB 的文件,而-50KB 为查找小于50KB 的文件)
-prune忽略某个目录
-exec …… {} \;后面可跟用于进一步处理搜索结果的命令(下文会有演示)

示例:
1)将当前目录及其子目录下所有文件后缀为 .c 的文件列出来

find . -name "*.c"

2)将目前目录其其下子目录中所有一般文件列出

find . -type f

3)将当前目录及其子目录下所有最近 20 天内更新过的文件列出

find . -mtime -20

4)将当前目录及其子目录下所有最近 20 分钟内更新过的文件列出

find . -mmin -20

5)查找 /home/miya/Desktop 下10分钟内被修改过的普通文件,并在删除之前询问

miya@asus-x550ld:~/Desktop$ find . -type f -mmin -10
./test.txt
miya@asus-x550ld:~/Desktop$ find . -type f -mmin -10 -ok rm {} \;
< rm ... ./test.txt > ? y
miya@asus-x550ld:~/Desktop$ ls ./test.txt
ls: cannot access './test.txt': No such file or directory

6)查找当前目录中文件属主具有读、写权限,并且文件所属组的用户和其他用户具有读权限的文件

find . -type f -perm 644 -exec ls -l {} \;

7)查找系统中所有文件长度为 0 的普通文件,并列出它们的完整路径

find / -type f -size 0 -exec ls -l {} \;

8)在整个文件系统中找出所有归属于 linuxprobe 用户的文件并复制到/root/findresults目录。该实验的重点是“-exec {} ;”参数,其中的{}表示 find 命令搜索出的每一个文件,并且命令的结尾必须是“;”。完成该实验的具体命令如下:

find / -user linuxprobe -exec cp -a {} /root/findresults/ \; 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值