Home > Linux > Create a tar file of an entire directory and its sub-directories

Create a tar file of an entire directory and its sub-directories

November 2nd, 2005 Tony Leave a comment Go to comments

To create a tar archive of an entire directory including all files and sub-directories:

tar -cvf mytarfile.tar mydir/

Categories: Linux Tags:
  1. John
    July 15th, 2007 at 12:32 | #1

    tar -cvfz mytarfile.tgz mydir/ will also compress the directory (gzip)

  2. August 8th, 2007 at 14:07 | #2

    Also … for a more universally compatible file try
    # zip -rv9 myzipfile.zip /mydir

    -9 is for maximum compression
    -v is for verbose output
    -r is recursive directories

  3. George
    May 19th, 2008 at 10:45 | #3

    The problem with the zip format is that it does not store file permissions.

  4. December 22nd, 2008 at 13:22 | #4

    I thank you so muhc for putting this code up. I can never remember it and always load this page when I need to make a backup.

    Do you have any good ways to just get one file out of the tar file?

  5. March 17th, 2009 at 08:46 | #5


    tar -cvfz mytarfile.tgz mydir/

    tar: mytarfile.tgz: Cannot stat: No such file or directory
    tar: Error exit delayed from previous errors

    so use it without dash in the beginning of the command as in:

    tar cvfz mytarfile.tgz mydir/

    and it works beautifully.

  6. anon
    July 27th, 2011 at 04:38 | #6

    I freaking HATE it when people post “solutions” that don’t freakin’ work. Take the extra one minute to test your little one-liner before publishing it to the world.

  7. anon
    August 12th, 2011 at 16:35 | #7

    @anon use it as a learning opportunity…it craps out because -f expects a file and the next argument you put was z
    tar -cvzf mytarfile.tgz mydir/ would work