参考文献

  • 鸟哥的Linux私房菜基础学习篇(第四版)

目录和路径

  • 相对路径和绝对路径
    • 绝对路径: 路径的写法一定由根目录/写起,例如/usr/share/doc这个目录;
    • 相对路径: 路径的写法不是由/写起,例如由cd ../home

目录的相关操作

  • .: 代表此层目录;
  • ..: 代表上一层目录;
  • -: 代表前一个工作目录;
  • ~: 代表目前使用者身份所在的家目录;
  • ~holelin: 代表holelin这个使用者的家目录
  • cd: (change directory)切换目录
  • pwd: (print working directory)显示当前目录
  • mkdir: (make directory)建立一个新目录
  • rmdir: 删除一个空目录

文件与目录管理

ls: 文件与目录的查看

1
2
3
ls [-aAdfFhilnrRSt] 文件名或目录名称
ls [--color={never,auto,always}] 文件名或目录名称
ls [--full-time] 文件名或目录名称
  • -a: 全部的文件,连同隐藏文件一起列出来

  • -A: 全部的文件,连同隐藏文件一起列出来,但不包括...这个两个目录

  • -d: 仅列出目录本身,而不是列出目录内的文件数据

  • -f: 直接列出目录结果,而不进行排序(ls 默认会以文件名称进行排序)

  • -F: 根据文件,目录等信息,给予附加数据结构,例如

    • *:代表可执行文件
    • /: 代表目录
    • =: 代表socket文件
    • |:代表FIFO文件
  • -h: 将文件容器以人类较易读的方式(如GB,KB)

  • -l: 详细信息展示,包含文件的属性与权限等数据

  • -i: 列出inode号码

  • -n: 列出UIDGID而非使用者与用户组的名称

  • -r: 将排序结果反向输出

  • -R: 连同子目录内容一起列出来,等于该目录下的所有文件都会显示出来

  • -S: 以文件容量大小排序,而不是文件名排序

  • -t: 依照时间排序,而不是用文件名

  • --color=never: 不要依据文件特性给予颜色显示

  • --color=always: 显示颜色

  • --color=auto: 让系统自行依据设置来判断是否给予颜色

  • --full-time: 以完整时间模式(包含年,月,日,时,分)输出

  • --time={atime,ctime}: 输出access时间或改变权限属性时间(ctime),而非内容修改时间(modification time)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 只列出目录

[root@iZuf6ib0sh7w9cc92x0h4qZ /]# ls -d */
bin/ boot/ dev/ etc/ home/ lib/ lib64/ media/ mnt/ opt/ proc/ ql/ root/ run/ sbin/ srv/ sys/ tmp/ usr/ var/

[root@iZuf6ib0sh7w9cc92x0h4qZ ~]# ls -F |grep "/$"
jenkins-project/
nacos-docker/
node_modules/
nps_files/
test/
tls-gen-main/

[root@iZuf6ib0sh7w9cc92x0h4qZ ~]# ls -l |grep "^d"
drwxr-xr-x 3 root root 20 1月 16 16:30 jenkins-project
drwxr-xr-x 7 root root 125 8月 1 2022 nacos-docker
drwxr-xr-x 163 root root 8192 11月 24 14:39 node_modules
drwxr-xr-x 4 root root 40 3月 10 2022 nps_files
drwxr-xr-x 2 root root 165 2月 9 23:01 test
drwxr-xr-x 8 root root 190 8月 29 16:38 tls-gen-main

