Tar Command

hi folks,

how to using tar with exclude directory and compress it using tar.Z

i only know how to exclude dir only with this command below:

tar -cvf /varios/restore/test.tar -X excludefile.txt /jfma/test1/

how to compress it using 1 command?

Thanx

It always helps to consult the man pages (although I have to admit tar 's is quite complex).
For compressing, just add the z option.
For excluding directories, add their names to the exclude file.

tar cvfX /varios/restore/test.tar excludefile.txt /jfma/test1/
compress /varios/restore/test.tar

The f and X options require the following two arguments.

The exclude file must absolutely match e.g. must be /jfma/test1//path/to/file; it is less confusing to omit the trailing slash, i.e. have /jfma/test1 and the exclude file /jfma/test1/path/to/file
I am talking of a standard tar - GNU tar has some default magic to strip leading and trailing slashes.

All in one go, tar to stdout and pipe this to compress:

tar cvfX - excludefile.txt /jfma/test1 | compress > /varios/restore/test.tar.Z
1 Like

There are variations depending on your OS, which you haven't told us. What OS flavour and version are you using? There are lots to choose from, AIX, HPUX, Solaris, OEL., CentOS, Fedora, RedHat, Suse..........etc.

Some will accept the -z flag as part of the tar command, some will not and you have to pass the output to a compression program afterwards, but the default compression programs can vary depending on your OS too, e.g. compress, gzip, bzip, etc.

If none of the above posts have given you an answer, please post your OS and version and we can find the appropriate process for you.

Kind regards,
Robin

4 Likes

for the compressing, is it similiar with

tar -cvf - /jfma/test1 | compress -c > test.tar.Z

Regards

------ Post updated at 11:42 PM ------

i am using AIX 6100-06-05-1115

For AIX, MadeInGermany has the correct format because compress is the default tool available there and the -z for tar is not an option:-

  • Use a - for the output filename to send it to STDOUT
  • Pipe the STDOUT into STDIN for a compress redirected to the file you want to create.

If you want to exclude files/directories then you can still use the -X as in your original post and end up with:-

tar -cvf - -X excludefile.txt /jfma/test1/ | compress > /varios/restore/test.tar

Kind regards,
Robin

1 Like

Thanx for your reply....it works :slight_smile: