美文网首页
tar 常用案例

tar 常用案例

作者: 轻飘飘D | 来源:发表于2020-11-24 18:38 被阅读0次

1.打包但不壓縮

[oracle@xag182 test_tar]$ ls
1.txt  2.txt

[oracle@xag182 test_tar]$ tar cvf test1.tar 1.txt
1.txt

[oracle@xag182 test_tar]$ tar -tvf test1.tar 
-rw-r--r-- oracle/oinstall   0 2019-09-23 10:07 1.txt

[oracle@xag182 test_tar]$ tar -rvf test1.tar 2.txt 
2.txt

[oracle@xag182 test_tar]$ tar -tvf test1.tar 
-rw-r--r-- oracle/oinstall   0 2019-09-23 10:07 1.txt
-rw-r--r-- oracle/oinstall   0 2019-09-23 10:07 2.txt

2.絕對路徑打包

[oracle@xag182 test_tar]$ tar -cPf /home/oracle/test_tar/test2.tar /home/oracle/test_tar/1.txt 
/home/oracle/test_tar/1.txt

[oracle@xag182 test_tar]$ tar -rPf /home/oracle/test_tar/test2.tar /home/oracle/test_tar/2.txt 
/home/oracle/test_tar/1.txt

[oracle@xag182 test_tar]$ tar -tvPf /home/oracle/test_tar/test2.tar 
-rw-r--r-- oracle/oinstall   0 2019-09-23 10:07 /home/oracle/test_tar/1.txt
-rw-r--r-- oracle/oinstall   0 2019-09-23 10:07 /home/oracle/test_tar/2.txt

[oracle@xag182 test_tar]$ tar -tPf /home/oracle/test_tar/test2.tar 
/home/oracle/test_tar/1.txt
/home/oracle/test_tar/2.txt

3.打包後刪除原文件

[oracle@xag182 test_tar]$  tar -cPf /home/oracle/test_tar/test3.tar /home/oracle/test_tar/1.txt --remove-files
[oracle@xag182 test_tar]$  tar -rPf /home/oracle/test_tar/test3.tar /home/oracle/test_tar/2.txt --remove-files

[oracle@xag182 test_tar]$ ls
test3.tar

[oracle@xag182 test_tar]$ tar -tPf /home/oracle/test_tar/test3.tar 
/home/oracle/test_tar/1.txt
/home/oracle/test_tar/2.txt

4.打包某日期後的文件

[oracle@xag182 test_tar]$ pwd
/home/oracle/test_tar

[oracle@xag182 test_tar]$ ll -1R
--------------------------------------------
[oracle@xag182 test_tar]$ ll -1R
.:
total 0
drwxr-xr-x 4 oracle oinstall 66 Nov 26 09:52 boot

./boot:
total 8
drwxr-xr-x 2 oracle oinstall   39 Nov 26 09:51 aaa
drwxr-xr-x 2 oracle oinstall   42 Nov 26 09:51 bbb
-rw-r--r-- 1 oracle oinstall 1072 Nov 26 09:52 boot001.dat
-rw-r--r-- 1 oracle oinstall 1072 Nov 26 09:48 boot001.txt

./boot/aaa:
total 8
-rw-r--r-- 1 oracle oinstall 458 Nov 26 09:51 aaa001.dat
-rw-r--r-- 1 oracle oinstall 458 Nov 26 09:47 aaa.txt

./boot/bbb:
total 8
-rw-r--r-- 1 oracle oinstall 171 Nov 26 09:51 bbb001.dat
-rw-r--r-- 1 oracle oinstall 171 Nov 26 09:46 bbb001.txt
-------------------------------------------

[oracle@xag182 test_tar]$ tar -N '2020/11/26 09:47:00' -cvPf /home/oracle/test_tar/test1.tar /home/oracle/test_tar/boot/
---------------------------------------------------------------------------------------------------------------
tar: Option --after-date: Treating date `2020/11/26 09:47:00' as 2020-11-26 09:47:00
/home/oracle/test_tar/boot/
/home/oracle/test_tar/boot/aaa/
/home/oracle/test_tar/boot/aaa/aaa.txt
/home/oracle/test_tar/boot/aaa/aaa001.dat
/home/oracle/test_tar/boot/bbb/
tar: /home/oracle/test_tar/boot/bbb/bbb001.txt: file is unchanged; not dumped
/home/oracle/test_tar/boot/bbb/bbb001.dat
/home/oracle/test_tar/boot/boot001.txt
/home/oracle/test_tar/boot/boot001.dat
---------------------------------------------------------------------------------------------------------------

[oracle@xag182 test_tar]$ tar -tPf test1.tar
/home/oracle/test_tar/boot/
/home/oracle/test_tar/boot/aaa/
/home/oracle/test_tar/boot/aaa/aaa.txt
/home/oracle/test_tar/boot/aaa/aaa001.dat
/home/oracle/test_tar/boot/bbb/
/home/oracle/test_tar/boot/bbb/bbb001.dat
/home/oracle/test_tar/boot/boot001.txt
/home/oracle/test_tar/boot/boot001.dat

-- 加上 *.txt後,僅僅能夠搜索 boot/目錄下,不能夠再搜索再下一層的子目錄
[oracle@xag182 test_tar]$ tar -N '2020/11/26 09:47:00' -cvPf /home/oracle/test_tar/test2.tar /home/oracle/test_tar/boot/*.txt
tar: Option --after-date: Treating date `2020/11/26 09:47:00' as 2020-11-26 09:47:00
/home/oracle/test_tar/boot/boot001.txt

