[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 732
  • Last Modified:

Tar command to exclude full path

I'm looking for the TAR command that wil exclude directories.
I'm using this command to tar a file, but when i tar it, it wil take the whole pad within the rar. e.g. i want all files in the direcotry /home/user tarred to a tar file. but when i tar the file it will tar /home/user + files!
when i untar the tar file it will tar the content to /home/user/+files

my tar command
tar zcf archive.$day.tar.gz   /var/log/compnaam/*.$day

how to do that

thnx in advance
1 Solution

tar zcf archive.$day.tar.gz  -C /var/log/compnaam/      *.$day

It changes to the directory /var/log/compnaam/ before tarring, but it's got a funky expansion of * in a lot  of shells (at least bash).
So if *.$day matches anything in your current dir, that one is used as the argument to tar  (which is bad).

to be 100% safe you would need to do;
(cd /var/log/compnaam/; tar zcf $OLDPWD/archive.$day.tar.gz *.$day)

# this changes to the dir in a "subshell" (...), inside the subsshell you change dir so that $OLDPWD becomes your current dir outside the subshell
You can create a file and use

tar --exclude FILE

in that file, list the things you want excluded...


cd /var/log/compnaam
tar zcf archive.$day.tar.gz   *.$day

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now