栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

linux命令日常总结【更新中】

linux命令日常总结【更新中】

1、tar指令(拆包后会覆盖同名文件,需注意。)

(1)tar -xf xxx.tar.gz或tar -xf xxx.tar.bz2,即便成功命名打包,但由于没有实际压缩,只是个名字而且,并未起到压缩作用,当解压时如果添加了z或j参数是无法解压文件的。但如果用tar -zxf xxx.tar.gz或tar jxf xxx.tar.bz2,打包且压缩文件,后续直接用tar xf可以直接解压gz或bz2文件,具体原因我也不清楚为什么。

证明未压缩可见文件很大。

[root@localhost miao]# ll
总用量 4
drwxrwxrwx 10 lin  lin  167 2月  19 11:19 elasticsearch-8.0.0
drwxrwxrwx  7 lin  lin 4096 11月  6 2020 elasticsearch-head-master
drwxr-xr-x  2 root   0    6 2月  24 11:40 nnn
drwxr-xr-x  2 root   0   59 2月  24 11:40 test
[root@localhost miao]# tar cvf 111.tar.gz nnn test
nnn/
test/
test/t1.txt
test/1.txt
test/2.txt
test/3.txt
[root@localhost miao]# tar cvf 222.tar.bz2 nnn test
nnn/
test/
test/t1.txt
test/1.txt
test/2.txt
test/3.txt
[root@localhost miao]# ll
总用量 28
-rw-r--r--  1 root   0 10240 2月  24 11:41 111.tar.gz
-rw-r--r--  1 root   0 10240 2月  24 11:41 222.tar.bz2
drwxrwxrwx 10 lin  lin   167 2月  19 11:19 elasticsearch-8.0.0
drwxrwxrwx  7 lin  lin  4096 11月  6 2020 elasticsearch-head-master
drwxr-xr-x  2 root   0     6 2月  24 11:40 nnn
drwxr-xr-x  2 root   0    59 2月  24 11:40 test
[root@localhost miao]# tar -zxvf 111.tar.gz 

gzip: stdin: not in gzip format
tar: Child returned status 1
tar: Error is not recoverable: exiting now
[root@localhost miao]# tar -jxvf 222.tar.bz2 
bzip2: (stdin) is not a bzip2 file.
tar: Child returned status 2
tar: Error is not recoverable: exiting now
[root@localhost miao]# tar -zcvf gz111.tar.gz nnn test
nnn/
test/
test/t1.txt
test/1.txt
test/2.txt
test/3.txt
[root@localhost miao]# tar -jcvf bz2222.tar.bz2 nnn test
nnn/
test/
test/t1.txt
test/1.txt
test/2.txt
test/3.txt
[root@localhost miao]# ll
总用量 36
-rw-r--r--  1 root   0 10240 2月  24 11:41 111.tar.gz
-rw-r--r--  1 root   0 10240 2月  24 11:41 222.tar.bz2
-rw-r--r--  1 root   0   186 2月  24 11:44 bz2222.tar.bz2
drwxrwxrwx 10 lin  lin   167 2月  19 11:19 elasticsearch-8.0.0
drwxrwxrwx  7 lin  lin  4096 11月  6 2020 elasticsearch-head-master
-rw-r--r--  1 root   0   183 2月  24 11:44 gz111.tar.gz
drwxr-xr-x  2 root   0     6 2月  24 11:40 nnn
drwxr-xr-x  2 root   0    59 2月  24 11:40 test
[root@localhost miao]# tar -xf bz2222.tar.bz2 -C /opt
[root@localhost miao]# ll
总用量 36
-rw-r--r--  1 root   0 10240 2月  24 11:41 111.tar.gz
-rw-r--r--  1 root   0 10240 2月  24 11:41 222.tar.bz2
-rw-r--r--  1 root   0   186 2月  24 11:44 bz2222.tar.bz2
drwxrwxrwx 10 lin  lin   167 2月  19 11:19 elasticsearch-8.0.0
drwxrwxrwx  7 lin  lin  4096 11月  6 2020 elasticsearch-head-master
-rw-r--r--  1 root   0   183 2月  24 11:44 gz111.tar.gz
drwxr-xr-x  2 root   0     6 2月  24 11:40 nnn
drwxr-xr-x  2 root   0    59 2月  24 11:40 test
[root@localhost miao]# cd /opt
[root@localhost opt]# ll
总用量 0
drwxr-xr-x 2 root 0  6 2月  24 11:22 a
drwxr-xr-x 2 root 0  6 2月  24 11:22 aa
drwxr-xr-x 2 root 0  6 2月  24 11:22 aaa
drwxr-xr-x 2 root 0  6 2月  24 11:22 aaaa
drwxr-xr-x 2 root 0  6 2月  24 11:22 ab
drwxr-xr-x 2 root 0  6 2月  24 11:22 bababa
drwxr-xr-x 2 root 0  6 2月  24 11:40 nnn
drwxr-xr-x 2 root 0 59 2月  24 11:40 test

(2)从tar包里解压单个文件并放到指定路径。

只想解压压缩包中的nnn文件与test文件中的t1.txt,并将其放到指定路径该怎样处理?

tar -jxvf xxx.tar.bz2 -C /opt nnn  test/t1.txt

tar [参数] 包文件 -C 【目标路径】  需要的文件

