Excluding directory when creating a .tar.gz file
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"
suresh
Updated on April 15, 2020Comments
-
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 thatMyBackup.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?
-
Organic Advocate over 7 yearsPossible duplicate of Shell command to tar directory excluding certain files/folders
-
Martin Schneider over 5 yearsthe
-p
option has no meaning in terms of the create function, right!?
-
-
jruzafa about 9 yearsWork for me when i remove absolute path in exclude argument.
-
Dmitri DB about 9 yearsThe tip about mixing absolute and relative paths was key for me here
-
Dr.jacky over 7 yearsTo exclude whole folder and its content: tar -pczvf MyBackup.tar.gz /home/user/public_html/ --exclude "/home/user/public_html/tmp/*"
-
James over 7 yearsThanks, this worked for me on an old Ubuntu 14.04 LTS box.
-
Ilia Hadzhiev about 7 yearsI needed to do this on OS X as well.
-
prayagupa almost 6 yearsI get
tar: Error exit delayed from previous errors.
in macos -
SenG over 5 yearsGetting an error tar: Removing leading `/' from member names
-
Black over 5 years" is required if the folder has space in the name
-
Black over 5 yearsDoes not work, I had to use
--exclude="/home/user/public_html/tmp
instead of--exclude "/home/user/public_html/tmp
-
kghbln about 5 yearsIndeed, exclude needs to be specified first
-
kghbln about 5 yearsDisregard 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 about 5 yearsthis answer is wrong. tar doesn't even have --exclude "" command. it should be --exclude=PATTERN exclude files, given as a PATTERN
-
scegg over 4 years--exclude parameters should be placed BEFORE any sources.
-
Festus Ngor over 3 yearsThanks... This worked for me on Ubuntu 18.04 LTS
-
Dziki_Jam over 2 yearsWorked for me after removing trailing slashes:
tar -pczf MyBackup.tar.gz --exclude "/home/user/public_html/tmp" /home/user/public_html
-
WiiLF about 2 yearsWorks perfectly - FreeBSD 13