Linux TAR Command
 

Linux TAR Command

 

This post was last updated on May 27th, 2020 at 05:18 pm

create:

tar -cvf mystuff.tar mystuff/
tar -czvf mystuff.tgz mystuff/

extracting:

tar -xvf mystuff.tar
tar -xzvf mystuff.tgz

testing/viewing:

tar -tvf mystuff.tar
tar -tzvf mystuff.tgz

Note that .tgz is the same thing as .tar.gz
Tar “tars up” a bunch of files into one “tar-file”
gzip is compression, but only works on one file, so the entire “tarfile” is compressed.

Also when creating a tar or cpio backup, never, never, never use an “absolute” path — you have been warned; also linux tar warns you of this too. The problem is that when you want to unpack, you cannot choose where to unpack to, you will be forced to unpack to the “same” absolute path. When creating a tar or cpio you should change the the appropriate directory and tar from there.

Also when creating a tar or cpio it is general good practice to tar up a directory (appropriately named) which contains your files, rather than just the files. This is good courtesy to anyone unpacking your tarfile.

Also take note of the following commands:
gzip
gunzip
cat
zcat
bzcat
bzip2
bunzip2
zgrep
bzgrep

(cd /mydir && tar -czf – .)|(cd /destdir && tar -xzvf -)
tar -czf – . | ssh [email protected] “(cd /destdir && tar -xzvf -)”
(cd /mydir && tar -czf – .) | ssh us[email protected] “(cd /destdir && tar -xzvf -)”

Errors:

If you receive an error such as the following:

[[email protected] test]# ssh [email protected] "(cd /rh62/home/kernel && tar -czvf - linux-2.2.22.tar.bz2)"|tar -xzvf -
[email protected]'s password:
linux-2.2.22.tar.bz2
linux-2.2.22.tar.bz2

gzip: stdin: decompression OK, trailing garbage ignored
tar: Child returned status 2
tar: Error exit delayed from previous errors
[[email protected] test]#

Then try leaving off the compression as follows:

[[email protected] test]# ssh [email protected] "(cd /rh62/home/kernel && tar -cvf - linux-2.2.22.tar.bz2)"|tar -xvf -
[email protected]'s password:
linux-2.2.22.tar.bz2
linux-2.2.22.tar.bz2
[[email protected] test]# echo $?
0
[[email protected] test]# 

Spaces in your filenames?

Having fun with spaces in the filename or directory name? Here’s your answer:

find . -type f -name '*jpg' | grep " " | while read REPLY; do
     tar -cf - "$REPLY" | (cd /tmp/jpg && tar -xvf -)
done

Bonus:

find . -type f | while read REPLY; do
     cp -a "$REPLY" /tmp/jpg/`echo $REPLY \
       | sed -e 's/ /_/g' -e 's/\//_/g' -e 's/^\._//'`
done

ssh [email protected] ‘tar -C / -czf – –exclude ./proc/\* –exclude ./dev/pts/\* .’ > gmaster.tgz
proc and dev was empty for the following command, so I didn’t exclude them:
tar -czf – . | ssh [email protected] “cat – > k12ltsp.master.tgz”

Previous Guided Installation of RedHat 6 on Compaq Smart Array Controller
Next Using a tape drive on a CCISS controller
 

About author

shibaa987
shibaa987 274 posts

Linux kernel developer and a firmware developer with an experience of 10+ years.

View all posts by this author →

You might also like

Commands 0 Comments

How to copy files & directories in Linux terminal?

Copying files and directories in Linux is one of the most common tasks. If you are using a graphical desktop environment then you can easily copy files or directories using

Commands 0 Comments

Top Command Usage With Example In Linux/Unix

This post was last updated on August 1st, 2020 at 08:47 pmThe top is one of the most frequently used command in Unix like operating systems for the administrative tasks.

Commands 0 Comments

Understanding The Output Of Top Command In Linux Or Unix

This post was last updated on August 1st, 2020 at 08:48 pmThe top is one of the most frequently used command in Unix like operating systems for the administrative tasks.

0 Comments

No Comments Yet!

You can be first to comment this post!

Leave a Reply