[toc]

2-Linux的命令

一、命令与选项

1、SHELL

通常来讲,计算机硬件是由运算器、控制器、存储器、输入/输出设备等共同组成的,而让各种硬件设备各司其职且又能协同运行的东西就是系统内核。Linux系统的内核负责完成对硬件资源的分配、调度等管理任务。

系统内核对计算机的正常运行非常重要,因此一般不建议直接去编辑内核中的参数,而是让用户通过基于系统调用接口开发出的程序或服务来管理计算机。

Shell就是这样的一个命令行工具。Shell(也称为终端或壳)充当的是人与内核(硬件)之间的翻译官,用户把一些命令“告诉”终端,它就会调用相应的程序服务去完成某些工作。

包括红帽系统在内的许多主流Linux系统默认使用的终端是Bash(Bourne-Again SHell)解释器。

2、什么是Linux的命令?

指在Linux终端(命令行)中输入从事某一特殊操作的代码就称之为命令(指令),是对Linux系统进行管理的命令(指令)。对于Linux系统来说,无论是中央处理器、内存、磁盘驱动器、键盘、鼠标,还是用户等都是文件,Linux系统管理的命令(指令)是它正常运行的核心。

image

3、选项

是指可供使用者挑拣的分类条目。linux中指的是命令的参数。

4、命令(指令)的标准格式

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

注意::命令名称、命令参数、命令对象之间请用空格键分隔,命令区分大小写。

  • 命令对象:一般是指要处理的文件、目录、用户等资源

  • 命令参数:即选项,可以用长格式(完整的选项名称),也可以用短格式(单个字母的缩写),两者分别用--与-作为前缀。

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

一个命令中可以包含多个命令参数(选项),要操作的命令对象也可以是多个。

例如:张三去买加两个鸡蛋一根火腿肠的鸡蛋饼,在这个命令中,买鸡蛋饼是命令的名称,鸡蛋饼是命令的对象,两个鸡蛋一根火腿肠是命令的参数。

Linux新手不会执行命令大多是因为参数比较复杂,参数值需要随不同的命令和需求情况而发生改变。因此,要想灵活搭配各种参数,执行自己想要的功能,则需要长时间的经验积累了。

5、man命令

作用:查看命令帮助信息

man命令是Linux下最核心的命令之一。而man命令也并不是英文单词man的意思,它是单词manual的缩写,即使用手册的意思。

格式:man 命令

综合应用:查看netstat帮助
man netstat

image

【linux命令在线中文手册】

【Linux命令大全】

二、基础命令

1、ls命令

用途:ls命令用于显示目录中的文件信息
格式:ls [选项] [文件]

示例1:ls

含义:列出当前工作目录 下的所有文件/文件夹的内容

[root@localhost ~]# ls
anaconda-ks.cfg  initial-setup-ks.cfg  公共  模板
视频  图片  文档  下载  音乐  桌面

注意:ls列出的结果颜色说明,其中蓝色的名称表示文件夹,黑色的表示文件,绿色表示拥有所有权限。|

示例2:ls 路径

含义:列出指定路径下的所有文件/文件夹的名称

路径可分为两种:相对路径、绝对路径

  • 相对路径:相对与参照物的路径,一般相对于当前的工作路径,linux中写法有两种:
    • .:代表当前的目录,也可以使用./来表示;
    • ..:代表上一层目录,也可以../来代表。
  • 绝对路径:直接从根“/”目录开始寻找对应的路径

查找用户家目录下的所有内容

[root@localhost tiger]# ls ../../root  #相对路径
anaconda-ks.cfg  initial-setup-ks.cfg  公共  模板
视频  图片  文档  下载  音乐  桌面
[root@localhost tiger]# ls /root #绝对路径
anaconda-ks.cfg  initial-setup-ks.cfg  公共  模板
视频  图片  文档  下载  音乐  桌面
示例3:ls 参数 路径

含义:列出指定路径下的所有文件/文件夹的名称,并指定格式进行显示

