Shell – tar unix not changing directory

directoryshelltarwildcards

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.

Best Answer

* 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.

Related Question