博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Linux基础 - 文件管理 解压缩

Posted on 2023-02-18 19:19  Kingdomer  阅读(37)  评论(0编辑  收藏  举报

 

tar zcvf monitor.tar.gz monitor/ --exclude=monitor/aa/logs/* --exclude=monitor/*.log  # 排查文件时,不能写完整路径,用相对路径

tar zcvf td2.tar.gz ./testdir/ --exclude=df.sh --exclude=a*    # 备份时,排除文件和文件目录

tar -N '2023-02-19 18:00:00' -zcvf /tmp/r.tar.gz /root/        # 只备份特定时间后的文件

tar -zcvf - yum.repos.d/ | openssl des3 -salt -k {passwd} -out /root/yum.tar.gz  # 打包时加密
openssl des3 -d -k {passwd} -salt -in yum.tar.gz | tar zxvf -                    # 加密后解包

 

### gzip
[root@centos78 tmp]# gzip yum.log         # 压缩后源文件消失
[root@centos78 tmp]# gzip -d yum.log.gz   

[root@centos78 ~]# gzip -r testdir/       # 递归对文件目录下的文件进行压缩
[root@centos78 testdir]# ls
ab  ab.txt.gz  ac  bb.log.gz  bc.log.gz  bd  be  dd.sh.gz  df  df.sh.gz  dg
[root@centos78 ~]# gzip -d -r testdir/    # 解压

### bzip2 不能对文件目录操作
[root@centos78 ~]# bzip2 -z ab.txt        # 压缩后源文件消失
[root@centos78 ~]# bzip2 -d ab.txt.bz2    # 解压文件

### zip与unzip
[root@centos78 ~]# yum install zip unzip
[root@centos78 ~]# zip ab.txt.zip ab.txt  # 源文件不会消失
[root@centos78 ~]# unzip ab.txt.zip       # zip文件不会消失
Archive:  ab.txt.zip
replace ab.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
 extracting: ab.txt

[root@centos78 ~]# zip -r td.zip  testdir/
[root@myos ~]# zip abc2.zip abc/ hsperfdata_root/     # 只打包文件目录
[root@myos ~]# zip -r abc2.zip abc/ hsperfdata_root/  # 将文件夹及其下文件进行打包
[root@myos ~]# unzip -l abc2.zip                      # 查看zip包的内容
[root@myos ~]# unzip abc2.zip -d /usr/                # 解压到指定路径

  

# -N只对特定时间之后的进行打包
[root@centos78 ~]# ll
total 8
-rw-r--r--. 1 root root    3 Feb 19 18:27 ab.txt
-rw-------. 1 root root 1259 Feb 19 17:17 anaconda-ks.cfg

[root@centos78 ~]# tar -N '2023-02-19 18:00:00' -zcvf /tmp/r.tar.gz /root/
tar: Removing leading `/' from member names
/root/
/root/.bash_history
/root/ab.txt
[root@centos78 ~]# tar tvf /tmp/r.tar.gz
dr-xr-x--- root/root         0 2023-02-19 18:26 root/
-rw------- root/root       976 2023-02-19 18:09 root/.bash_history
-rw-r--r-- root/root         3 2023-02-19 18:27 root/ab.txt

 

# 排除指定文件
[root@centos78 testdir]# ls
ab  ab.txt  ac  bb.log  bc.log  bd  be  dd.sh  df  df.sh  dg
[root@centos78 testdir]# cd ..
[root@centos78 ~]# tar zcvf td1.tar.gz ./testdir/ --exclude=df.sh
./testdir/
./testdir/ab/
./testdir/ac/
./testdir/bd/
./testdir/be/
./testdir/df/
./testdir/dg/
./testdir/ab.txt
./testdir/bb.log
./testdir/bc.log
./testdir/dd.sh
[root@centos78 ~]# tar zcvf td2.tar.gz ./testdir/ --exclude=df.sh --exclude=a*
./testdir/
./testdir/bd/
./testdir/be/
./testdir/df/
./testdir/dg/
./testdir/bb.log
./testdir/bc.log
./testdir/dd.sh

 

[root@centos78 ~]# tar -cvf /tmp/etc.tar /etc       # 仅打包,不压缩
[root@centos78 ~]# tar -xvf etc.tar                 # 解包

[root@centos78 ~]# tar -zcvf /tmp/etc.tar.gz /etc   # 打包,以gzip压缩
[root@centos78 ~]# tar -zxvf /tmp/etc.tar.gz        # 解包

[root@centos78 ~]# yum install bzip2
[root@centos78 ~]# tar -jcvf /tmp/etc.tar.bz2 /etc  # 打包,以bzip2压缩
[root@centos78 ~]# tar -jxvf /tmp/etc.tar.bz2       # 解包
 
[root@centos78 ~]# tar tvf /tmp/etc.tar             # -t 查看压缩包中文件列表, -v 查看文件的详细信息
[root@centos78 ~]# tar tvf /tmp/etc.tar.bz2
[root@centos78 ~]# tar zxvfp /tmp/etc.tar.gz        # -p 解压时保存文件原有属性

[root@centos78 ~]# tar zxvf /tmp/etc.tar.gz etc/group  # 只解压出包中的一个文件
etc/group 

[root@centos78 tmp]# tar -cvf - /etc | tar -xvf -      # 将/etc打包后直接解开到/tmp,在shell中使用

 

[root@cl-node03 docker_image_build]# ls
Dockerfile  index.html  nginx-1.20.1.tar.gz  yum.repos.d
[root@cl-node03 docker_image_build]# tar -zcvf - yum.repos.d/ | openssl des3 -salt -k {passwd} -out /root/yum.tar.gz
yum.repos.d/
yum.repos.d/CentOS-Base.repo
yum.repos.d/CentOS-CR.repo
yum.repos.d/CentOS-Debuginfo.repo
yum.repos.d/CentOS-Media.repo
yum.repos.d/CentOS-Sources.repo
yum.repos.d/CentOS-Vault.repo
yum.repos.d/CentOS-fasttrack.repo
yum.repos.d/CentOS-x86_64-kernel.repo

[root@cl-node03 ~]# tar zxvf yum.tar.gz 
gzip: stdin: not in gzip format
tar: Child returned status 1
tar: Error is not recoverable: exiting now

[root@cl-node03 ~]# openssl des3 -d -k {passwd} -salt -in yum.tar.gz | tar zxvf -
yum.repos.d/
yum.repos.d/CentOS-Base.repo
yum.repos.d/CentOS-CR.repo
yum.repos.d/CentOS-Debuginfo.repo
yum.repos.d/CentOS-Media.repo
yum.repos.d/CentOS-Sources.repo
yum.repos.d/CentOS-Vault.repo
yum.repos.d/CentOS-fasttrack.repo
yum.repos.d/CentOS-x86_64-kernel.repo

 

[root@myos ~]# 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@myos ~]#  unzip
UnZip 6.00 of 20 April 2009, by Info-ZIP.  Maintained by C. Spieler.  Send
bug reports using http://www.info-zip.org/zip-bug.html; see README for details.

Usage: unzip [-Z] [-opts[modifiers]] file[.zip] [list] [-x xlist] [-d exdir]
  Default action is to extract files in list, except those in xlist, to exdir;
  file[.zip] may be a wildcard.  -Z => ZipInfo mode ("unzip -Z" for usage).

  -p  extract files to pipe, no messages     -l  list files (short format)
  -f  freshen existing files, create none    -t  test compressed archive data
  -u  update files, create if necessary      -z  display archive comment only
  -v  list verbosely/show version info       -T  timestamp archive to latest
  -x  exclude files that follow (in xlist)   -d  extract files into exdir
modifiers:
  -n  never overwrite existing files         -q  quiet mode (-qq => quieter)
  -o  overwrite files WITHOUT prompting      -a  auto-convert any text files
  -j  junk paths (do not make directories)   -aa treat ALL files as text
  -U  use escapes for all non-ASCII Unicode  -UU ignore any Unicode fields
  -C  match filenames case-insensitively     -L  make (some) names lowercase
  -X  restore UID/GID info                   -V  retain VMS version numbers
  -K  keep setuid/setgid/tacky permissions   -M  pipe through "more" pager
  -O CHARSET  specify a character encoding for DOS, Windows and OS/2 archives
  -I CHARSET  specify a character encoding for UNIX and other archives

See "unzip -hh" or unzip.txt for more help.  Examples:
  unzip data1 -x joe   => extract all files except joe from zipfile data1.zip
  unzip -p foo | more  => send contents of foo.zip via pipe into program more
  unzip -fo foo ReadMe => quietly replace existing ReadMe if archive file newer