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.


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.
| improve this answer | |
  • 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
  • 9
    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
  • 1
    Doesn't work: tar: selecteddir: Cannot stat: No such file or directory – tribbloid Mar 25 '18 at 2:59

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'
| improve this answer | |
  • 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
  • 1
    I know it's an old post but just in case: using a different delimiter may be easier than escaping, e.g. --transform='s:/home/username/dir1/dir2/::g' – David Z Jul 27 at 6:14

First, go to the working directory,

cd /your/working/directory/ 

Then use magic * :-)

tar -cvf temp.tar *
| improve this answer | |
  • 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
  • You can use * and .* to get both regular and hidden files. You can also use --exclude thearchive.tar to avoid archiving some files – Kiruahxh Feb 12 at 14:03

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 ./
| improve this answer | |
  • 13
    But then your temp.tar archive contains . as a root folder. – cgrim Oct 2 '18 at 8:46

To avoid that dot dir, you can strip it after the fact:

tar czC /path/to/dir . --transform='s,^\./,,' >| foo.tgz

This will create an archive with only the contents of /path/to/dir.

| improve this answer | |

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.