To create a tar archive of an entire directory including all files and sub-directories:
tar -cvf mytarfile.tar mydir/
tar -cvfz mytarfile.tgz mydir/ will also compress the directory (gzip)
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
The problem with the zip format is that it does not store file permissions.
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?
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.
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.
@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