[oracle@xag182 test_tar]$ tar -tPf test2.tar
/home/oracle/test_tar/boot/boot001.txt

5.結合find 命令一起使用

[oracle@xag182 test_tar]$ ll -1R

-rw-r--r-- 1 oracle oinstall 1072 Nov 26 09:52 boot001.dat
-rw-r--r-- 1 oracle oinstall 1072 Nov 26 09:48 boot001.txt
-rw-r--r-- 1 oracle oinstall 458 Nov 26 09:51 aaa001.dat
-rw-r--r-- 1 oracle oinstall 458 Nov 26 09:47 aaa.txt
-rw-r--r-- 1 oracle oinstall 171 Nov 26 09:51 bbb001.dat
-rw-r--r-- 1 oracle oinstall 171 Nov 26 09:46 bbb001.txt

[oracle@xag182 test_tar]$ find ./boot -type f -newermt '2020-11-26 09:47:00' ! -newermt '2020-11-26 09:51:59'
./boot/aaa/aaa.txt
./boot/aaa/aaa001.dat
./boot/bbb/bbb001.dat
./boot/boot001.txt

[oracle@xag182 test_tar]$ find ./boot -name *.dat
./boot/aaa/aaa001.dat
./boot/bbb/bbb001.dat
./boot/boot001.dat

[oracle@xag182 test_tar]$ find ./boot -name *.dat | xargs tar -cvf test4.tar
./boot/aaa/aaa001.dat
./boot/bbb/bbb001.dat
./boot/boot001.dat

[oracle@xag182 test_tar]$ tar -tPf test3.tar
./boot/aaa/aaa001.dat
./boot/bbb/bbb001.dat
./boot/boot001.dat

[oracle@xag182 test_tar]$ tar -tPf test4.tar
./boot/aaa/aaa001.dat
./boot/bbb/bbb001.dat
./boot/boot001.dat

[oracle@xag182 test_tar]$ find ./boot -name "*.dat" -exec tar -cvf test5.tar {} \;
./boot/aaa/aaa001.dat
./boot/bbb/bbb001.dat
./boot/boot001.dat

[oracle@xag182 test_tar]$ tar -cvf test6.tar $(find ./boot -name "*.dat")
./boot/aaa/aaa001.dat
./boot/bbb/bbb001.dat
./boot/boot001.dat

6.結合 ls find命令

[oracle@xag182 test_tar]$ ls
a1.txt  a2.txt  a3.dat  b1.txt  b2.txt  b3.dat

[oracle@xag182 test_tar]$  ls *.dat | xargs -i  tar zcvf {}.tar.gz {}
a3.dat
b3.dat

[oracle@xag182 test_tar]$ ls
a1.txt  a2.txt  a3.dat  a3.dat.tar.gz  b1.txt  b2.txt  b3.dat  b3.dat.tar.gz

[oracle@xag182 test_tar]$  ls *.txt | xargs -i  tar zcf {}.tar.gz {}

[oracle@xag182 test_tar]$ ls *.txt.tar.gz
a1.txt.tar.gz  a2.txt.tar.gz  b1.txt.tar.gz  b2.txt.tar.gz

[oracle@xag182 test_tar]$ 
ls -1 -f ./boot -I "." -I ".." 2>/dev/null | xargs -i find ./boot/{} -name "*.dat" 
./boot/aaa/aaa001.dat
./boot/bbb/bbb001.dat
./boot/boot001.dat

[oracle@xag182 test_tar]$ 
ls -1 -f ./boot -I "." -I ".." 2>/dev/null | xargs -i find ./boot/{} -name "*.dat" ! -name "bbb*.dat"
./boot/aaa/aaa001.dat
./boot/boot001.dat

