Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a folder structure like so:

file1
file2
file3
file4
folder1
folder2

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

share|improve this question
add comment

3 Answers 3

up vote 3 down vote accepted

tar has an --exclude switch.

share|improve this answer
add comment

You can use --exclude=pattern option which prevents any file or member whose name matches the wildcard (pattern) from being operated on. For example, to create an archive with all the contents of the directory src' except for files whose names end in .exe', use the command `tar -cf src.tar --exclude='*.exe' src'.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.