Shell command to tar directory excluding certain files/folders

后端 未结 28 2042
春和景丽
春和景丽 2020-11-27 08:31

Is there a simple shell command/script that supports excluding certain files/folders from being archived?

I have a directory that need to be archived with a sub dire

相关标签:
28条回答
  • 2020-11-27 09:12

    For those who have issues with it, some versions of tar would only work properly without the './' in the exclude value.

    Tar --version
    

    tar (GNU tar) 1.27.1

    Command syntax that work:

    tar -czvf ../allfiles-butsome.tar.gz * --exclude=acme/foo
    

    These will not work:

    $ tar -czvf ../allfiles-butsome.tar.gz * --exclude=./acme/foo
    $ tar -czvf ../allfiles-butsome.tar.gz * --exclude='./acme/foo'
    $ tar --exclude=./acme/foo -czvf ../allfiles-butsome.tar.gz *
    $ tar --exclude='./acme/foo' -czvf ../allfiles-butsome.tar.gz *
    $ tar -czvf ../allfiles-butsome.tar.gz * --exclude=/full/path/acme/foo
    $ tar -czvf ../allfiles-butsome.tar.gz * --exclude='/full/path/acme/foo'
    $ tar --exclude=/full/path/acme/foo -czvf ../allfiles-butsome.tar.gz *
    $ tar --exclude='/full/path/acme/foo' -czvf ../allfiles-butsome.tar.gz *
    
    0 讨论(0)
  • 2020-11-27 09:12

    Use the find command in conjunction with the tar append (-r) option. This way you can add files to an existing tar in a single step, instead of a two pass solution (create list of files, create tar).

    find /dir/dir -prune ... -o etc etc.... -exec tar rvf ~/tarfile.tar {} \;
    
    0 讨论(0)
  • 2020-11-27 09:14

    You can use cpio(1) to create tar files. cpio takes the files to archive on stdin, so if you've already figured out the find command you want to use to select the files the archive, pipe it into cpio to create the tar file:

    find ... | cpio -o -H ustar | gzip -c > archive.tar.gz
    
    0 讨论(0)
  • 2020-11-27 09:16

    After reading all this good answers for different versions and having solved the problem for myself, I think there are very small details that are very important, and rare to GNU/Linux general use, that aren't stressed enough and deserves more than comments.

    So I'm not going to try to answer the question for every case, but instead, try to register where to look when things doesn't work.

    IT IS VERY IMPORTANT TO NOTICE:

    1. THE ORDER OF THE OPTIONS MATTER: it is not the same put the --exclude before than after the file option and directories to backup. This is unexpected at least to me, because in my experience, in GNU/Linux commands, usually the order of the options doesn't matter.
    2. Different tar versions expects this options in different order: for instance, @Andrew's answer indicates that in GNU tar v 1.26 and 1.28 the excludes comes last, whereas in my case, with GNU tar 1.29, it's the other way.
    3. THE TRAILING SLASHES MATTER: at least in GNU tar 1.29, it shouldn't be any.

    In my case, for GNU tar 1.29 on Debian stretch, the command that worked was

    tar --exclude="/home/user/.config/chromium" --exclude="/home/user/.cache" -cf file.tar  /dir1/ /home/ /dir3/
    

    The quotes didn't matter, it worked with or without them.

    I hope this will be useful to someone.

    0 讨论(0)
  • 2020-11-27 09:16

    Your best bet is to use find with tar, via xargs (to handle the large number of arguments). For example:

    find / -print0 | xargs -0 tar cjf tarfile.tar.bz2
    
    0 讨论(0)
  • 2020-11-27 09:17

    You can have multiple exclude options for tar so

    $ tar --exclude='./folder' --exclude='./upload/folder2' -zcvf /backup/filename.tgz .
    

    etc will work. Make sure to put --exclude before the source and destination items.

    0 讨论(0)
提交回复
热议问题