2

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.

Joe
  • 344
  • 7
  • 23

4 Answers4

3

How about

cd /
tar cvf /tmp/test.tar --exclude="./root/test/dir[34]" ./root/test
MadHatter
  • 78,442
  • 20
  • 178
  • 229
  • like it but how about not finding the right patterns for a big exclude list? – user237419 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
3

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
Joe
  • 344
  • 7
  • 23
2

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

user237419
  • 1,663
  • 8
  • 8
  • 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/ – user237419 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 ;) – user237419 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
  • 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) – user237419 Mar 28 '11 at 10:47
0

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.

jscott
  • 24,204
  • 8
  • 77
  • 99
Jonathan Ross
  • 2,173
  • 11
  • 14