xargs和文件打包和压缩
一.自己分析
1.xargs功能:它默认本身是读取键盘输入,读取输入之后把他显示
xargs a b c <-键盘输入 a b c
1)从某种角度来讲,他和cat很像,但是xargs功能并不在cat这个效果上,他可以接收键盘的标准输出,然后把输入的结果传给xargs后续命令,来作为后续命令的参数。实际上刚才xargs后省略了一个echo,全部应该是 xargs echo ,实际上,现在键盘输入的东西都输入给了echo,而且他默认会把这个多个换行换掉,换成空格隔开。实际上,xargs的功能是这样的:读取键盘上的标准输入,然后把生成的这个标准输入的字符串传给echo,作为echo的参数,这是他的基本功能。
2)当然这个命令可以不用echo,可以换成别的,比如:xargs ls -l
[root@centos7 ~]# xargs ls -l /etc/issue /etc/passwd -rw-r--r--. 1 root root 133 Jan 4 21:15 /etc/issue -rw-r--r--. 1 root root 2284 Jan 11 18:16 /etc/passwd
键盘输入文件名,结束ctrl + d
把这些输入的内容,利用xargs传给了ls -l 作为其参数,也就是说xargs可以生成参数,那么只要支持参数的命令,参数是可以动态生成的,而不是写死的,从而增强了我们处理命令的功能。
3)综上,xargs的功能可以用一句话概括:用xargs可以把一些命令的参数动态生成.
2.
[root@centos7 ~]# ls f*| xargs rm
显示所有f开头的文件,然后用xargs传给rm执行
3.当然你也可以指定几个一行
[root@centos7 ~]# echo {1..3} | xargs -n1 1 2 3 [root@centos7 ~]# echo {1..4} | xargs -n2 1 2 3 4
4.批量创建用户用xargs更方便(面试)
[root@centos7 ~]# echo user{1..10} | xargs -n1 useradd [root@centos7 ~]# getent passwd user1:x:1001:1001::/home/user1:/bin/bash user2:x:1002:1002::/home/user2:/bin/bash user3:x:1003:1003::/home/user3:/bin/bash user4:x:1004:1004::/home/user4:/bin/bash user5:x:1005:1005::/home/user5:/bin/bash user6:x:1006:1006::/home/user6:/bin/bash user7:x:1007:1007::/home/user7:/bin/bash user8:x:1008:1008::/home/user8:/bin/bash user9:x:1009:1009::/home/user9:/bin/bash user10:x:1010:1010::/home/user10:/bin/bash #注意:不能直接传给useradd做参数,因为useradd一次只能处理一个用户名,所以加-n1 #删除: [root@centos7 ~]# echo user{1..10} | xargs -n1 userdel -r
5.
[root@centos7 ~]# find /bin/ -perm /7000 | xargs ls -Sl -rwsr-xr-x. 1 root root 2447304 Apr 1 2020 /bin/Xorg -rwxr-sr-x. 1 root screen 475240 Oct 1 2020 /bin/screen ---x--s--x. 1 root nobody 382216 Aug 9 2019 /bin/ssh-agent ---s--x---. 1 root stapusr 212080 Oct 14 2020 /bin/staprun ---s--x--x. 1 root root 147336 Oct 1 2020 /bin/sudo -rwsr-xr-x. 1 root root 78408 Aug 9 2019 /bin/gpasswd -rwsr-xr-x. 1 root root 73888 Aug 9 2019 /bin/chage -rwsr-xr-x. 1 root root 57656 Aug 9 2019 /bin/crontab -rwsr-xr-x. 1 root root 53048 Oct 31 2018 /bin/at -rwsr-xr-x. 1 root root 44264 Oct 1 2020 /bin/mount -rwsr-xr-x. 1 root root 41936 Aug 9 2019 /bin/newgrp -rwx--s--x. 1 root slocate 40520 Apr 11 2018 /bin/locate -rwsr-xr-x. 1 root root 32128 Oct 1 2020 /bin/su -rwsr-xr-x. 1 root root 32096 Oct 31 2018 /bin/fusermount -rwsr-xr-x. 1 root root 31984 Oct 1 2020 /bin/umount -rwsr-xr-x. 1 root root 27856 Apr 1 2020 /bin/passwd -rws--x--x. 1 root root 23968 Oct 1 2020 /bin/chfn -rws--x--x. 1 root root 23880 Oct 1 2020 /bin/chsh -rwsr-xr-x. 1 root root 23576 Apr 1 2020 /bin/pkexec -rwxr-sr-x. 1 root tty 19544 Oct 1 2020 /bin/write -r-xr-sr-x. 1 root tty 15344 Jun 10 2014 /bin/wall
这个7表示的是所有特殊权限,所有这里的意思是只要有suid、sgid或stoik权限都行
6.
[root@centos7 ~]# touch 'a b' [root@centos7 ~]# ls 'a b' a b [root@centos7 ~]# find -type f | xargs rm rm: cannot remove ‘./a’: No such file or directory rm: cannot remove ‘b’: No such file or directory
发现报错了,因为他并不认为‘a b’是一个文件,而是认为a是一个文件,b是一个文件,所以就报错了。为什么他会出现这个原因?就是因为,默认情况下,他会认为'a b'中的这个空格是切割文件的切割符,但是现在恰恰不行。因为'a b'是一个文件,那为了告知系统,'a b'中的空格不做切割文件的分隔符,那我就要指定另一个切割文件的分隔符,用ASCII中的0.
[root@centos7 ~]# find -type f -print0 | xargs -0 rm
7.下载哔哩哔哩视频
yum install python3-pip -y pip3 install you-get seq 60 | xargs -i -P3 you get https://www.bilibili.com/video/BV14K411W7UF/?spm_id_from=333.788.recommend_more_video.{}
8.gzip.bzip2、xz都只能压单个文件,所以并不常用,而zip可以压文件夹,常用
9.xz、gzip也好,本身这些工具默认下不去处理文件夹,只能打包一个文件,那怎么办哪,没关系,我们用tar,用tar先把他打包成一个tar包,可以把文件夹打成一个tar包,然后再把它压缩成gz不就行了。
10.1)将etc打包到etc.tar文件中
[root@centos7 data]# tar cf etc.tar /etc/ tar: Removing leading `/' from member names [root@centos7 data]# ls etc.tar etc.tar
注意:这里只是打包不压缩
2)如果你想压缩:
[root@centos7 data]# tar zcf etc.tar.gz /etc/ tar: Removing leading `/' from member names [root@centos7 data]# tar jcf etc.tar.bz2 /etc/ tar: Removing leading `/' from member names [root@centos7 data]# tar Jcf etc.tar.xz /etc/ tar: Removing leading `/' from member names [root@centos7 data]# ls etc.tar etc.tar.bz2 etc.tar.gz etc.tar.xz
压缩比越高越慢
11.tar预览
tar tf etc.tar
预览里面包括什么文件
tar tvf etc.tar
看到每个文件的大小属性
12.解开tar
1)解的话,默认解到当前路径下:
tar xvf etc.tar
2)我们也可以解到别的路径下:
[root@centos7 data]# tar xvf etc.tar -C /opt [root@centos7 opt]# ls -d etc etc
解到opt下
13.所以tar命令看起来复杂,实际上只用掌握三个:cf tf xf.
14.1) gzip支持管道,可以把一个命令执行结果传到gzip中生成一个文件
[root@centos7 data]# echo message | gzip > m.gz [root@centos7 data]# ls m.gz m.gz
2)tar也支持管道
15.split切割
1)
split -b 100k -d etc.tar
这个地方加-d表示切出的文件自动加数字
2)当然如果不加-d,切出的则是aa、ab、ac...
二.相关资料
1.参数替换 xargs
由于很多命令不支持管道|来传递参数,xargs用于产生某个命令的参数,xargs 可以读入 stdin 的数 据,并且以空格符或回车符将 stdin 的数据分隔成为参数
另外,许多命令不能接受过多参数,命令执行可能会失败,xargs 可以解决
注意:文件名或者是其他意义的名词内含有空格符的情况
find 经常和 xargs 命令进行组合,形式如下:
find | xargs COMMAND
2.compress 和 uncompress
此工具来自于ncompress包,此工具目前已经很少使用
对应的文件是 .Z 后缀
格式
compress Options [file ...] uncompress file.Z #解压缩
常用选项
-d 解压缩,相当于uncompress -c 结果输出至标准输出,不删除原文件 -v 显示详情
zcat file.Z 不显式解压缩的前提下查看文本文件内容
范例:
zcat file.Z >file
3.gzip和gunzip
来自于 gzip 包
对应的文件是 .gz 后缀
格式:
gzip [OPTION]... FILE ...
常用选项:
-k keep, 保留原文件,CentOS 8 新特性 -d 解压缩,相当于gunzip -c 结果输出至标准输出,保留原文件不改变 -# 指定压缩比,#取值为1-9,值越大压缩比越大
4.bzip2和bunzip2
来自于 bzip2 包
对应的文件是 .bz2 后缀
格式:
bzip2 [OPTION]... FILE ...
常用选项
-k keep, 保留原文件 -d 解压缩 -c 结果输出至标准输出,保留原文件不改变 -# 1-9,压缩比,默认为9
5.xz 和 unxz
来自于 xz 包
对应的文件是 .xz 后缀
格式
xz [OPTION]... FILE ...
常用选项
-k keep, 保留原文件 -d 解压缩 -c 结果输出至标准输出,保留原文件不改变 -# 压缩比,取值1-9,默认为6
6.zip 和 unzip
zip 可以实现打包目录和多个文件成一个文件并压缩,但可能会丢失文件属性信息,如:所有者和组信 息,一般建议使用 tar 代替
分别来自于 zip 和 unzip 包
对应的文件是 .zip 后缀
范例: zip帮助
[root@centos8 ~]#zip Copyright (c) 1990-2008 Info-ZIP - Type 'zip "-L"' for software license. Zip 3.0 (July 5th 2008). Usage: zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list] The default action is to add or replace zipfile entries from list, which can include the special name - to compress standard input. If zipfile and list are omitted, zip compresses stdin to stdout. -f freshen: only changed files -u update: only changed or new files -d delete entries in zipfile -m move into zipfile (delete OS files) -r recurse into directories -j junk (don't record) directory names -0 store only -l convert LF to CR LF (-ll CR LF to LF) -1 compress faster -9 compress better -q quiet operation -v verbose operation/print version info -c add one-line comments -z add zipfile comment -@ read names from stdin -o make zipfile as old as latest entry -x exclude the following names -i include only the following names -F fix zipfile (-FF try harder) -D do not add directory entries -A adjust self-extracting exe -J junk zipfile prefix (unzipsfx) -T test zipfile integrity -X eXclude eXtra file attributes -y store symbolic links as the link instead of the referenced file -e encrypt -n don't compress these suffixes -h2 show more help [root@centos8 ~]#zip -h2 Extended Help for Zip See the Zip Manual for more detailed help Zip stores files in zip archives. The default action is to add or replace zipfile entries. Basic command line: zip options archive_name file file ... Some examples: Add file.txt to z.zip (create z if needed): zip z file.txt Zip all files in current dir: zip z * Zip files in current dir and subdirs also: zip -r z .
7.tar
tar 即 Tape ARchive 磁带归档,可以对目录和多个文件打包一个文件,并且可以压缩,保留文件属性不 丢失,常用于备份功能,推荐使用
对应的文件是 .tar 后缀
格式
tar [-ABcdgGhiklmMoOpPrRsStuUvwWxzZ][-b <区块数目>][-C <目的目录>][-f <备份文件>][-F