creating a tar archive without including parent directory

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.

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'

First, go to the working directory,

cd /your/working/directory/ 

Then use magic * :-)

tar -cvf temp.tar *

Tags:

Tar