Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Simplified directory structure:

/root/test
/root/test/dir1
/root/test/dir2
/root/test/dir3
/root/test/dir4

I want to tar /root/test so that when I extract the archive I get dir1 and dir2. I want to exclude dir3 and dir4 and possibly other files and subdirectories depending on the specific application of the command.

I realise there is a lot of tar examples and snippets around the net but I cannot seem to get a specific example or combine the ones available online to make this work as above. Any help would be much appreciated.

share|improve this question

4 Answers 4

How about

cd /
tar cvf /tmp/test.tar --exclude="./root/test/dir[34]" ./root/test
share|improve this answer
    
like it but how about not finding the right patterns for a big exclude list? –  sysfault Mar 28 '11 at 10:26
    
I will test this, but as @adirau mentioned - how about when the file/directory names are not so regular. Ones above were just examples, ideally needs to be for any directory/file names. –  Joe Mar 28 '11 at 10:29
    
Using what @Jonathon Ross mentioned I can get this to exclude as desired. However, the contents of the tar become ./root/test/dir1 etc so you end up creating a root dir within whichever folder you extract it rather than just the dirs (dir1, dir2) themselves. –  Joe Mar 28 '11 at 10:34

if you have a limited number of folders you want to tar you can just do

tar -C /root/test -cf arch.tar dir1 dir2



root@pinkpony:~# ls /root/test/
dir1  dir2  dir3
root@pinkpony:~# pwd
/root
root@pinkpony:~# tar -C /root/test -cf arch.tar dir1 dir2
root@pinkpony:~# tar tf arch.tar 
dir1/
dir2/

EDIT (make the code included in comment readable):

if you have complex rules for building the list of directories you want to tar, you may want to start scripting:

root@pinkpony:~# (cd /root/test/ ; ls |grep -E '^dir(1|2)$' | xargs  tar cf /tmp/blah.tar )
root@pinkpony:~# tar tf /tmp/blah.tar 
dir1/
dir2/

you can use whatever you need instead of that silly ls |grep thingie, just an example; xargs is the part i want to point to your attention

share|improve this answer
    
Did cross my mind too. However, I'm trying to automate this for an arbitrary folder so this would involve first having to get a list of the files/folders in it and then excluding manually to construct the command which just seemed a bit bloated. –  Joe Mar 28 '11 at 10:30
    
script it; use grep/sed/awk to build the input for your tar, like this for example: root@pinkpony:~# (cd /root/test/ ; ls |grep -E '^dir(1|2)$' | xargs tar cf /tmp/blah.tar ) root@pinkpony:~# tar tf /tmp/blah.tar dir1/ dir2/ –  sysfault Mar 28 '11 at 10:33
    
It is a possibility. I'm just going to see if there's a neater solution to this using simply the tar command before going down this road. It just seems somewhat long winded for something which I would have thought was easy. –  Joe Mar 28 '11 at 10:38
    
you said it in your first comment: "I'm trying to automate this for an arbitrary folder so this would involve first having to get a list of the files/folders in it" so i safely assumed the logic for selecting the files/folders is arbitrary and should be defined out of tar's space for flexibility sake ;) –  sysfault Mar 28 '11 at 10:41
    
Of course the other issue (my fault for not putting this in the original question) would be that I may want to exclude subsubdirectories such as /root/test1/test12 etc, complicating this even further. –  Joe Mar 28 '11 at 10:43
up vote 2 down vote accepted

I think I've managed to do it by putting together what has been said here with some experimentation.

tar -cvf test.tar --exclude-from=exclude --directory="/root/test" .

Where exclude is a file with:

dir3
dir4
share|improve this answer

FWIW you can also specify a file to list excludes for a longer list of files or directories. It's easier than adjusting a long command line.

Check out mycrpt for a quick, safe encryption method too. Sorry, slightly OT.

share|improve this answer
    
Ah great. I'll combine this with MadHatter's method. –  Joe Mar 28 '11 at 10:31

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.