Facebook Twitter Instagram
    WiredRevolution.com
    • Home
    • About
    • Contact Us
    • Essential Linux Commands
    • Sitemap
    Facebook Twitter Instagram
    WiredRevolution.com
    commands

    Using tar to archive files

    RyanBy RyanOctober 7, 2008Updated:October 30, 20083 Mins Read
    Facebook Twitter Pinterest LinkedIn Reddit WhatsApp Email

    TAR is the GNU Tape ARchive utility. It is used to pack the contents of multiple files or directories in an archive file called a tarball. Tar can preserve directory organization including file ownership, permissions, links, as well as directory structure. To save space you can optionally enable compression with gzip, bzip2, or another external program.

    For these examples I’m going to be using this sample directory which contains a few files.

    /mydir
    /mydir/script.sh
    /mydir/picture.jpg
    /mydir/document.txt
    

    Lets create a tarball from this directory. Run the following command.

    $ tar -cvf mydir.tar mydir
    

    Lets go over these options.

    -c create tarball
    -v verbose
    -f output file

    To extract this tarball.

    $ tar -xvf mydir.tar
    

    The new option ‘-x‘ instructs tar to extract the archive.

    Add compression with gzip when creating the archive by including the -z option.

    $ tar -cvzf mydir.tgz mydir
    

    Extracting the archive is similar. Make sure to include the gzip option as well.

    $ tar -xvzf mydir.tgz
    

    You can also use the bzip2 utility to compress the archive, use the -j option.

    $ tar -cvjf mydir.tbz mydir
    

    And extract it.

    $ tar -xvjf mydir.tbz
    

    To preserve permissions use ‘-p‘ when creating as well as extracting.

    $ tar -cvzpf mydir.tgz mydir
    
    $ tar -xvzpf mydir.tgz
    

    You may have noticed the file extensions used on the archives are different. Here is a quick rundown on the naming conventions. Understanding the extensions allows you to quickly figure out how to extract it.

    *.tar (uncompressed tar archive)
    *.tgz or *.tar.gz (gzip compressed tar archive)
    *.tbz or *.tar.bz2 (bzip2 compressed tar archive)

    You want to avoid including files in the current working directory when creating a tarball. When extracted these files will be dumped into the user’s current working directory instead of its own subdirectory, potentially overwriting files and creating a big mess.

    Another helpful option to use is ‘-C ‘. This will extract a tarball in a specific directory.

    $ tar -xvzf mydir.tgz -C /tmp/testdir
    

    A good way to check for any problems with extraction or to see exactly a tarball contains is to execute tar with the -t option. This will list the contents of a tarball and show the location they will be extracted.

    $ tar -tf mydir.tgz
    
    mydir/
    mydir/script.sh
    mydir/picture.jpg
    mydir/document.txt
    

    This will also help you avoid situations where a tarball might use an absolute path (path starting at root ‘/’) when extracting instead of a relative path (path that is relative to your current directory) like the previous examples. This is not necessarily a bad thing, but it can be dangerous if you don’t realize this before extracting, since an absolute path can install to anywhere on your system.

    Share. Facebook Twitter Pinterest LinkedIn WhatsApp Reddit Email
    Previous ArticleView program output with watch
    Next Article Determine file type with the file command

    Related Posts

    How to X session forwarding over SSH

    Download digital camera photos with gphoto2

    List all open files with lsof

    Most Commented
    March 12, 2009

    Fix blue tinted video in Ubuntu

    September 10, 2010

    Setup SSH access between VirtualBox Host and Guest VMs

    March 8, 2011

    Install GNOME Shell in Ubuntu 10.10 Maverick

    April 4, 2009

    Setup the PS3 Bluetooth Controller on Ubuntu

    October 22, 2008

    How to correctly use LD_LIBRARY_PATH

    Recent Comments
    • Execute command on linux virtual machine (or server) from windows commandline on Setup SSH access between VirtualBox Host and Guest VMs
    • Solved: How to SSH to a VirtualBox guest externally through a host? - Daily Developer Blog on Setup SSH access between VirtualBox Host and Guest VMs
    • How to SSH to a VirtualBox guest externally through a host? [closed] – Code D3 on Setup SSH access between VirtualBox Host and Guest VMs
    • How to copy and paste from VirtualBox? [duplicate] on Setup SSH access between VirtualBox Host and Guest VMs
    • Jackie Laguna on Fix OpenGL: ChoosePixelFormat SketchUp error in WINE
    © 2025 ThemeSphere. Designed by ThemeSphere.

    Type above and press Enter to search. Press Esc to cancel.