Tar – Creating Archive Without Including Parent Directory

tar

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.

Best Answer

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.
Related Question