Linux command tar command

Keywords: Linux

tar command

  • Functional description: Back up files

  • Usage: tar [options]...Archive and Compress File Name FILE...

  • Note: The'-'may be omitted from the tar command option

    option Effect
    -c Create an archive file in.tar format
    -C Specify the destination folder when expanding the archive
    -f Indicates the use of archive files
    -t List View Files in Archive
    -x Unzip package file in.tar format
    -z Call gzip program to compress or decompress
    -j Call bzip2 program to compress or decompress
    -J Call xz program to compress or decompress
    -r Add files to archived files
    -p Preserve file and directory permissions when packaging
    -v Output Details
    --exclude Exclude specified files when packaging, dominate wildcards
    -X Specify a file to write exclusions to

File archiving is to package several files into one file first. One disadvantage of archiving is that it will not reduce the volume but also increase the volume. Generally speaking, after archiving, you will use the compression tool to compress them. Archiving and compression are usually used together, and the archive will not delete the original file.

Example 1: Back up the / etc directory as an etc.tar file

[root@node1 ~]# tar cf etc.tar /etc/
tar: Removing leading `/' from member names
[root@node1 ~]# ll -h 
total 28M
-rw-r--r-- 1 root root 28M Feb 25 17:46 etc.tar

Example 2: View a list in the etc.tar file

[root@node1 ~]# tar tf etc.tar 

Example 3: Release the etc.tar file to the current directory

[root@node1 ~]# tar xf etc.tar
[root@node1 ~]# ls 
etc  etc.tar

Example 4: Release the etc.tar file to the /tmp/newtest directory

tar [options]...Archive and Compress File Name [-C Target Directory]

[root@node1 ~]# mkdir /tmp/new
[root@node1 ~]# tar xf etc.tar -C /tmp/new
[root@node1 ~]# ll /tmp/new/
total 12
drwxr-xr-x 74 root root 8192 Feb 25 12:43 etc

Example 5: Create five files to package and add/etc/passwd to the package file.

[root@node1 ~]# for i in `seq 5`;do touch $i.txt;done
[root@node1 ~]# ls
1.txt  2.txt  3.txt  4.txt  5.txt  etc  etc.tar
[root@node1 ~]# tar cf test.tar *.txt
[root@node1 ~]# tar tf test.tar 
1.txt
2.txt
3.txt
4.txt
5.txt
5.txt
[root@node1 ~]# tar rf test.tar /etc/passwd
tar: Removing leading `/' from member names
[root@node1 ~]# tar tf test.tar 
1.txt
2.txt
3.txt
4.txt
5.txt
etc/passwd

When archiving is complete, compression is usually required, and with compression tools (gzip, bzip2, xz), you can compress multiple files.

Example 6: Copy all files under /var/log to the / root directory and archive using gzip, bzip2, xz, respectively

[root@node1 ~]# cp -r /var/log/ ./
[root@node1 ~]# du -sh log/
4.1M    log/
[root@node1 ~]# tar zcf log.gz log/	#Call gzip for compression
[root@node1 ~]# tar jcf log1.bz2 log/	#Call bzip2 for compression
[root@node1 ~]# tar Jcf log2.xz log/	#Call xz for compression
[root@node1 ~]# du -sh *
4.1M    log
300K    log1.bz2
204K    log2.xz
412K    log.gz

Example 7: Create {a-e}5 txt files and package them, but exclude a.txt and b.txt from the packaging process

[root@node1 ~]# mkdir /tmp/test
[root@node1 ~]# cd /tmp/test
[root@node1 test]# for i in {a..e};do touch $i.txt;done
[root@node1 test]# ls
a.txt  b.txt  c.txt  d.txt  e.txt
[root@node1 test]# tar cf test.tar *.txt --exclude=a.txt --exclude=b.txt
[root@node1 test]# tar tf test.tar 
c.txt
d.txt
e.txt

Example 8: Package all files in the root directory, but exclude mp3 files.

[root@node1 test]# for i in `seq 10`;do touch $i.mp3;done
[root@node1 test]# ls
10.mp3  1.mp3  2.mp3  3.mp3  4.mp3  5.mp3  6.mp3  7.mp3  8.mp3  9.mp3  a.txt  b.txt  c.txt  d.txt  e.txt
[root@node1 test]# tar cf text.tar *.txt --exclude=*.mp3
[root@node1 test]# tar tf text.tar 
a.txt
b.txt
c.txt
d.txt
e.txt

Example 9: Package the MP3 file in example 8, but exclude 1.mp3, 3.mp3, 5.mp3

[root@node1 test]# cat >>exclude.txt<<EOF
> 1.mp3
> 3.mp3
> 5.mp3
> EOF

[root@node1 test]# tar cf mp3.tar *.mp3 -X exclude.txt 
[root@node1 test]# tar tf mp3.tar 
10.mp3
2.mp3
4.mp3
6.mp3
7.mp3
8.mp3
9.mp3

Posted by Kazlaaz on Sun, 17 May 2020 09:40:41 -0700