配合 | 和xargs 命令使用 格式: find ... | xargs COMMAND
范例: seq 10 | xargs 1 2 3 4 5 6 7 8 9 10
简单理解为1个一组
seq 10 | xargs -n1 1 2 3 4 5 6 7 8 9 10 #批量创建和删除用户 echo user{1..10} |xargs -n1 useradd echo user{1..100} | xargs -n1 userdel -r
简单理解为两个一组
echo {1..10} |xargs -n2 1 2 3 4 5 6 7 8 9 10
分组删除当前目录下的大量文件
ls | xargs -n 200 rm
find配合xargs分组删除
find -name ".sh" | xargs ls -l find -name ".sh" | xargs -n 50 rm -f
删除小文件
cd /PATH ls ./ |xargs -n1000 rm -f
-perm [/]-]mode:
mode: 精准权限匹配
/mode: 任何一类用户(u,g,o)的权限中的任何一位(r,w,x)符合条件即满足; 9位权限之间存在“或”关系 -mode: 每一类用户(u,g,o)的权限中的每一位(r,w,x)同时符合条件即满足; 9位权限之间存在“与”关系
文件压缩解压
tar 打包和解包: 可以对目录和多个文件打包成一个文件,保留文件属性不丢失,常用于备份功能,推荐使用;对应的文件是 .tar 后缀
格式: tar [option] [打包文件名] [需要打包的文件路径]
注意:tar 本身只支持归档备份文件,压缩功能由-z/-j/-J指定的压缩工具实现。
常用参数:
-z 通过gzip指令处理备份文件。 ***
-v 显示执行过程。 ***
-x 提取归档所有文件
-c 建立新的备份文件 ***
-C 解压到指定目录 ***
-f 指定备份文件 ***
-r 追加文件到归档
-t或--list 列出备份文件的内容。 ***
-d 对比备份文件内和文件系统上的文件的差异。
-h 不建立符号连接,直接复制该连接所指向的原始文件。
--checkpoint 读取备份文件时列出目录名称。
--delete 从备份文件中删除指定的文件。
--exclude=<范本样式> 排除符合范本样式的文件。
--tf查看压缩包中的内容
范例:
创建归档文件来自 foo 和 bar:
tar -cf archive.tar foo bar
提取归档的所有文件:
tar -xf archive.tar
列出所有归档文件内容:
tar -tvf archive.tar
创建归档并 gzip 压缩:
tar -zcvf archive.tar.gz log
提取归档文件并 gzip 解压:
tar -zxvf log.tar.gz
创建归档并 bzip2 压缩:
tar -jcvf log.tar.bz log
提取归档并解压到指定目录:
tar -zxvf log.tar.gz -C /opt
只打包目录内的文件,不所括目录本身
cd /etc tar zcvf /root/etc.tar.gz ./
--exclude 排除文件
tar zcvf /root/a.tgz --exclude=/app/host1 --exclude=/app/host2 /app tar zcvf mybackup.tgz -T /root/includefilelist -X /root/excludefilelist -T 选项指定输入文件 -X 选项指定包含要排除的文件列表
管道
input 输入:终端键盘、鼠标等设备输入 output 输出:命令、脚本、程序的信息输出
I/O重定向:就是将默认的输入、输出,指向一个新的目标
[06:53:44 root@localhost ~]# ll /dev/std* lrwxrwxrwx. 1 root root 15 Jul 9 06:14 /dev/stdin -> /proc/self/fd/0 lrwxrwxrwx. 1 root root 15 Jul 9 06:14 /dev/stdout -> /proc/self/fd/1 lrwxrwxrwx. 1 root root 15 Jul 9 06:14 /dev/stderr -> /proc/self/fd/2
-
标准输入(STDIN) -0 默认接受来自终端窗口的输入
-
标准输出(STDOUT) -1 默认输出到终端窗口
-
标准错误(STDERR) -2 默认错误输出到终端窗口
示例: [07:56:22 root@localhost ~]# who root pts/0 2023-07-09 07:28 (10.0.0.1) root pts/1 2023-07-09 07:28 (10.0.0.1) [07:56:25 root@localhost ~]# [07:56:26 root@localhost ~]# ls test.txt test.txt1 ls: cannot access test.txt1: No such file or directory test.txt [07:58:14 root@localhost ~]# ts/0 ls: cannot access test.txt1: No such file or directory [07:58:17 root@localhost ~]# [07:58:18 root@localhost ~]# [07:58:18 root@localhost ~]# ls test.txt test.txt1 2> /dev/pts/0 test.txt [07:58:23 root@localhost ~]#
和标准错误输出重新定向
STDOUT STDERR
实现标准输出重定向的符号
>
格式:命令 操作符号 文件名
# 清空文件原有内容,再输出到文件中 1> 或 > # 把STDOUT重定向到文件 2> # 把STDERR重定向到文件 &> 或 >& # 把标准输出和错误都重定向 # 追加内容到文件内容 >> # 追加标准输出重定向至文件 2>> # 追加标准错误重定向至文件 &>> # 追加标准输出和错误都重定向
示例:
标准输出和错误输出各自定向至不同位置
COMMAND > /path/file.log 2> /path/file.err
[07:47:42 root@localhost ~]# ls test.txt test.txt1 ls: cannot access test.txt1: No such file or directory test.txt [07:47:34 root@localhost ~]# cat test.log test.txt [07:47:37 root@localhost ~]# cat test.err ls: cannot access test.txt1: No such file or directory [07:47:42 root@localhost ~]#
合并标准输出和错误输出为同一个数据流进行重定向
COMMAND > /path/file.out 2>&1 (顺序很重要)
COMMAND >> /path/file.out 2>&1
[07:50:05 root@localhost ~]# ls test.txt test.txt1 ls: cannot access test.txt1: No such file or directory test.txt [07:50:10 root@localhost ~]# [07:50:10 root@localhost ~]# ls test.txt test.txt1 > ./test.log ls: cannot access test.txt1: No such file or directory [07:50:38 root@localhost ~]# [07:50:42 root@localhost ~]# cat test.log test.txt [07:50:58 root@localhost ~]# [07:51:03 root@localhost ~]# ls test.txt test.txt1 2> ./test.log test.txt [07:51:08 root@localhost ~]# [07:51:10 root@localhost ~]# cat test.log ls: cannot access test.txt1: No such file or directory [07:51:11 root@localhost ~]# [07:51:48 root@localhost ~]# ls test.txt test.txt1 > ./test.log 2>&1 [07:52:07 root@localhost ~]# [07:52:12 root@localhost ~]# cat test.log ls: cannot access test.txt1: No such file or directory test.txt [07:52:15 root@localhost ~]# [07:52:30 root@localhost ~]# ls test.txt test.txt1 &> ./test.log [07:52:37 root@localhost ~]# [07:52:39 root@localhost ~]# cat test.log ls: cannot access test.txt1: No such file or directory test.txt
合并多个命令输出
(CMD1;CMD2......) 合并多个程序的STDOUT
[08:01:47 root@localhost ~]# (ls;hostname) > test.txt [08:01:53 root@localhost ~]# [08:01:54 root@localhost ~]# cat test.txt
常用定界符 EOF:也可以是其他字符
[09:09:52 root@localhost ~]# cat > test.txt << EOF 1 2 3 EOF [0
'|' 表示):省略了标准输出到输入中间环节
管道是从一个命令或程序输出的数据流传递到另一个命令和程序输入的机制,它使用管道符(|)将一个命令的标准输出传递到另一个命令的标准输入。
格式:command1 | command2 | ...
注意:STDERR默认不能通过管道转发,可利用2>&1 或 |& 实现
command1 2>&1 | command2 command1 |& command2
[18:27:47 root@localhost ~]# ls testmt test.txt 2>&1| grep test >t.log [18:28:38 root@localhost ~]# [18:28:40 root@localhost ~]# cat t.log ls: cannot access testmt: No such file or directory test.txt [18:28:42 root@localhost ~]# ls testmt test.txt |& grep test >t.log [18:29:04 root@localhost ~]# [18:29:04 root@localhost ~]# cat t.log ls: cannot access testmt: No such file or directory test.txt
gzip和gunzip
来自于 gzip 包,对应的文件是 .gz 后缀
格式 gzip [OPTION] FILE
注意:在 Linux 中,打包和压缩是分开处理的,gzip 命令只会压缩,不能打包,推荐使用tar -zcvf
常用选项: -d 解压缩包,相当于gunzip -c 结果输出至标准输出,保留原文件不改变 -r 递归压缩指定目录下以及子目录下的所有文件,不会压缩目录,只会递归压缩目录下的文件 -# 指定压缩比,#取值为1-9,值越大压缩比越大 -l, --list list compressed file contents
#解压缩 gunzip file.gz #不显式解压缩的前提下查看文本文件内容 zcat file.gz
范例
压缩文件 (源文件会消失)
gzip messages
压缩保留源文件
gzip -c messages > messages.gz cat messages | gzip > m.gz
解压缩包
gzip -c -d messages.gz > messages # 指定解压后的文件名称
查看压缩包文件信息
zcat messages.gz > messages
zip 和 unzip
zip 可以实现打包目录和多个文件成一个文件并压缩,但可能会丢失文件属性信息,
如:所有者和组信 息,一般建议使用 tar 代替 分别来自于 zip 和 unzip 包
对应的文件是 .zip 后缀 范例: zip帮助