cmdref.net - Cheat Sheet and Example

cmdref.net is command references/cheat sheets/examples for system engineers.

User Tools

Site Tools


Sidebar








Etc


Reference














.

os:linux:command:tar.html



Linux Commands#File, Directory

How to use tar command in Linux

The GNU version of the tar archiving utility



Option

Main operation mode:
       -c, --create
              create a new archive

       -f, --file=ARCHIVE
              use archive file or device ARCHIVE

       -v, --verbose
              verbosely list files processed

       -x, --extract, --get
              extract files from an archive

       -z, --gzip
              filter the archive through gzip

       -t, --list
              list the contents of an archive

       -j, --bzip2
              filter the archive through bzip2

Common options:
       -C, --directory=DIR
              change to directory DIR


Operation modifiers:
       --no-same-owner
              extract files as yourself (default for ordinary users)


Example

tar zcvf /tmp/test.tar.gz user01
tar zxvf user01.tar.gz
tar zxvf user01.tar.gz  -C /home

tar --no-same-owner zxvf test.tar.gz   <- with root user

tar

tar -cvf test.tar Dir1 Dir2   <- Create, Verbose, File
tar -tvf test.tar   <- Test, Verbose, File
tar -xvf test.tar   <- eXtract, Verbose, File




tar.gz、tgz

tar zcvf test.tar.gz DIR1 DIR2
tar ztvf test.tar.gz          <- test
zcat FILE.tar.gz | tar tvf -    <- test
tar zxvf test.tar.gz 


tar.bz2、tbz2

tar jcvf test.tar.bz2 DIR1 DIR2
tar jtvf test.tar.bz2   <- test
tar jxvf test.tar.bz2


Tips

Checking Full Path

Good
# useradd user01
# cd /home
# tar zcvf /tmp/`hostname`_user01.tar.gz user01
user01/
user01/.bash_logout
user01/.bash_profile
user01/.bashrc
#
# cd /tmp/
# tar ztvf centos7_user01.tar.gz
drwx------ user01/user01     0 2018-08-08 01:11 user01/
-rw-r--r-- user01/user01    18 2017-09-07 01:25 user01/.bash_logout
-rw-r--r-- user01/user01   193 2017-09-07 01:25 user01/.bash_profile
-rw-r--r-- user01/user01   231 2017-09-07 01:25 user01/.bashrc
#
# mkdir /home2
# tar zxvf centos7_user01.tar.gz -C /home2
# rm -rf /home2
No Good
# cd /tmp/
# tar zcvf user01.tar.gz /home/user01
tar: Removing leading `/' from member names
/home/user01/
/home/user01/.bash_logout
/home/user01/.bash_profile
/home/user01/.bashrc
#
# tar ztvf user01.tar.gz
drwx------ user01/user01     0 2018-08-08 01:11 home/user01/
-rw-r--r-- user01/user01    18 2017-09-07 01:25 home/user01/.bash_logout
-rw-r--r-- user01/user01   193 2017-09-07 01:25 home/user01/.bash_profile
-rw-r--r-- user01/user01   231 2017-09-07 01:25 home/user01/.bashrc


Exclude

# mkdir test
# mkdir test/test1
# mkdir test/test2
# mkdir test/test3
# mkdir test/test4
# mkdir test/test4/test1

# tree test
test
|-- test1
|-- test2
|-- test3
`-- test4
    `-- test1

# tar zcvf test1.tar.gz --exclude test/test1  --exclude test/test2  test
test/
test/test4/
test/test4/test1/
test/test3/


# vi test.conf
test/test1
test/test2

# tar zcvf test2.tar.gz -X test.conf test
test/
test/test4/
test/test4/test1/
test/test3/



os/linux/command/tar.html.txt · Last modified: 2018/08/08 by admin

Page Tools