Excluding directory when creating a .tar.gz file

2019-03-07 17:08发布

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?

9条回答
Bombasti
2楼-- · 2019-03-07 18:00

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.

查看更多
虎瘦雄心在
3楼-- · 2019-03-07 18:01

Try this

tar -pczvf advancedarts.tar.gz /home/user/public_html/ --exclude /home/user/public_html/tmp
查看更多
Bombasti
4楼-- · 2019-03-07 18:02

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
查看更多
登录 后发表回答