127

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?

7ochem
  • 2,035
  • 1
  • 29
  • 37
suresh
  • 1,273
  • 2
  • 9
  • 4
  • 3
    Possible duplicate of [Shell command to tar directory excluding certain files/folders](http://stackoverflow.com/questions/984204/shell-command-to-tar-directory-excluding-certain-files-folders) – Organic Advocate Aug 26 '16 at 18:20
  • the `-p` option has no meaning in terms of the create function, right!? – Martin Schneider Feb 11 '19 at 13:50

10 Answers10

217

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

tar -pczf MyBackup.tar.gz /home/user/public_html/ --exclude "/home/user/public_html/tmp" 
Victor Parmar
  • 5,351
  • 5
  • 29
  • 35
  • 8
    To exclude whole folder and its content: tar -pczvf MyBackup.tar.gz /home/user/public_html/ --exclude "/home/user/public_html/tmp/*" – Dr.jacky Nov 14 '16 at 06:08
  • I get `tar: Error exit delayed from previous errors.` in macos – prayagupd Jun 28 '18 at 23:32
  • Getting an error tar: Removing leading `/' from member names – SenG Nov 20 '18 at 10:02
  • 2
    Does not work, I had to use `--exclude="/home/user/public_html/tmp` instead of `--exclude "/home/user/public_html/tmp` – Black Dec 05 '18 at 15:01
  • Indeed, exclude needs to be specified first – kghbln Mar 12 '19 at 09:48
  • 3
    Disregard my previous comment which I cannot longer edit: `exclude` needs to be specified first as https://stackoverflow.com/users/3904223/oussaka notes. At least that's the only thing that worked for me – kghbln Mar 12 '19 at 10:01
  • this answer is wrong. tar doesn't even have --exclude "" command. it should be --exclude=PATTERN exclude files, given as a PATTERN – Don Dilanga Apr 15 '19 at 21:06
  • --exclude parameters should be placed BEFORE any sources. – scegg Dec 15 '19 at 06:56
52

Try moving the --exclude to before the include.

tar -pczf MyBackup.tar.gz --exclude "/home/user/public_html/tmp/" /home/user/public_html/ 
Tamil Selvan C
  • 18,342
  • 12
  • 44
  • 63
Martin Algesten
  • 11,444
  • 2
  • 46
  • 74
19

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

Baz
  • 34,567
  • 11
  • 66
  • 88
user
  • 199
  • 1
  • 2
18

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
oussaka
  • 770
  • 7
  • 11
4

You can also exclude more than one using only one --exclude. Like this example:

tar -pczf MyBackup.tar.gz --exclude={"/home/user/public_html/tmp","/home/user/public_html/data"} /home/user/public_html/

In --exclude= you must finish the directory name without / and must in between MyBackup.tar.gz and /home/user/public_html/

The syntax is:

tar <OPTIONS> <TARBALL_WILL_CREATE> <ARGS> <PATH_TO_COMPRESS>
Stephen Rauch
  • 40,722
  • 30
  • 82
  • 105
LozanoMatheus
  • 194
  • 3
  • 10
4

tar -pczf <target_file.tar.gz> --exclude /path/to/exclude --exclude /another/path/to/exclude/* /path/to/include/ /another/path/to/include/*

Tested in Ubuntu 19.10.

  1. The = after exclude is optional. You can use = instead of space after keyword exclude if you like.
  2. Parameter exclude must be placed before the source.
  3. The difference between use folder name (like the 1st) or the * (like the 2nd) is: the 2nd one will include an empty folder in package but the 1st will not.
scegg
  • 270
  • 2
  • 9
3

Try this

tar -pczvf advancedarts.tar.gz /home/user/public_html/ --exclude /home/user/public_html/tmp
Yogesh
  • 367
  • 3
  • 17
3

The accepted answer did not work for me, running unxutils tar on windows10. Instead, I had to put the files/dirs to archive as the last parameter, like this:

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

Then it worked.

user2081279
  • 563
  • 6
  • 9
2

This worked for me:

tar -zcvf target.tar.gz target/ --exclude="target/backups" --exclude="target/cache"
Black
  • 12,789
  • 26
  • 116
  • 196
-2

The exclude option needs to include the = sign and " are not required.

--exclude=/home/user/public_html/tmp