Excluding directory when creating a .tar.gz file

240,655

Solution 1

Try removing the last / at the end of the directory path to exclude

tar -pczf MyBackup.tar.gz --exclude="/home/user/public_html/tmp" /home/user/public_html/

Be aware that the exclude argument:

1- Should be used with a =, like this: --exclude=PATTERN

2- Expects a pattern (as the user Don Dilanga pointed out), not a directory, though a directory will work well as a pattern if it's long enough to not match any single files.

3- Has to be placed before the source directory. (as pointed out by kghbln)

Solution 2

Try moving the --exclude to before the include.

tar -pczf MyBackup.tar.gz --exclude "/home/user/public_html/tmp/" /home/user/public_html/ 

Solution 3

The correct command for exclude directory from compression is :

tar --exclude='./folder' --exclude='./upload/folder2' -zcvf backup.tar.gz backup/

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

and you can check the contents of the tar.gz file without unzipping :

tar -tf backup.tar.gz

Solution 4

Yes, remove the trailing / and (at least in ubuntu 11.04) all the paths given must be relative or full path. You can't mix absolute and relative paths in the same command.

sudo tar -czvf 2011.10.24.tar.gz ./start-directory --exclude "home/user/start-directory/logs"

will not exclude logs directory but

sudo tar -czvf 2011.10.24.tar.gz ./start-directory --exclude "./start-directory/logs"

will work

Solution 5

This worked for me:

tar -zcvf target.tar.gz target/ --exclude="target/backups" --exclude="target/cache"
Share:
240,655
suresh
Author by

suresh

Updated on April 15, 2020

Comments

  • suresh
    suresh about 4 years

    I have a /public_html/ folder, in that folder there's a /tmp/ folder that has like 70gb of files I don't really need.

    Now I am trying to create a .tar.gz of /public_html/ excluding /tmp/

    This is the command I ran:

    tar -pczf MyBackup.tar.gz /home/user/public_html/ --exclude "/home/user/public_html/tmp/" 
    

    The tar is still being created, and by doing an ls -sh I can see that MyBackup.tar.gz already has about 30gb, and I know for sure that /public_html/ without /tmp/ doesn't have more than 1GB of files.

    What did I do wrong?

  • jruzafa
    jruzafa about 9 years
    Work for me when i remove absolute path in exclude argument.
  • Dmitri DB
    Dmitri DB about 9 years
    The tip about mixing absolute and relative paths was key for me here
  • Dr.jacky
    Dr.jacky over 7 years
    To exclude whole folder and its content: tar -pczvf MyBackup.tar.gz /home/user/public_html/ --exclude "/home/user/public_html/tmp/*"
  • James
    James over 7 years
    Thanks, this worked for me on an old Ubuntu 14.04 LTS box.
  • Ilia Hadzhiev
    Ilia Hadzhiev about 7 years
    I needed to do this on OS X as well.
  • prayagupa
    prayagupa almost 6 years
    I get tar: Error exit delayed from previous errors. in macos
  • SenG
    SenG over 5 years
    Getting an error tar: Removing leading `/' from member names
  • Black
    Black over 5 years
    " is required if the folder has space in the name
  • Black
    Black over 5 years
    Does not work, I had to use --exclude="/home/user/public_html/tmp instead of --exclude "/home/user/public_html/tmp
  • kghbln
    kghbln about 5 years
    Indeed, exclude needs to be specified first
  • kghbln
    kghbln about 5 years
    Disregard my previous comment which I cannot longer edit: exclude needs to be specified first as stackoverflow.com/users/3904223/oussaka notes. At least that's the only thing that worked for me
  • Don Dilanga
    Don Dilanga about 5 years
    this answer is wrong. tar doesn't even have --exclude "" command. it should be --exclude=PATTERN exclude files, given as a PATTERN
  • scegg
    scegg over 4 years
    --exclude parameters should be placed BEFORE any sources.
  • Festus Ngor
    Festus Ngor over 3 years
    Thanks... This worked for me on Ubuntu 18.04 LTS
  • Dziki_Jam
    Dziki_Jam over 2 years
    Worked for me after removing trailing slashes: tar -pczf MyBackup.tar.gz --exclude "/home/user/public_html/tmp" /home/user/public_html
  • WiiLF
    WiiLF about 2 years
    Works perfectly - FreeBSD 13