cp,rm,mv复制,删除与移动

  • cp: 除了单纯的复制之外,还可以建立链接文件(快捷方式),对比两文件的新旧而予更新,以及复制整个目录等功能

    1
    2
    cp [-adfilprsu] 源文件(source) 目标文件(destination)
    cp [options] source1 source2 source3 .... directory
    • -a: 相当于-dr --preserve=all的意思
    • -d: 若源文件为链接文件的属性,则复制链接文件属性而非文件本身;
    • -f: 为了强制(force),若目标文件已经存在且无法开启,则删除后再尝试一下;
    • -i: 若目标文件(destination)已经存在时,在覆盖时会优先询问操作的进行;
    • -l: 进行硬链接的链接文件建立,而非复制文件本身;
    • -p: 连同文件的属性(权限,用户,时间)一起复制过去,而非使用默认属性(备份时常用)
    • -r: 递归复制,用于目录的复制操作
    • -s: 复制称为符号链接文件(symbol link),即"快捷方式"
    • -u: destination比source旧才更新destination,或destination不存在的情况下才能复制
    • --preserve=all: 除了-p的权限相关参数外,还加入SELinux的属性,links,xattr等也复制
    • **注意:**如果源文件有两个以上,则最后一个目录文件一定是"目录"才行;
    • 在默认的条件中,cp的源文件与目标文件的权限是不同的,目标文件的拥有者通常会是命令操作者本身
  • mv: 移动目录和文件,还可以做重命名的查找

    1
    2
    mv [-fiu] source destination
    mv [options] source1 source2 source3 ... directory
    • -f: 如果目标文件已经存在,不会询问而直接覆盖;
    • -i: 若目标文,已经存在时,就会询问是否覆盖;
    • -u: 若目标文件已经存在,且source比较新,才会更新(update)
  • rm: 删除

    1
    rm [-fir] 文件或目录
    • -f: 就是force,忽略不存在的文件,不会出现告警信息
    • -i: 交互模式,在删除前会询问使用者是否操作
    • -f: 递归删除,最常用于目录的删除

mkdir: 创建目录

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
mkdir /a
# 创建多个目录
mkdir b c d
# 创建多级目录
mkdir -p /e/f/g
# 创建多级目录
mkdir -p redis-637{1..6}/{conf,data}
redis-cluster/
├── redis-6371
│   ├── conf
│   └── data
├── redis-6372
│   ├── conf
│   └── data
├── redis-6373
│   ├── conf
│   └── data
├── redis-6374
│   ├── conf
│   └── data
├── redis-6375
│   ├── conf
│   └── data
└── redis-6376
├── conf
└── data

rmdir: 删除目录

1
rmdir /a

获取路径的文件名称与目录名称

1
2
3
4
# basename /etc/sysconfig/network
network
# dirname /etc/sysconfig/network
/etc/sysconfig

分割文件和数据

split

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 生成一个大小为100KB的测试文件(data.file)
dd if=/dev/zero bs=100k count=1 of=data.file

# 将文件分割成指定大小
[root@iZuf6ib0sh7w9cc92x0h4qZ test]# split -b 10k data.file
[root@iZuf6ib0sh7w9cc92x0h4qZ test]# ll
总用量 220
-rw-r--r-- 1 root root 102400 2月 9 22:09 data.file
-rw-r--r-- 1 root root 10240 2月 9 22:13 xaa
-rw-r--r-- 1 root root 10240 2月 9 22:13 xab
-rw-r--r-- 1 root root 10240 2月 9 22:13 xac
-rw-r--r-- 1 root root 10240 2月 9 22:13 xad
-rw-r--r-- 1 root root 10240 2月 9 22:13 xae
-rw-r--r-- 1 root root 10240 2月 9 22:13 xaf
-rw-r--r-- 1 root root 10240 2月 9 22:13 xag
-rw-r--r-- 1 root root 10240 2月 9 22:13 xah
-rw-r--r-- 1 root root 10240 2月 9 22:13 xai
-rw-r--r-- 1 root root 10240 2月 9 22:13 xaj

