I have a tar file and when I list the files from tar, I can see them as follow:

-rw-rw-r-- root/ccs       3207 2017-02-15 18:43 HMS/PRE.UPG.APPROGS/AP.CLM.NEW
-rw-rw-r-- root/ccs        210 2017-02-15 18:43 HMS/PRE.UPG.APPROGS/AP.R.STUB_1
-rw-r--r-- ccs/ccs         128 2017-05-05 19:01 HMS/BP/_KI.TEST
-rw-rw-r-- ccs/ccs        2683 2017-05-05 19:01 HMS/BP/GET.TOKEN
and etc.

I would like all files and folders to be restored and overwrite (including sub-folders) /usr/CCS/HMS.

But when I ran the command below:

tar xvfz /usr/CCS/backup/hms1_24jan2018.tar.gz -C / HMS*

Instead of overwriting /usr/CCS/HMS and all sub-folders, the above command is restoring on the root as /HMS.

Can I please get some guidance, I would like the untar to overwrite all files and folders under /usr/CCS/HMS from the tar file.

  • Shouldn't you be using -C /usr/CSS ? – Raman Sailopal Jan 30 '18 at 13:57

It seems to me that you should be able to do

tar xvzf /usr/CCS/backup/hms1_24jan2018.tar.gz -C /usr/CCS

or, alternatively

cd /usr/CCS
tar xvzf /usr/CCS/backup/hms1_24jan2018.tar.gz

When you use -C /, the files will be unpacked into the root directory. Since the path in the archive starts with HMS/, this will create a directory called HMS in the root of the filesystem hierarchy.

If you want to extract all files (as per your question), you should not specify filenames to extract (you have HMS* on the command line).

Note also that existing files under /usr/CCS/HMS that are not included in the archive will be left in place (unmodified). tar will not replace the full contents of a directory just because it happens to unpack a few files in it.

  • Thanks Kusalananda, let me run the tar xvzf /usr/CCS/backup/hms1_24jan2018.tar.gz -C /usr/CSS, will provide update soon. – Muhammad Jan 30 '18 at 14:22
  • I tried and got this error: [root@hms1 backup]# tar xvzf /usr/CCS/backup/hms1_24jan2018.tar.gz -C /usr/CSS tar: /usr/CSS: Cannot open: No such file or directory tar: Error is not recoverable: exiting now. Please note that /usr/CCS folder exists, but the system in the command did not recognize /usr/CCS – Muhammad Jan 30 '18 at 14:47
  • @Muhammad I had written CSS in my answer rather than CCS. Sorry, now corrected. – Kusalananda Jan 30 '18 at 14:51
  • Perfect, now running as advised, will provide update once finished. – Muhammad Jan 30 '18 at 15:02
  • Thank you Kusalananda, your solution worked perfectly. Thank you so much for everyone, help from this forum is amazing. God bless you all. We can mark this answered. – Muhammad Jan 30 '18 at 15:27

It is creating folder in root and overriding it as you have used -C / HMS*. Change it to -C /usr/CCS/HMS. It will override the required directory

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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