Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I am executing a tar command to compress files which are present in another directory.

I executed following command:

tar -czf /backupmnt/abc.tar.gz -C /backupmnt/statusService/ *

I want to create abc.tar.gz file in /backupmnt which should include all files in /backupmnt/statusService/ directory, but I am getting below error:

tar: components: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors

components is present in current directory from where I am executing the command. Below are the contents of /backupmnt/statusService

SS-01:/ # ls /backupmnt/statusService
MJ.netact.xml.tar.gz      gmoTemp_fm.tar.gz  mr.properties.tar.gz  probe.properties.tar.gz  relay_logs.tar.gz  ss_logs.tar.gz  tomcat_conf.tar.gz
esymac_config.txt.tar.gz  gmoTemp_pm.tar.gz  o2ml.tar.gz           probes_logs.tar.gz       ss_conf.tar.gz     ss_pm.tar.gz    tomcat_logs.tar.gz

I am not able to get where I am wrong.

share|improve this question

3 Answers 3

up vote 6 down vote accepted

* is expanded by the shell before tar gets executed. So, making tar change the directory invalidates the arguments that * expanded into. You can simply tell tar to compress the directory instead:

tar -czf /backupmnt/abc.tar.gz -C /backupmnt/statusService/ .

The . represents the current directory, which will change when tar changes directories. This will result in hidden files (those beginning with .) being included in the archive.

share|improve this answer

Why do not use explicit directory change?

cd /backupmnt/statusService && tar -czf /backupmnt/abc.tar.gz *

Or you can use relative paths:

cd /
tar -czf /backupmnt/abc.tar.gz backupmnt/statusService1/* backupmnt/statusService2/* backupmnt/statusService3/*

Which can be even better solution as you will keep files separated in folders and avoid overwrite of duplicated filenames

share|improve this answer
    
This one I was using in shell script.-C option I am using because I have to add multiple files located at various locations. doing cd may lead to rejection of my script –  pankaj_ar Apr 28 at 7:03
    
On the next add you can use update command instead of create. And you can use relative paths, see my edited answer –  Romeo Ninov Apr 28 at 7:06
    
1 problem I got in this, instead of taking files only, it has taken them with complete directory structure inside tar.gz file –  pankaj_ar Apr 28 at 7:17
    
True, you will get the directory structure inside. And IMHO this will be better (to have overview from where is the file). Of course you can create few different tar files per directory. I use in my practice both ways –  Romeo Ninov Apr 28 at 7:28

-C is not a portable option anyway. It's a GNU option not a Unix one.

tar is no longer specified by the Single Unix Specification anyway as there's so much difference between the implementations. It has pax to create archives portably.

You could do:

pax -ws'|.*//||' /backupmnt/statusService//. /some/other/dir//./*.ext |
  xz > file.tar.xz

(xz/gzip are not specified by POSIX).

That is use //. to mark which part of the paths are to be stripped.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.