常用的参数:

  • -a参数:看到全部文件(包括隐藏文件
  • -l参数:可以查看文件的属性、大小等详细信息。
  • -h 参数:可读与-l一起,以易于阅读的格式输出文件大小

ls -l

[root@localhost ~]# ls -l
总用量 8
-rw-------. 1 root root 1751 2月  17 03:40 anaconda-ks.cfg
-rw-r--r--. 1 root root 1779 2月  17 03:47 initial-setup-ks.cfg
drwxr-xr-x. 2 root root    6 2月  16 19:51 公共
drwxr-xr-x. 2 root root    6 2月  16 19:51 模板
drwxr-xr-x. 2 root root    6 2月  16 19:51 视频
drwxr-xr-x. 2 root root    6 2月  16 19:51 图片
drwxr-xr-x. 2 root root    6 2月  16 19:51 文档
drwxr-xr-x. 2 root root    6 2月  16 19:51 下载
drwxr-xr-x. 2 root root    6 2月  16 19:51 音乐
drwxr-xr-x. 2 root root    6 2月  16 19:51 桌面

上述列表中的第一列字符表示文档的类型,“-”表示对应的文档类型为文件,“d”表示对应的文档类型为文件夹

ls -la

[root@localhost ~]# ls -la
总用量 52
dr-xr-x---. 14 root root 4096 2月  18 16:21 .
dr-xr-xr-x. 17 root root  224 2月  17 03:39 ..
-rw-------.  1 root root 1751 2月  17 03:40 anaconda-ks.cfg
-rw-------.  1 root root 1115 2月  17 00:19 .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 2月  16 20:03 .cache
drwxr-xr-x. 14 root root  261 2月  16 20:01 .config
----省略部分输出----

如果一个目录或文件名以一个点.开始,表示这个目录或文件是一个隐藏目录或文件(如:.bashrc)。即以默认方式查找时,不显示该目录或文件。

ls -lh

[root@localhost ~]# ls -lh
总用量 8.0K
-rw-------. 1 root root 1.8K 2月  17 03:40 anaconda-ks.cfg
-rw-r--r--. 1 root root 1.8K 2月  17 03:47 initial-setup-ks.cfg
drwxr-xr-x. 2 root root    6 2月  16 19:51 公共
drwxr-xr-x. 2 root root    6 2月  16 19:51 模板
drwxr-xr-x. 2 root root    6 2月  16 19:51 视频
drwxr-xr-x. 2 root root    6 2月  16 19:51 图片
drwxr-xr-x. 2 root root    6 2月  16 19:51 文档
drwxr-xr-x. 2 root root    6 2月  16 19:51 下载
drwxr-xr-x. 2 root root    6 2月  16 19:51 音乐
drwxr-xr-x. 2 root root    6 2月  16 19:51 桌面

文件的大小将以易读的方式显示:如1.8k

2、pwd命令

用途:pwd命令用于显示目录中的文件信息
格式:pwd [参数]

示例1:pwd

显示当前目录

[root@localhost mail]# pwd
/var/mail
示例2:pwd 参数

参数:

  • -P参数:显示当前目录的实际物理地址
  • -L参数:显示,逻辑地址

显示逻辑地址

[root@localhost mail]# pwd -L
/var/mail

显示物理地址

目录是软连接,显示实际物理地址。

[root@localhost mail]# pwd -P
/var/spool/mail

3、cd命令

用途:cd命令用于切换工作路径。

格式:cd [目录路径]

cd命令应该是最常用的一个Linux命令。可以通过cd命令迅速、灵活地切换到不同的工作目录。

示例1:cd 路径的方式切换进/etc目录中
[root@localhost mail]# cd /etc
[root@localhost etc]# pwd
/etc
示例2:当前在/usr/local/下,使用相对路径切换目录到root家目录。
[root@localhost local]# cd  ../../root
[root@localhost ~]# pwd
/root

除了常见的切换目录方式,还可以:

  • 使用“cd-”命令返回到上一次所处的目录
  • 使用“cd..”命令进入上级目录,
  • 使用“cd ~”命令切换到当前用户的家目录,
  • 使用“cd ~username”切换到其他用户的家目录。
示例3:从家目录返回到/usr/local/
[root@localhost ~]# cd -
/usr/local
[root@localhost local]# pwd
/usr/local
示例4:切换到用户家目录
[root@localhost local]# cd ~
[root@localhost ~]# pwd
/root

4、history命令

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

格式:history [-c]

执行history命令能显示出当前用户在本地计算机中执行过的最近1000条命令记录。如果觉得1000不够用,还可以自定义/etc/profile文件中的HISTSIZE变量值。在使用history命令时,如果使用-c参数则会清空所有的命令历史记录。还可以使用!编码数字的方式来重复执行某一次的命令。

并且可以通过管道符|grep命令联合使用,查询指定的命令

示例:查看指定命令记录(如ls),并使用!编号执行一条命令

① 查询命令

image

[root@localhost ~]# history |grep ls
  ………………省略部分输出………………
  110  ls
  115  cls
  144  ls /var/log

② 执行编号为144的命令

[root@localhost ~]# !144
ls /var/log
anaconda  cups   lastlog   ppp    speech-dispatcher  vmware-network.1.log'
………………省略部分输出………………

5、stat命令

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

格式:stat 文件名称

示例:查看/etc/passwd文件信息
[root@localhost ~]# stat /etc/passwd
  文件:"/etc/passwd"
  大小:2307      	块:8          IO 块:4096   普通文件
设备:fd00h/64768d	Inode:17719304    硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:system_u:object_r:passwd_file_t:s0
最近访问:2021-02-19 08:43:54.016510998 +0800
最近更改:2021-02-17 03:39:26.181003977 +0800
最近改动:2021-02-17 03:39:26.181003977 +0800
创建时间:-

6、grep命令

用途:用于在文本中执行关键词搜索,并显示匹配的结果,通常通过管道符与其它命令一起使用。

示例1:使用grep查询/etc/passwd中root用户信息

格式:grep 要匹配的内容 目录路径

[root@localhost ~]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

可以使用 -c参数只查询匹配的行数

[root@localhost ~]# grep -c  root /etc/passwd
2
示例2:与ps命令配合查询firewalld进程
[root@localhost ~]# ps -ef | grep firewalld
root      70169      1 22 11:08 ?        00:00:00 /usr/bin/python2 -Es 
/usr/sbin/firewalld --nofork --nopid
root      70253  12829  0 11:08 pts/1    00:00:00 grep --color=auto firewalld

7、whereis命令

用途:用于查看程序的安装目录

有时使用yum安装程序后,不知道程序默认安装在那里,即可使用此命令查看。非常实用。而且用法简单

格式:whereis 程序名称

示例:查看firewalld所在目录信息
[root@localhost ~]# whereis firewalld
firewalld: /usr/sbin/firewalld /usr/lib/firewalld 
/etc/firewalld /usr/share/firewalld 
/usr/share/man/man1/firewalld.1.gz

8、mkdir命令

用途:mkdir命令用于创建空白的目录
格式:mkdir [选项] 目录

示例1:mkdir 目录

在当前目录创建test目录

[root@localhost ~]# mkdir test
[root@localhost ~]# ls
anaconda-ks.cfg  initial-setup-ks.cfg  test  公共  模板  视频  图片  文档  下载  音乐  桌面

在当前目录下一次创建多个目录

每个目录使用空隔隔开

[root@localhost test]# mkdir a b c
[root@localhost test]# ls
a  b  c

opt目录创建test目录

[root@localhost ~]# mkdir /opt/test
[root@localhost ~]# mkdir ../opt/test1
[root@localhost ~]# ls /opt
rh  test  test1
示例2:mkdir 参数 目录

选项参数:

  • -m参数:设置权限模式(类似chmod)
  • -p参数:需要时创建目标目录的上层目录,但即使这些目录已存在也不存在错误处理
  • -v参数:每次创建新目录都显示信息

mkdir -m 目录

[root@localhost ~]# mkdir -m 777 d.txt
[root@localhost ~]# ll
总用量 8
drwxrwxrwx. 2 root root    6 2月  18 16:40 d.txt
………………省略部分输出………………

mkdir -p 目录

[root@localhost ~]# mkdir a/b/c/test
mkdir: 无法创建目录"a/b/c/test": 没有那个文件或目录
[root@localhost ~]# mkdir -p a/b/c/test
[root@localhost ~]# cd a/b/c/test
[root@localhost test]# pwd
/root/a/b/c/test

创建多级目录时,如果目录不存在,则需使用-p参数

mkdir -v 目录

[root@localhost test]# mkdir -v a
mkdir: 已创建目录 "a"

9、touch命令

用途:touch命令一是创建新的空文件,二是改变已有文件的时间戳属性。

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

示例1:touch 文件

当前目录为用户家目录'~'

在当前目录创建linux.txt文件

[root@localhost ~]# touch linux.txt
[root@localhost ~]# ls
anaconda-ks.cfg  initial-setup-ks.cfg  linux.txt  test  公共  模板  视频  图片  文档  下载  音乐  桌面

在当前目录下一次创建多个文件

每个文件使用空隔隔开

[root@localhost ~]# touch a.txt b.txt
[root@localhost ~]# ls
anaconda-ks.cfg  a.txt  b.txt  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面

test目录创建linux.txt文件

[root@localhost ~]# touch test/linux.txt
[root@localhost ~]# ls test/
linux.txt
示例2:touch 参数 文件

常用参数:

  • -a参数:仅修改“读取时间”(atime)
  • -m参数:仅修改“修改时间”(mtime)
  • -d参数:同时修改atime与mtime
  • -c参数:不创建任何文件
  • -r参数:使用参考档的时间记录
  • -t参数:使用[[CC]YY]MMDDhhmm[.ss]格式的时间替代当前时间

使用-a参数修改读取时间

① 查看c.txt文件信息

[root@localhost test]# stat c.txt
  文件:"c.txt"
  大小:0         	块:0          IO 块:4096   普通空文件
设备:fd00h/64768d	Inode:3953309     硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2021-02-18 16:47:52.726348201 +0800
最近更改:2021-02-18 16:47:52.726348201 +0800
最近改动:2021-02-18 16:47:52.726348201 +0800

② 修改访问时间

[root@localhost test]# touch -a c.txt
[root@localhost test]# stat c.txt
  文件:"c.txt"
  大小:0         	块:0          IO 块:4096   普通空文件
设备:fd00h/64768d	Inode:3953309     硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2021-02-18 16:49:04.015102952 +0800
最近更改:2021-02-18 16:47:52.726348201 +0800
最近改动:2021-02-18 16:49:04.015102952 +0800

使用-m修改“修改时间”

[root@localhost test]# touch -m c.txt
[root@localhost test]# stat c.txt
  文件:"c.txt"
  大小:0         	块:0          IO 块:4096   普通空文件
设备:fd00h/64768d	Inode:3953309     硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2021-02-18 16:49:04.015102952 +0800
最近更改:2021-02-18 16:49:55.997924116 +0800
最近改动:2021-02-18 16:49:55.997924116 +0800

使用-d同时修改atime与mtime

① 在c.txt文件中写入内容,查看最后修改时间

[root@localhost test]# echo "hello linux" > c.txt 
[root@localhost test]# stat c.txt
  文件:"c.txt"
  大小:12        	块:8          IO 块:4096   普通文件
设备:fd00h/64768d	Inode:3953309     硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2021-02-18 16:49:04.015102952 +0800
最近更改:2021-02-18 16:52:38.614364678 +0800
最近改动:2021-02-18 16:52:38.614364678 +0800

② 将修改与访问时间更改为之前的时间2021-01-27 15:14:48"

[root@localhost test]# touch -d "2021-02-18 16:49:04" c.txt
[root@localhost test]# stat c.txt
  文件:"c.txt"
  大小:12        	块:8          IO 块:4096   普通文件
设备:fd00h/64768d	Inode:3953309     硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2021-02-18 16:49:04.000000000 +0800
最近更改:2021-02-18 16:49:04.000000000 +0800
最近改动:2021-02-18 16:54:29.716982459 +0800

注意:日期要用双隐号括起来

使用-rb.txt的文档时间记录赋给c.txt

① 查看b.txt文件信息

[root@localhost test]# stat b.txt 
  文件:"b.txt"
  大小:0         	块:0          IO 块:4096   普通空文件
设备:fd00h/64768d	Inode:3953308     硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2021-02-18 16:47:52.726348201 +0800
最近更改:2021-02-18 16:47:52.726348201 +0800
最近改动:2021-02-18 16:47:52.726348201 +0800

② 查看c.txt文件信息

[root@localhost test]# stat c.txt
  文件:"c.txt"
  大小:12        	块:8          IO 块:4096   普通文件
设备:fd00h/64768d	Inode:3953309     硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2021-02-18 16:49:04.000000000 +0800
最近更改:2021-02-18 16:49:04.000000000 +0800
最近改动:2021-02-18 16:54:29.716982459 +0800

③ 将b.txt文件的时间记录赋给c.txt

[root@localhost test]# touch -r b.txt c.txt
[root@localhost test]# stat c.txt
  文件:"c.txt"
  大小:12        	块:8          IO 块:4096   普通文件
设备:fd00h/64768d	Inode:3953309     硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2021-02-18 16:47:52.726348201 +0800  #访问时间与最近改动时间已修改成功
最近更改:2021-02-18 16:47:52.726348201 +0800
最近改动:2021-02-18 16:58:48.714228845 +0800

使用`-c -t' 修改时间

[root@localhost test]# touch -c -t 202011211811 c.txt
[root@localhost test]# stat c.txt
  文件:"c.txt"
  大小:12        	块:8          IO 块:4096   普通文件
设备:fd00h/64768d	Inode:3953309     硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2020-11-21 18:11:00.000000000 +0800
最近更改:2020-11-21 18:11:00.000000000 +0800
最近改动:2021-02-18 17:03:04.551544582 +0800

注意:-c参数为不创建新文建,使用-t参数时需注意与-c参数配合使用,否则会已日期形式创建新文件(比如:202101011111为名称的文件),同时被修改的文件时间也会达不到预期。

10、cp命令

用途:cp命令用于复制文件或目录

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

在Linux系统中,复制操作具体分为3种情况:

  • 如果目标文件是目录,则会把源文件复制到该目录中;
  • 如果目标文件也是普通文件,则会询问是否要覆盖它;
  • 如果目标文件不存在,则执行正常的复制操作。
示例1:cp 源文件 目标文件

当前目录为用户家目录'~'

~/test目录下的b.txt文件复制到~/b文件夹下

① 不指定文件名

[root@localhost test]# cp b.txt ../b
[root@localhost test]# ls ../b
b.txt

② 指定文件名

[root@localhost test]# cp b.txt ../b/cp.txt
[root@localhost test]# ls ../b
b.txt  cp.txt
示例2:cp 参数 源文件 目标文件

常用选项参数:

  • -p参数:保留原始文件的属性

  • -r参数:递归持续复制(用于目录)

  • -d参数:若对象为“链接文件”,则保留该“链接文件”的属性

  • -a参数:相当于-pdr(p、d、r为上述参数)

  • -l对源文件建立硬连接,而非复制文件

  • -s对源文件建立符号连接,而非复制文件

  • -b覆盖已存在的文件目标前将目标文件备份

使用-a

保留原有文件属性,将~/a/b/c/c.txt以及内部文件复制到~/test目录。

[root@localhost test]# touch ../a/b/c/c.txt
[root@localhost test]# cp -a ../a ./
[root@localhost test]# stat a/b/c/c.txt 
  文件:"a/b/c/c.txt"
  大小:0         	块:0          IO 块:4096   普通空文件
设备:fd00h/64768d	Inode:17720283    硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2021-02-18 17:24:11.855528821 +0800
最近更改:2021-02-18 17:24:11.855528821 +0800
最近改动:2021-02-18 17:26:11.608275887 +0800

注意:使用-a参数时,相当于-pdr,可以递归复制,并保留原有属性,同时若对象为“链接文件”,则保留该“链接文件”的属性

使用-l建立硬连接

① 查看b.txt信息

[root@localhost test]# stat b.txt 
  文件:"b.txt"
  大小:0         	块:0          IO 块:4096   普通空文件
设备:fd00h/64768d	Inode:3953308     硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2021-02-18 17:12:43.398996392 +0800
最近更改:2021-02-18 16:47:52.726348201 +0800
最近改动:2021-02-18 16:47:52.726348201 +0800

② 使用-l参数复制

[root@localhost test]# cp -l b.txt b_1.txt
[root@localhost test]# stat b.txt 
  文件:"b.txt"
  大小:0         	块:0          IO 块:4096   普通空文件
设备:fd00h/64768d	Inode:3953308     硬链接:2 
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2021-02-18 17:12:43.398996392 +0800
最近更改:2021-02-18 16:47:52.726348201 +0800
最近改动:2021-02-18 17:30:27.132736185 +0800

使用-s建立软连接

[root@localhost test]# cp -s b.txt ./b_2.txt
[root@localhost test]# ls -l
总用量 4
lrwxrwxrwx. 1 root root  5 2月  18 17:32 b_2.txt -> b.txt
………………省略部分输出………………

注意:
软连接也称符号链接,使用cp命令创建时只能于当前目录中创建相对的符号链接。可使用ln命令创建不目文件夹下的软连。

使用-b覆盖已存在的文件目标前将目标文件备份

[root@localhost test]# cp -b b.txt c.txt
cp:是否覆盖"c.txt"? y
[root@localhost test]# ls
a  a.txt  b_1.txt  b_2.txt  b.txt  c.txt  c.txt~

11、mv命令

用途:mv命令是“move”单词的缩写,其功能大致和英文含义一样,可以移动文件或对其改名。
格式:mv [选项] 源文件 [目标路径|目标文件名]

与复制的区别:

mv与cp的结果不同。mv命令好像文件“搬家”,文件名称发生改变,但个数并未增加。而cp命令是对文件进行复制操作,文件个数是有增加的。

示例1:mv 源文件 目标路径|目标文件名

移动

[root@localhost test]# mv ~/anaconda-ks.cfg ./
[root@localhost test]# ls
a  anaconda-ks.cfg  a.txt  b_1.txt  b_2.txt  b.txt  c.txt  c.txt~

改名

[root@localhost test]# mv ~/initial-setup-ks.cfg ./mv.cfg
[root@localhost test]# ls
a  anaconda-ks.cfg  a.txt  b_1.txt  b_2.txt  b.txt  c.txt  c.txt~  mv.cfg
示例2:mv 参数 源文件 目标路径|目标文件名

常用参数:

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

使用-i

[root@localhost test]# mv -i ~/linux.txt ./
mv:是否覆盖"./linux.txt"? n
[root@localhost test]# mv -i ~/linux.txt ./
mv:是否覆盖"./linux.txt"? y

mv命令默认提示即使用-i参数,所以此参数可不用显式使用。

使用-f

[root@localhost test]# mv -f ~/linux.txt ./

注意:直接移动,不作提示,此参数慎用。

使用-b

移动时如果文件存在则创建附件,然后覆盖

[root@localhost test]# mv -b ../linux.txt ./
mv:是否覆盖"./linux.txt"? y
[root@localhost test]# ls
a  linux.txt  linux.txt~

使用-u

① 查看源文件与目标文件信息

[root@localhost test]# stat linux.txt 
  文件:"linux.txt"
  大小:6         	块:8          IO 块:4096   普通文件
设备:fd00h/64768d	Inode:3953309     硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2021-02-18 19:50:08.646505114 +0800
最近更改:2021-02-18 19:48:31.264627283 +0800
最近改动:2021-02-18 19:48:31.264627283 +0800
创建时间:-
[root@localhost test]# stat ../linux.txt 
  文件:"../linux.txt"
  大小:6         	块:8          IO 块:4096   普通文件
设备:fd00h/64768d	Inode:33574984    硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2021-02-18 19:48:31.264627283 +0800
最近更改:2021-02-18 19:48:31.264627283 +0800
最近改动:2021-02-18 19:50:08.646505114 +0800

源文件与目标文件一样。

② 使用-u参数移动

[root@localhost test]# mv -u linux.txt ../
[root@localhost test]# ls
a  linux.txt

因为源文件与目标文件一样,所以没有移动,查看目录文件还在。

③ 修改源文件内容

[root@localhost test]# echo "hello" >>linux.txt 
[root@localhost test]# cat linux.txt 
hello
hello

④ 使用-u参数再次移动

[root@localhost test]# mv -u linux.txt ../
mv:是否覆盖"../linux.txt"? y
[root@localhost test]# ls
a

此时因源文件比目标文件内容新,所以移动成功,查看目录文件已被移走。

12、rm命令

用途:rm命令用于删除文件或目录

格式:rm [选项] 文件

rm命令命令的功能为删除一个目录中的一个或多个文件或目录,它也可以将某个目录及其下的所有文件及子目录均删除。对于链接文件,只是删除了链接,原有文件均保持不变。

示例1:rm 文件
[root@localhost test]# ll
总用量 0
drwxr-xr-x. 3 root root 28 2月  18 19:58 a
lrwxrwxrwx. 1 root root  5 2月  18 19:58 al -> a.txt
-rw-r--r--. 1 root root  0 2月  18 19:56 a.txt
-rw-r--r--. 1 root root  0 2月  18 19:55 b.txt
-rw-r--r--. 1 root root  0 2月  18 19:55 c.txt
[root@localhost test]# rm al
rm:是否删除符号链接 "al"?y
[root@localhost test]# ls
a  a.txt  b.txt  c.txt

a文件为b.txt的软连接,删除后,b.txt保持不变。

示例2:rm 参数 文件

常用参数:

  • -i参数:删除前会询问用户是否操作
  • -f参数:强制删除,忽略不存在的文件,不会出现警告信息
  • -r/R参数:递归删除文件夹下的所有文件以及子目录文件。

使用-i

[root@localhost test]# rm -i b.txt 
rm:是否删除普通空文件 "b.txt"?n

rm命令删除前会询问用户是否操作,所以此参数可不用显示使用。

使用-f

[root@localhost test]# rm -f b.txt 
[root@localhost test]# ls
a  a.txt  c.txt

注意:直接删除,不作提示,此参数慎用。

使用-r/R

[root@localhost test]# rm test1/
rm: 无法删除"test1/": 是一个目录
[root@localhost test]# rm -R test1/
rm:是否删除目录 "test1/"?y

删除多级目录时,必须使用-r/R参数,不分大小写。

注意:rm是一个危险的命令,使用的时候要特别当心,尤其对于新手,否则整个系统就会毁在这个命令(比如在/(根目录)下执行rm * -rf)。

13、vim命令

用途:vim 是一款文本编辑器,可以通过vim命令打开编辑器来查看文件内容或编辑文件

格式:vim 文件

示例1:vim 文件

① 打开文件

[root@localhost test]# vim a.txt 

② 退出文件

退出打开的文件时,按下shift+英文冒号,在出现的命令行模式中输入q(在没有修改文件内容的前提下),按下回车即可退出。

③ 如果需要打开的文件为只读模式可加入-R参数

[root@localhost test]# vim -R a.txt 

14、cat命令

用途:cat命令用于查看内容较少的纯文本文件

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

示例1:cat 文件
[root@localhost test]# cat a.txt 
hello cat
示例2:cat 参数 文件

常用参数:

  • -n参数:显示行数(空行也编号)
[root@localhost test]# cat -n a.txt 
     1	hello cat

注意:当文件内容较大时,文本内容会在屏幕上快速闪动(滚屏),用户往往看不清所显示的具体内容。因此对于较长文件内容可以按Ctrl+S键,停止滚屏;以及Ctrl+Q键可以恢复滚屏;而按Ctrl+C(中断)键则可以终止该命令的执行。或者对于大文件,干脆用more命令!

示例2:cat 文件 文件... > 新文件

cat可以合并多个文件到一个新文件中

[root@localhost test]# cat a.txt 
hello cat
[root@localhost test]# cat c.txt 
hello c.txt
[root@localhost test]# cat a.txt c.txt >b.txt
[root@localhost test]# cat b.txt 
hello cat
hello c.txt

三、进阶指令(重点)

1、df命令

用途:df命令显示磁盘空间使用情况

格式:df [参数] [指定文件]

df命令的英文全称即“Disk Free”,顾名思义功能是用于显示系统上可使用的磁盘空间。默认显示单位为KB,建议使用“df -h”的参数组合,根据磁盘容量自动变换合适的单位,更利于阅读。

日常普遍用该命令可以查看磁盘被占用了多少空间、还剩多少空间等信息。

示例1:df 不使用参数
[root@localhost test]# df
文件系统                   1K-块    已用     可用 已用% 挂载点
devtmpfs                  914440       0   914440    0% /dev
tmpfs                     931516       0   931516    0% /dev/shm
tmpfs                     931516   10700   920816    2% /run
tmpfs                     931516       0   931516    0% 
………………省略部分输出………………
示例3:df 参数 使用参数

常用的参数:

  • -a参数:显示所有系统文件
  • -h参数:以容易阅读的方式显示
  • -T参数:输出每个文件系统的类型
  • -t参数:只显示指定类型(TYPE)的文件系统

使用 -a

[root@localhost test]# df -a
文件系统                   1K-块    已用     可用 已用% 挂载点
sysfs                          0       0        0     - /sys
proc                           0       0        0     - /proc
devtmpfs                  914440       0   914440    0% /dev
securityfs                     0       0        0     - /sys/kernel/security
tmpfs                     931516       0   931516    0% /dev/shm
devpts                         0       0        0     - /dev/pts
tmpfs                     931516   10700   920816    2% /run
………………省略部分输出………………

使用-h

[root@localhost test]# df -ah
文件系统                 容量  已用  可用 已用% 挂载点
sysfs                       0     0     0     - /sys
proc                        0     0     0     - /proc
devtmpfs                 894M     0  894M    0% /dev
securityfs                  0     0     0     - /sys/kernel/security
tmpfs                    910M     0  910M    0% /dev/shm
devpts                      0     0     0     - /dev/pts
tmpfs                    910M   11M  900M    2% /run
tmpfs                    910M     0  910M    0% /sys/fs/cgroup
………………省略部分输出………………

使用-T 查看系统文件类型,-t查看指定系统文件类型

① 查看系统文件类

[root@localhost test]# df -T
文件系统                类型        1K-块    已用     可用 已用% 挂载点
devtmpfs                devtmpfs   914440       0   914440    0% /dev
tmpfs                   tmpfs      931516       0   931516    0% /dev/shm
tmpfs                   tmpfs      931516   10700   920816    2% /run
tmpfs                   tmpfs      931516       0   931516    0% /sys/fs/cgroup
/dev/mapper/centos-root xfs      17811456 4803428 13008028   27% /
/dev/sda1               xfs       1038336  189248   849088   19% /boot
………………省略部分输出………………

② 查看指定系统文件类型

[root@localhost test]# df -t xfs
文件系统                   1K-块    已用     可用 已用% 挂载点
/dev/mapper/centos-root 17811456 4803428 13008028   27% /
/dev/sda1                1038336  189248   849088   19% /boot
示例3:df 参数 指定文件
[root@localhost test]# df -h /run
文件系统        容量  已用  可用 已用% 挂载点
tmpfs           910M   11M  900M    2% /run
[root@localhost test]# df -h /
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   17G  4.6G   13G   27% /

2、free命令

用途:free命令用于显示当前系统中内存的使用量信息

格式:free [-h]

示例:查看当前内存使用情况
[root@localhost test]# free -h
              total        used        free      shared  buff/cache   available
Mem:           1.8G        886M         85M         41M        847M        708M
Swap:          2.0G        264K        2.0G
totalusedfreesharedbuffersbuff/cacheavailable
内存总量已用量空闲量进程共享的内存量磁盘缓存的内存量缓存的内存量可用量

Mem:内存 ,可用的内存为72M
Swap:用于临时内存,即虚拟内存,与windows的虚拟内存功能一样,当内存不够用时,临时使用硬盘上的空间做内存。

3、head命令

用途:head命令将每个指定文件的头10行(即默认显示10)显示到标准输出。

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

示例1:head 文件
[root@localhost test]# head /etc/tcsd.conf 

#
# This is the configuration file for the trousers tcsd. (The Trusted Computing
# Software Stack Core Services Daemon).
#
# Defaults are listed below, commented out
#
# Send questions to: trousers-users@lists.sourceforge.net
#

不加参数情况下默认显示前10行内容。同时默认不显示文件名的头信息。

示例2:head 参数 文件

常用的参数:

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

使用-n

[root@localhost test]# head -n 3 /etc/tcsd.conf 

#
# This is the configuration file for the trousers tcsd. (The Trusted Computing

使用-c

从头开始显示多少个字符数。

[root@localhost test]# head -c 3 b.txt 
hel
[root@localhost test]# 

使用-v

[root@localhost test]# head -v -n 2 b.txt 
==> b.txt <==
hello cat
hello c.txt

显示文件名头信息。

4、tail命令

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

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

示例1:tail 文件

没有参数情况下默认显示最后10行。

[root@localhost ~]# tail /etc/tcsd.conf 
#
# Option: disable_ipv6
# Values: 0 or 1
# Description: This options determines if the TCSD will bind itself to the
# machine's local IPv6 addresses in order to receive requisitions through
# its TCP port. Value of 1 disables IPv6 support, so clients cannot reach
# TCSD using that protocol.
#
#  disable_ipv6 = 0
#

如果给定的文件不止一个,则在显示的每个文件前面加一个文件名标题。

[root@localhost test]# tail a.txt b.txt 
==> a.txt <==
hello cat

==> b.txt <==
hello cat
hello c.txt

如果没有指定文件或者文件名为“-”,则读取标准输入。

[root@localhost ~]# tail 
a
d
q
示例2:tail 参数 文件

常用的参数:

  • -n参数:输出最后N行,而非默认的最后10行
  • -f参数:当文件增长时,输出后续添加的数据

tail -n

tail命令的操作方法与head命令非常相似,只需要执行tail -n 20 文件名命令就可以达到这样的效果。

[root@localhost test]# tail -n 1 b.txt 
hello c.txt

tail -f

tail命令最强悍的功能是可以持续刷新一个文件的内容,当想要实时查看最新日志文件时,这特别有用。

① 使用命令查看/var/log/messages文件

[root@localhost test]# tail -f /var/log/messages

image

② 打开一个终端,切换用户,可查看文件的动态变化

image

当进行用户切换时,内容发生动态变化了。

5、less命令

用途:less命令与more命令类似,但可以通过翻页键查看上下页的内容。

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

示例1:less 文件
[root@localhost test]# less /var/log/messages 

按键功能如下:

  • b 向后翻一页
  • d 向后翻半页
  • h 显示帮助界面
  • Q 退出less 命令
  • u 向前滚动半页
  • y 向前滚动一行
  • 空格键 滚动一页
  • 回车键 滚动一行
示例2:less 参数 文件

常用参数:

  • -e参数:当文件显示结束后,自动离开
  • -N参数:显示每行的行号
  • -o参数:将查看的内容输出到指定文件

ps查看进程信息并通过less分页显示,同时显示行号,在文件显示结束后,自动结束。

[root@localhost test]# ps -ef |less -N
[root@localhost test]# ps -ef |less -Ne

可将查看的内容输入到文件

[root@localhost test]# ps -ef |less -Ne -o a.txt 
[root@localhost test]# cat a.txt 
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 14:09 ?        00:00:11 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
root          2      0  0 14:09 ?        00:00:00 [kthreadd]
root          4      2  0 14:09 ?        00:00:00 [kworker/0:0H]
root          6      2  0 14:09 ?        00:00:00 [ksoftirqd/0]
root          7      2  0 14:09 ?        00:00:00 [migration/0]
root          8      2  0 14:09 ?        00:00:00 [rcu_bh]
………………省略部分输出………………

6、wc命令

用途:wc命令统计指定文件中的字节数、字数、行数,并将统计结果显示输出。

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

示例1:wc 文件
[root@localhost test]# wc a.txt 
  264  2370 20685 a.txt
示例2:wc 参数 文件

常用参数:

  • -w参数:统计字数(一个字被定义为由空白、跳格或换行字符分隔的字符串)
  • -c参数:统计字节数
  • -l参数:统计行数,或--lines:只显示列数
  • -m参数:统计字符数
  • -L参数:打印最长行的长度
[root@localhost test]# wc -w a.txt 
2370 a.txt  #字数
[root@localhost test]# wc -c a.txt 
20685 a.txt # 字节数
[root@localhost test]# wc -l a.txt 
264 a.txt # 行数
[root@localhost test]# wc -L a.txt 
777 a.txt 最长行长度
[root@localhost test]# wc -m a.txt 
20685 a.txt #字符数

7、date命令

用途:date命令用于显示及设置系统的时间或日期

格式:date [选项] [+指定的格式]

示例1:date [+]指定的格式]

只接执行date将显示系统当前标准时间。

CST 时区缩写。

[root@localhost test]# date
2021年 02月 18日 星期四 20:42:53 CST

按指定格式输出

[root@localhost test]# date "+%Y-%m-%d %H:%M:%S"
2021-02-18 20:44:11

常用的格式说明

参数作用
%S秒(00~59)
%M分钟(00~59)
%H小时(00~23)
%I小时(00~12)
%Y完整年份(例如:2020)
%m月份(1~12)
%d(月份中的)几号(用两位表示) (01..31)

设置系统时间

若是不以加号作为开头,则表示要设定时间,而时间格式MMDDhhmm[[CC]YY][.ss],其中MM为月份,DD为日,hh为小时,mm为分钟,CC为年份前两位数字,YY为年份后两位数字,ss为秒数。

[root@localhost test]# date 020101012021
2021年 02月 01日 星期一 01:01:00 CST

还原系统时间与网络同步执行命令/usr/sbin/ntpdate stdtime.gov.hk即可,需安装ntpdate

示例2:date -u [指定格式]

常用参数:

  • -u参数:显示或设置全球时间(格林威治时间)
  • -d参数: 显示由string指定的时间,而不是当前时间

使用-u

[root@localhost test]# date -u
2021年 02月 18日 星期四 12:47:19 UTC

使用-d

  • 显示前一天

    [root@localhost test]# date -d "-1 day" +%F
    2021-02-17
    
  • 显示下一天

    [root@localhost test]# date -d "+1 day" +%F
    2021-02-19
    
  • 显示上一月

    [root@localhost test]# date -d "-1 month" +%F
    2021-01-18
    
  • 显示下一月

    [root@localhost test]# date -d "+1 month" +%F
    2021-03-18
    
  • 显示上一年

    [root@localhost test]# date -d "-1 year" +%F
    2020-02-18
    
  • 显示下一年

    [root@localhost test]# date -d "+1 year" +%F
    2022-02-18
    

8、cal命令

用途:rm命令用于查看日历等时间信息,如只有一个参数,则表示年份(1-9999),如有两个参数,则表示月份和年份

格式:cal [选项] [月份] [年份]

示例1:cal [月份] [年份]

执行cal

不加参数与目标对象,将输出当前月份日历信息

[root@localhost test]# cal
      二月 2021     
日 一 二 三 四 五 六
    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

执行cal 月份 年份

[root@localhost test]# cal 03 21
       三月 21      
日 一 二 三 四 五 六
                   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
示例2:cal 参数 [月份] [年份]

常用参数:

  • -1参数:查看一个月内的日历
  • -3参数:查看三个月内的日历
  • -j参数:查看罗马日历
  • -h参数:关闭高亮显示
  • -m参数:已星期一开头,显示日历
  • -s参数:已星期日开头,显示日历
  • -y参数:查看12个月的日历
[root@localhost test]# cal -s
      二月 2021     
日 一 二 三 四 五 六
    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

[root@localhost test]# cal -m
      二月 2021     
一 二 三 四 五 六 日
 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

其它参数可自行测试。

9、clear命令

用途:clear指令用来清除终端屏幕,在终端中通过快捷键Ctrl+L清除屏幕

格式:clear

[root@localhost test]# clear

注意:clear命令只能清除可见屏中的内容,但缓存中的内容还在,可上下键查看,如果想要完全清除终端屏幕,可使用reset命令。


作业

1、系统内核的作用?

2、什么是SHELL?

3、主流linux系统默认使用的Shell解析器是什么?

4、什么是linux命令,什么选项?

5、命令(指令)的标准格式?

6、Linux中使用什么命令来查看命令的帮助信息?

7、用于查看文件的具体存储信息和时间等信息的命令是什么?

刚学java菜鸡,永劫无间蚀月,王者荣耀王者,金铲铲小铂金,第五人格菜鸡,原神开服玩家,星穹铁道菜鸡,崩坏的菜鸡,闪耀暖暖,和平精英,LOL,CSGO,以及三A大作收集者等等。。。