I am new to Ubuntu (terminal especially) and I am experimenting with the tar command.

I have created a /test folder containing one single /test.txt file at /var/www/html/test/test.txt.

I would like to tar only the /test folder (and its contents) to /home/jo/backup.

My process so far is as follows:

cd ~ 
tar cvf testBackup.tar /var/www/html/test/ 

This successfully creates a testBackup.tar located at /home/jo/backup. However when I extract the tar using the following command:

tar cvf testBackup.tar /var/www/html/test/

The folder var is visible, and I have to cd to the /test directory (/var/www/html/test) in order to see the contents.

Basically I only want to tar the test folder, not the three before (/var/www/html)

Am I doing something wrong or is this how the command works?

  • 1
    use cd /var/www/html/;tar cvf testBackup.tar ./test;mv testBackup.tar ~/backup/ – LittleByBlue Oct 19 '15 at 14:36
  • thanks @LittleByBlue can you briefly explain what this is doing? Or what I am doing wrong? :) – jonboy Oct 19 '15 at 14:37
  • you need to go to the directory above test. see also: man tar I'll make an answer – LittleByBlue Oct 19 '15 at 14:38

As tar is designed for backups,too

you may specify the least distant path. In your case it is test.

So you can use this chain:

cd /var/www/html;tar cvf testBackup.tar ./test;mv testBackup.tar ~/backup/

cd /var/www/html;tar cvf testBackup.tar ./test;rsync  testBackup.tar ~/backup/;rm testBackup.tar # for cross net compability

which will tar only your test folder and move it to your backup directory.

See also:

man tar
  • this is exactly what I was looking for, thank you for the explanation. – jonboy Oct 19 '15 at 14:42
  • Why do you use the mv command? You could specify the correct target directly... – Byte Commander Oct 19 '15 at 14:43
  • @ByteCommander as it is more extensible (eg using rsync) – LittleByBlue Oct 19 '15 at 14:50

go to the location and run the command

cd /var/www/html
tar cvf /home/jo/backup/testBackup.tar test

Typically I go to the directory first, then execute the tar command. This way, we do the dancing up front and allow easy what-you-want extract in the end:

cd /var/www/html
tar cvf ~/testBackup.tar test

The tar command probably has options that allow the same thing without the dancing, just read the man page (man tar).

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.