Shell command to tar directory excluding certain files/folders

后端 未结 28 2044
春和景丽
春和景丽 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:21

    You can also use one of the "--exclude-tag" options depending on your needs:

    • --exclude-tag=FILE
    • --exclude-tag-all=FILE
    • --exclude-tag-under=FILE

    The folder hosting the specified FILE will be excluded.

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

    I've experienced that, at least with the Cygwin version of tar I'm using ("CYGWIN_NT-5.1 1.7.17(0.262/5/3) 2012-10-19 14:39 i686 Cygwin" on a Windows XP Home Edition SP3 machine), the order of options is important.

    While this construction worked for me:

    tar cfvz target.tgz --exclude='<dir1>' --exclude='<dir2>' target_dir
    

    that one didn't work:

    tar cfvz --exclude='<dir1>' --exclude='<dir2>' target.tgz target_dir
    

    This, while tar --help reveals the following:

    tar [OPTION...] [FILE]
    

    So, the second command should also work, but apparently it doesn't seem to be the case...

    Best rgds,

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

    gnu tar v 1.26 the --exclude needs to come after archive file and backup directory arguments, should have no leading or trailing slashes, and prefers no quotes (single or double). So relative to the PARENT directory to be backed up, it's:

    tar cvfz /path_to/mytar.tgz ./dir_to_backup --exclude=some_path/to_exclude

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

    Success Case: 1) if giving full path to take backup, in exclude also should be used full path.

    tar -zcvf /opt/ABC/BKP_27032020/backup_27032020.tar.gz --exclude='/opt/ABC/csv/' --exclude='/opt/ABC/log/' /opt/ABC

    2) if giving current path to take backup, in exclude also should be used current path only.

    tar -zcvf backup_27032020.tar.gz --exclude='ABC/csv/' --exclude='ABC/log/' ABC

    Failure Case:

    1. if giving currentpath directory to take backup and full path to ignore,then wont work

      tar -zcvf /opt/ABC/BKP_27032020/backup_27032020.tar.gz --exclude='/opt/ABC/csv/' --exclude='/opt/ABC/log/' ABC

    Note: mentioning exclude before/after backup directory is fine.

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

    old question with many answers, but I found that none were quite clear enough for me, so I would like to add my try.

    if you have the following structure

    /home/ftp/mysite/
    

    with following file/folders

    /home/ftp/mysite/file1
    /home/ftp/mysite/file2
    /home/ftp/mysite/file3
    /home/ftp/mysite/folder1
    /home/ftp/mysite/folder2
    /home/ftp/mysite/folder3
    

    so, you want to make a tar file that contain everyting inside /home/ftp/mysite (to move the site to a new server), but file3 is just junk, and everything in folder3 is also not needed, so we will skip those two.

    we use the format

    tar -czvf <name of tar file> <what to tar> <any excludes>
    

    where the c = create, z = zip, and v = verbose (you can see the files as they are entered, usefull to make sure none of the files you exclude are being added). and f= file.

    so, my command would look like this

    cd /home/ftp/
    tar -czvf mysite.tar.gz mysite --exclude='file3' --exclude='folder3'
    

    note the files/folders excluded are relatively to the root of your tar (I have tried full path here relative to / but I can not make that work).

    hope this will help someone (and me next time I google it)

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

    I had no luck getting tar to exclude a 5 Gigabyte subdirectory a few levels deep. In the end, I just used the unix Zip command. It worked a lot easier for me.

    So for this particular example from the original post
    (tar --exclude='./folder' --exclude='./upload/folder2' -zcvf /backup/filename.tgz . )

    The equivalent would be:

    zip -r /backup/filename.zip . -x upload/folder/**\* upload/folder2/**\*

    (NOTE: Here is the post I originally used that helped me https://superuser.com/questions/312301/unix-zip-directory-but-excluded-specific-subdirectories-and-everything-within-t)

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