57

I am trying to create a graphical program for my script.

Inside the script I use tar to create a tar archive.

From the graphical program I get the full name of file that I want to create a tar archive.

tar -cvf temp.tar /home/username/dir1/dir2/selecteddir 

My tar archive includes home, username, dir1, dir2 and selecteddir while i want tar to create archive only including selecteddir.

72

You can use the -C option of tar to accomplish this:

tar -C /home/username/dir1/dir2 -cvf temp.tar selecteddir

From the man page of tar:

-C directory
         In c and r mode, this changes the directory before adding the following files.  
         In x mode, change directories after opening the archive but before extracting 
         entries from the archive.
  • do you know how i can split $path into selecteddir and path upto /home/username/dir1/dir2 so I can user -C option – Sujit Maharjan Nov 17 '14 at 3:35
  • @SujitMaharjan you can use dirname to extract everything until "selecteddir" and basename to extract "selecteddir". Try the two commands with the $path as argument. – mkc Nov 17 '14 at 3:42
  • 2
    Worth adding that this is valid for both GNU and BSD Tar. – Luke Exton Jul 5 '16 at 23:11
  • 3
    Note that you can't use globbing with the -C option. tar -C /home/username/dir1/dir2 -cvf temp.tar '*.csv' won't work. – Christian Long Aug 29 '16 at 14:59
  • Doesn't work: tar: selecteddir: Cannot stat: No such file or directory – tribbloid Mar 25 '18 at 2:59
10

There are two methods that you can use to approach this problem.

The first one, in my opinion, is easier. Simply cd into directory directly above the one you want to compress. In this case it would be dir2.

$ cd /home/username/dir1/dir2/
$ tar -cvf temp.tar selecteddir

The second way is to use the option --transform which takes a sed expression and runs it against the files names. Note: you will have to escape / in the sed expression.

$ tar -cvf temp.tar /home/username/dir1/dir2/selecteddir --transform='s/\/home\/username\/dir1\/dir2\///g'
  • Does the first method need a dot (.) on the end? I tried it without and got: tar: Cowardly refusing to create an empty archive. When I have the dot, it annoyingly makes the root contain a directory named .. So, close but not perfect. – JW01 Dec 21 '15 at 12:58
  • 1
    Further to my previous comment. I solved my issue. $ cd /home/username/dir1/dir2/ && tar -cvf temp.tar selecteddir * worked for me. Using asterisk instead of a dot .. Update: Woops - Although I lost the hidden files. – JW01 Dec 21 '15 at 13:10
5

First, go to the working directory,

cd /your/working/directory/ 

Then use magic * :-)

tar -cvf temp.tar *
  • I'm trying to archive hidden directories and if I add them directly (No *) it works, but when using * it only adds the name of the archive to the archive ... – Ole Oct 5 '18 at 10:35
1

Actually, I found a problem using Ketan's answer

tar -C /home/username/dir1/dir2 -cvf temp.tar selecteddir

When you want to just copy all files just in dir2, just all files then I first come out the idea:

tar -C /home/username/dir1/dir2 -cvf temp.tar *

However, when you are not in dir2 directory, it would cause problem since * would tar files in your current diectroy. And I fixed the problem using command below:

 tar -C /home/username/dir1/dir2 -cvf temp.tar ./
  • 4
    But then your temp.tar archive contains . as a root folder. – cgrim Oct 2 '18 at 8:46

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.