[oracle@xag182 test_tar]$ 
ls -1 -f ./boot -I "." -I ".." 2>/dev/null | xargs -i find ./boot/{} -name "*.dat" ! -name "bbb*.dat" | xargs tar -cvf test7.tar
./boot/aaa/aaa001.dat
./boot/boot001.dat

[oracle@xag182 test_tar]$ 
ls -1 -f ./boot -I "." -I ".." 2>/dev/null | xargs -i find ./boot/{} -name "*.dat" ! -name "bbb*.dat" 1>filelist.dat 2>/dev/null

[oracle@xag182 test_tar]$ cat filelist.dat 
./boot/aaa/aaa001.dat
./boot/boot001.dat

[oracle@xag182 test_tar]$ tar -T filelist.dat -cvf test8.tar
./boot/aaa/aaa001.dat
./boot/boot001.dat

[oracle@xag182 test_tar]$ tar -tPf test8.tar 
./boot/aaa/aaa001.dat
./boot/boot001.dat

7.find ( maxdepth 及 mindepth)

[oracle@xag182 ~]$ pwd
/home/oracle

[oracle@xag182 ~]$ find /home/oracle/test_tar -maxdepth 2 -mindepth 1 -type f -name "*.txt"
/home/oracle/test_tar/a1.txt
/home/oracle/test_tar/a2.txt
/home/oracle/test_tar/b1.txt
/home/oracle/test_tar/b2.txt
/home/oracle/test_tar/aaaa/aa01.txt
/home/oracle/test_tar/aaaa/aa02.txt
/home/oracle/test_tar/bbbb/bb01.txt
/home/oracle/test_tar/bbbb/bb02.txt

[oracle@xag182 ~]$ find /home/oracle/test_tar -maxdepth 1 -mindepth 1 -type f -name "*.txt"
/home/oracle/test_tar/a1.txt
/home/oracle/test_tar/a2.txt
/home/oracle/test_tar/b1.txt
/home/oracle/test_tar/b2.txt
  1. cur(ftp案例)
[mppay@UAT_C_TDRMP-101_6 tmp]$ find . -name "*.txt" |xargs -i curl ftp://192.168.1.200//CUR/UAT/C/pmc_10.0.101.7/tomcat/logs/pmc/ -u "applog:2Yv0kgAml0AYi6tr" -T {}
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 11111    0     0  100 11111      0   132k --:--:-- --:--:-- --:--:--     0
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     8    0     0    0     8      0     28 --:--:-- --:--:-- --:--:--     0

[mppay@UAT_C_TDRMP-101_6 tmp]$ ls *.txt |xargs -i curl ftp://192.168.1.200//CUR/UAT/C/pmc_10.0.101.7/tomcat/logs/pmc/ -u "applog:2Yv0kgAml0AYi6tr" -T {}
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     8    0     0    0     8      0     97 --:--:-- --:--:-- --:--:--     0
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 11111    0     0  100 11111      0  39173 --:--:-- --:--:-- --:--:--     0

相关文章

  • tar 常用案例

    1.打包但不壓縮 2.絕對路徑打包 3.打包後刪除原文件 4.打包某日期後的文件 5.結合find 命令一起使用 ...

  • Linux 打包压缩

    windows常用rar mac常用zip linux常用tar.gz 打包tar -cvf 新包名.tar 要打...

  • Linux-tar命令解析

    tar -cxtru 案例:tar -cvf result.tar targettar -cvf result.t...

  • 2019-07-08打包压缩

    不同系统中常用的打包方式是不同的Windows常用rarMac常用zipLinux常用tar.gz tar演练-t...

  • Linux打包压缩

    不同系统中常用的打包方式是不同的Windows常用rarMac常用zipLinux常用tar.gz tar演练-t...

  • Linux压缩与解压缩命令

    常用压缩格式 压缩格式: .zip、.gz、.bz2、.tar.gz、.tar.bz2、.tar.xz最常用的是....

  • 5.Linux—压缩命令

    压缩命令 压缩常用的是:.tar.gz .tar.zb2 常用压缩格式:.zip .gz .bz2 常用压缩...

  • 压缩命令 zip gz

    常用压缩格式: .zip .gz .bz2常用压缩格式: .tar.gz .tar.bz2 .zip格式...

  • linux下常用的解压命令

    #linux下常用的解压命令 ****** * ```.tar``` 解包:```tar xvf FileNam...

  • Linux tar 快速查看

    tar的本质是将多个文件合并成一个,常用参数有: 0. tar 归档:tar -cf all.tar *.jpg ...

网友评论

      本文标题:tar 常用案例

      本文链接:https://www.haomeiwen.com/subject/mmmiiktx.html