[root@localhost opt]# ll
总用量 0
drwxr-xr-x 2 root 0  6 2月  24 11:22 a
drwxr-xr-x 2 root 0  6 2月  24 11:22 aa
drwxr-xr-x 2 root 0  6 2月  24 11:22 aaa
drwxr-xr-x 2 root 0  6 2月  24 11:22 aaaa
drwxr-xr-x 2 root 0  6 2月  24 11:22 ab
drwxr-xr-x 2 root 0  6 2月  24 11:22 bababa
[root@localhost opt]# cd /miao
[root@localhost miao]# tar -jxvf bz2222.tar.bz2 -C /opt nnn/
nnn/
[root@localhost miao]# ll
总用量 12
-rw-r--r--  1 root   0  186 2月  24 11:44 bz2222.tar.bz2
drwxrwxrwx 10 lin  lin  167 2月  19 11:19 elasticsearch-8.0.0
drwxrwxrwx  7 lin  lin 4096 11月  6 2020 elasticsearch-head-master
-rw-r--r--  1 root   0  183 2月  24 11:44 gz111.tar.gz
[root@localhost miao]# cd /opt
[root@localhost opt]# ll
总用量 0
drwxr-xr-x 2 root 0  6 2月  24 11:22 a
drwxr-xr-x 2 root 0  6 2月  24 11:22 aa
drwxr-xr-x 2 root 0  6 2月  24 11:22 aaa
drwxr-xr-x 2 root 0  6 2月  24 11:22 aaaa
drwxr-xr-x 2 root 0  6 2月  24 11:22 ab
drwxr-xr-x 2 root 0  6 2月  24 11:22 bababa
drwxr-xr-x 2 root 0  6 2月  24 11:40 nnn
[root@localhost opt]# rm -rf nnn
[root@localhost opt]# ll
总用量 0
drwxr-xr-x 2 root 0 6 2月  24 11:22 a
drwxr-xr-x 2 root 0 6 2月  24 11:22 aa
drwxr-xr-x 2 root 0 6 2月  24 11:22 aaa
drwxr-xr-x 2 root 0 6 2月  24 11:22 aaaa
drwxr-xr-x 2 root 0 6 2月  24 11:22 ab
drwxr-xr-x 2 root 0 6 2月  24 11:22 bababa
[root@localhost opt]# cd /miao
[root@localhost miao]# tar -jxvf bz2222.tar.bz2 -C /opt nnn test/t1.txt
nnn/
test/t1.txt
[root@localhost miao]# cd /opt
[root@localhost opt]# ll
总用量 0
drwxr-xr-x 2 root 0  6 2月  24 11:22 a
drwxr-xr-x 2 root 0  6 2月  24 11:22 aa
drwxr-xr-x 2 root 0  6 2月  24 11:22 aaa
drwxr-xr-x 2 root 0  6 2月  24 11:22 aaaa
drwxr-xr-x 2 root 0  6 2月  24 11:22 ab
drwxr-xr-x 2 root 0  6 2月  24 11:22 bababa
drwxr-xr-x 2 root 0  6 2月  24 11:40 nnn
drwxr-xr-x 2 root 0 20 2月  24 14:47 test
[root@localhost opt]# cd test
[root@localhost test]# ll
总用量 0
-rw-r--r-- 1 root 0 0 2月  24 11:40 t1.txt
[root@localhost test]# 

2、删除指定文件之外的文件。

eg.除了含有“a和b”命名的文件,其他全部删除。

ls |grep -v a |xargs rm -rf

[root@localhost opt]# ll
总用量 0
drwxr-xr-x 2 root 0  6 2月  24 11:22 a
drwxr-xr-x 2 root 0  6 2月  24 11:22 aa
drwxr-xr-x 2 root 0  6 2月  24 11:22 aaa
drwxr-xr-x 2 root 0  6 2月  24 11:22 aaaa
drwxr-xr-x 2 root 0  6 2月  24 11:22 ab
drwxr-xr-x 2 root 0  6 2月  24 11:22 bababa
drwxr-xr-x 2 root 0  6 2月  24 11:40 nnn
drwxr-xr-x 2 root 0 20 2月  24 14:47 test
[root@localhost opt]# ls |grep -v a
nnn
test
[root@localhost opt]# ls |grep -v a |xargs rm
rm: 无法删除"nnn": 是一个目录
rm: 无法删除"test": 是一个目录
[root@localhost opt]# ls |grep -v a |xargs rm -rf
[root@localhost opt]# ll
总用量 0
drwxr-xr-x 2 root 0 6 2月  24 11:22 a
drwxr-xr-x 2 root 0 6 2月  24 11:22 aa
drwxr-xr-x 2 root 0 6 2月  24 11:22 aaa
drwxr-xr-x 2 root 0 6 2月  24 11:22 aaaa
drwxr-xr-x 2 root 0 6 2月  24 11:22 ab
drwxr-xr-x 2 root 0 6 2月  24 11:22 bababa
[root@localhost opt]# 

3、压缩解压缩gzip和gunzip

压缩:gzip 4

简单解压:(1)gzip -d 4.gz (2)gunzip 4.gz

保留源文件的压缩:gzip -c 4 >4.gz

保留压缩包的解压,会覆盖同名文件:gunzip -c 4.gz >4

将4压缩成666.gz包:gzip -c 4 >666.gz

解压666.gz包时,保留原来的文件名解压:gunzip -N 666.gz

将4.gz解压成666文件:gunzip -c 4.gz >666

目前我的测试中,-cN参数无法同时使用。

gzip -r test 将test目录下的所有子文件分别压缩,test还是目录,因为gzip命令只能压缩不能打包。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/745452.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号