Linux Commands#File, Directory
The GNU version of the tar archiving utility
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)
## Create tar -cvf test.tar Dir1 Dir2 <- Create, Verbose, File ## Test tar -tvf test.tar <- Test, Verbose, File ## eXtract tar -xvf test.tar <- eXtract, Verbose, File
## Create tar zcvf test.tar.gz DIR1 DIR2 tar zcvf testdir_back.tar.gz -C /etc testdir # cd /etc; tar zcvf testdir_back.tar.gz testdir ## Test tar ztvf test.tar.gz zcat FILE.tar.gz | tar tvf - ## eXtract tar zxvf test.tar.gz tar zxvf user01.tar.gz -C /home # cd /home; tar zxvf user01.tar.gz tar --no-same-owner zxvf test.tar.gz <- with root user owner
## Create tar jcvf test.tar.bz2 DIR1 DIR2 ## Test tar jtvf test.tar.bz2 <- test ## eXtract tar jxvf test.tar.bz2
# 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 # cd /home2; tar zxvf centos7_user01.tar.gz # rm -rf /home2
# 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
# 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/