# 将文件分割成指定大小并指定以数字为后缀
[root@iZuf6ib0sh7w9cc92x0h4qZ test]# split -b 10k data.file -d -a 4
[root@iZuf6ib0sh7w9cc92x0h4qZ test]# ll
总用量 340
-rw-r--r-- 1 root root 102400 2月 9 22:09 data.file
-rw-r--r-- 1 root root 10240 2月 9 22:14 x0000
-rw-r--r-- 1 root root 10240 2月 9 22:14 x0001
-rw-r--r-- 1 root root 10240 2月 9 22:14 x0002
-rw-r--r-- 1 root root 10240 2月 9 22:14 x0003
-rw-r--r-- 1 root root 10240 2月 9 22:14 x0004
-rw-r--r-- 1 root root 10240 2月 9 22:14 x0005
-rw-r--r-- 1 root root 10240 2月 9 22:14 x0006
-rw-r--r-- 1 root root 10240 2月 9 22:14 x0007
-rw-r--r-- 1 root root 10240 2月 9 22:14 x0008
-rw-r--r-- 1 root root 10240 2月 9 22:14 x0009

# 将文件分割成指定大小并指定以数字为后缀并指定前缀
[root@iZuf6ib0sh7w9cc92x0h4qZ test]# split -b 10k data.file -d -a 10 prefix
[root@iZuf6ib0sh7w9cc92x0h4qZ test]# ll
总用量 580
-rw-r--r-- 1 root root 102400 2月 9 22:09 data.file
-rw-r--r-- 1 root root 10240 2月 9 22:16 prefix0000000000
-rw-r--r-- 1 root root 10240 2月 9 22:16 prefix0000000001
-rw-r--r-- 1 root root 10240 2月 9 22:16 prefix0000000002
-rw-r--r-- 1 root root 10240 2月 9 22:16 prefix0000000003
-rw-r--r-- 1 root root 10240 2月 9 22:16 prefix0000000004
-rw-r--r-- 1 root root 10240 2月 9 22:16 prefix0000000005
-rw-r--r-- 1 root root 10240 2月 9 22:16 prefix0000000006
-rw-r--r-- 1 root root 10240 2月 9 22:16 prefix0000000007
-rw-r--r-- 1 root root 10240 2月 9 22:16 prefix0000000008
-rw-r--r-- 1 root root 10240 2月 9 22:16 prefix0000000009

csplit

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
32
33
34
35
36
37
38
39
40
41
42
43
[root@iZuf6ib0sh7w9cc92x0h4qZ test]# cat server.log
SERVER-1
[C] 192.168..0.1 success
[C] 192.168..0.1 success
[C] 192.168..0.1 success
[C] 192.168..0.1 success
[C] 192.168..0.1 success
[C] 192.168..0.1 failed
[C] 192.168..0.1 success
SERVER-2
[C] 192.168..0.1 success
[C] 192.168..0.1 success
[C] 192.168..0.1 success
[C] 192.168..0.1 success
[C] 192.168..0.1 failed
[C] 192.168..0.1 success
[C] 192.168..0.1 success
SERVER-3
[C] 192.168..0.1 success
[C] 192.168..0.1 failed
[C] 192.168..0.1 success
[C] 192.168..0.1 success
[C] 192.168..0.1 success
[C] 192.168..0.1 success
[C] 192.168..0.1 success

[root@iZuf6ib0sh7w9cc92x0h4qZ test]# csplit server.log /SERVER/ -n 2 -s {*} -f server -b "%02d.log" ; rm server00.log
rm:是否删除普通空文件 'server00.log'?y
[root@iZuf6ib0sh7w9cc92x0h4qZ test]# ll
总用量 216
-rw-r--r-- 1 root root 183 2月 9 22:24 server01.log
-rw-r--r-- 1 root root 183 2月 9 22:24 server02.log
-rw-r--r-- 1 root root 183 2月 9 22:24 server03.log
-rw-r--r-- 1 root root 549 2月 9 22:23 server.log
[root@iZuf6ib0sh7w9cc92x0h4qZ test]# cat server01.log
SERVER-1
[C] 192.168..0.1 success
[C] 192.168..0.1 success
[C] 192.168..0.1 success
[C] 192.168..0.1 success
[C] 192.168..0.1 success
[C] 192.168..0.1 failed
[C] 192.168..0.1 success
  • /SERVER/用来匹配某一行,分割过程即从此处开始
    • /[REGEX]/标识文本样式.包括从当前行(第一行)直到(但不包括)包含"SERVER"的匹配行
  • -n 指定分割后的文件名后缀的数字个数,例如01,02,03等.
  • -s 使命令进入静默模式,不打印其他信息.
  • {*} 表示根据匹配重复执行分割,直到文件末尾为止,可以用{整数}的形式来指定分割执行的次数
  • -f指定分割后的文件名前缀
  • -b指定后缀格式,如"%02d.log"
  • 因为分割后的第一个文件没有任何内容(匹配的单词就位于文件的第一行中),使用删除server00.log文件

