creating a tar archive without including parent directory

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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'

Method 3

First, go to the working directory,

cd /your/working/directory/

Then use magic * 🙂

tar -cvf temp.tar *

Method 4

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 ./

Method 5

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.

Method 6

The traditional and portable way to do it is with:

(cd /home/username/dir1/dir2 && tar cf - selecteddir | gzip) > temp.tar.gz

(or lzop/lz4/xz your preferred compressor; or you could skip compression altogether if you didn’t care about saving space).

To uncompress into that same directory, you just reverse the pipeline:

(cd /home/username/dir1/dir2 && gzip -d | tar xpf -) < temp.tar.gz

Method 7

If you want to tar a bunch of files without any parent folder, you can cd into the folder and then tar . (indicating the current directory) while specifying ../yourfiles.tar as the output file. For example, if you want to tar all the files in the yourfiles folder, then cd to the parent folder of yourfiles and type run this:

cd yourfiles && tar -cf ../yourfiles.tar . && cd ../

The parent of yourfiles will now also contain yourfiles.tar.

Note that when you extract yourfiles.tar with tar -xf yourfiles.tar it will extract all the files into the current directory, rather than creating a folder. However, if you right-click and “Extract here” in Ubuntu, it will create a folder called yourfiles to put the files in (“incorrect” behavior, but better UX).


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x