Home » Questions » Computers [ Ask a new question ]

Tar an entire folder EXCEPT a few files and folders?

Tar an entire folder EXCEPT a few files and folders?

I have a folder structure like so:

Asked by: Guest | Views: 191
Total answers/comments: 2
Guest [Entry]

tar has an --exclude switch.
Guest [Entry]

"Without writing each file, how can I tar zip all the files and folders, except file1 and folder1?

Assuming the main folder that contains all the files/folders you want to compress is located at /home/admin/mystuff/ (ie /home/admin/mystuff/file1, /home/admin/mystuff/folder1/, etc), either of the following should work.

If you're currently inside the /home/admin/ folder, you could do this:

tar -cz --exclude mystuff/file1 --exclude mystuff/folder1/ -f my_new_file.tar.gz mystuff

Or, if you're inside the directory you want to compress (in this case, /home/admin/mystuff/), you could do:

tar -cz --exclude file1 --exclude folder1/ -f my_new_file.tar.gz .

Note the dot at the end of the second option.

Also note that I used tar -cz because I prefer .tar.gz files... If you only want a .tar file, you can just change the command to tar -c --exclude [...] (dropping the z from -cz) and then change the output file from my_new_file.tar.gz to my_new_file.tar."