根据扩展名切分文件名

1
2
3
4
5
6
7
8
[root@iZuf6ib0sh7w9cc92x0h4qZ test]# cat test1.sh
# !/bin/bash

file_jpg="simple.jpg";
name=${file_jpg%.*}
ext=${file_jpg#*.}
echo File name is: $name
echo File ext is: $ext
  • ${VAR%.*}: 从VAR中删除位于%右侧的通配符所匹配的字符串,%通配符从右向左进行匹配
    • %属于非贪婪操作,它从右向左找出匹配通配符的最短结果
    • %%属于贪婪操作,它从右向左找出匹配通配符的最长结果
  • ${VAR#*.}: 从VAR中删除位于#左侧的通配符所匹配的字符串,#通配符从左向右进行匹配
    • #属于非贪婪操作,它从左向右找出匹配通配符的最短结果
    • ##属于贪婪操作,它从左向右找出匹配通配符的最长结果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@iZuf6ib0sh7w9cc92x0h4qZ test]# cat test2.sh
# !/bin/bash
URL=www.baidu.com
# 移除.*所匹配的最右边的内容
echo ${URL%.*}
# 将从右边开始一致到匹配到最左边的*.移除
echo ${URL%%.*}
# 移除*.所匹配的最左边的内容
echo ${URL#*.}
# 将从左边开始一致到匹配到最右边的*.移除
echo ${URL##*.}
[root@iZuf6ib0sh7w9cc92x0h4qZ test]# ./test2.sh
www.baidu
www
baidu.com
com

文件处理

grep 数据过滤

1
2
3
4
5
6
7
8
9
10
11
12
13
# 打印匹配某个结果之后的3行
~ seq 10 |grep 5 -A 3
5
6
7
8

# 打印匹配某个结果之前的3行
~ seq 10 |grep 5 -B 3
2
3
4
5

shred

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
32
33
34
35
36
37
38
shred --help
Usage: shred [OPTION]... FILE...
Overwrite the specified FILE(s) repeatedly, in order to make it harder
for even very expensive hardware probing to recover the data.

If FILE is -, shred standard output.

Mandatory arguments to long options are mandatory for short options too.
-f, --force change permissions to allow writing if necessary
-n, --iterations=N overwrite N times instead of the default (3)
--random-source=FILE get random bytes from FILE
-s, --size=N shred this many bytes (suffixes like K, M, G accepted)
-u deallocate and remove file after overwriting
--remove[=HOW] like -u but give control on HOW to delete; See below
-v, --verbose show progress
-x, --exact do not round file sizes up to the next full block;
this is the default for non-regular files
-z, --zero add a final overwrite with zeros to hide shredding
--help display this help and exit
--version output version information and exit

Delete FILE(s) if --remove (-u) is specified. The default is not to remove
the files because it is common to operate on device files like /dev/hda,
and those files usually should not be removed.
The optional HOW parameter indicates how to remove a directory entry:
'unlink' => use a standard unlink call.
'wipe' => also first obfuscate bytes in the name.
'wipesync' => also sync each obfuscated byte to the device.
The default mode is 'wipesync', but note it can be expensive.

CAUTION: shred assumes the file system and hardware overwrite data in place.
Although this is common, many platforms operate otherwise. Also, backups
and mirrors may contain unremovable copies that will let a shredded file
be recovered later. See the GNU coreutils manual for details.

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation <https://www.gnu.org/software/coreutils/shred>
or available locally via: info '(coreutils) shred invocation'