Tar directory contents without parent and exclude subdirectories/files

9,887

Solution 1

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

Solution 2

How about

cd /
tar cvf /tmp/test.tar --exclude="./root/test/dir[34]" ./root/test

Solution 3

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:
9,887

Related videos on Youtube

Joe
Author by

Joe

SOreadytohelp

Updated on September 18, 2022

Comments

  • Joe
    Joe over 1 year

    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.

  • user237419
    user237419 about 13 years
    like it but how about not finding the right patterns for a big exclude list?
  • Joe
    Joe about 13 years
    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
    Joe about 13 years
    Ah great. I'll combine this with MadHatter's method.
  • user237419
    user237419 about 13 years
    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/
  • Joe
    Joe about 13 years
    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
    Joe about 13 years
    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.
  • user237419
    user237419 about 13 years
    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 ;)
  • Joe
    Joe about 13 years
    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.
  • user237419
    user237419 about 13 years
    ok, then for variable lists of files to tar you probably want to use -T or -X tar options (exclusively ofcourse), depeding what list is easier for you to generate externally (files to backup or files NOT to backup)