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.
tar
add a comment |
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.
tar
add a comment |
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.
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.
tar
tar
asked Nov 17 '14 at 2:05
Sujit MaharjanSujit Maharjan
435159
435159
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
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 usedirname
to extract everything until "selecteddir" andbasename
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
2
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
add a comment |
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
add a comment |
First, go to the working directory,
cd /your/working/directory/
Then use magic *
:-)
tar -cvf temp.tar *
wow! thanks it was really amazing.
– ViaSat
Mar 23 '18 at 0:29
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
add a comment |
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 ./
1
But then your temp.tar archive contains.
as a root folder.
– cgrim
Oct 2 '18 at 8:46
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f168357%2fcreating-a-tar-archive-without-including-parent-directory%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 usedirname
to extract everything until "selecteddir" andbasename
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
2
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
add a comment |
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 usedirname
to extract everything until "selecteddir" andbasename
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
2
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
add a comment |
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.
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.
edited Nov 17 '14 at 2:44
answered Nov 17 '14 at 2:38
mkcmkc
5,81342742
5,81342742
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 usedirname
to extract everything until "selecteddir" andbasename
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
2
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
add a comment |
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 usedirname
to extract everything until "selecteddir" andbasename
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
2
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
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
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
@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
2
Worth adding that this is valid for both GNU and BSD Tar.
– Luke Exton
Jul 5 '16 at 23:11
Worth adding that this is valid for both GNU and BSD Tar.
– Luke Exton
Jul 5 '16 at 23:11
2
2
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
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
Doesn't work: tar: selecteddir: Cannot stat: No such file or directory
– tribbloid
Mar 25 '18 at 2:59
add a comment |
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
add a comment |
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
add a comment |
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'
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'
answered Nov 17 '14 at 2:27
ryanmjacobsryanmjacobs
29127
29127
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
add a comment |
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
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
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
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
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
add a comment |
First, go to the working directory,
cd /your/working/directory/
Then use magic *
:-)
tar -cvf temp.tar *
wow! thanks it was really amazing.
– ViaSat
Mar 23 '18 at 0:29
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
add a comment |
First, go to the working directory,
cd /your/working/directory/
Then use magic *
:-)
tar -cvf temp.tar *
wow! thanks it was really amazing.
– ViaSat
Mar 23 '18 at 0:29
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
add a comment |
First, go to the working directory,
cd /your/working/directory/
Then use magic *
:-)
tar -cvf temp.tar *
First, go to the working directory,
cd /your/working/directory/
Then use magic *
:-)
tar -cvf temp.tar *
answered Feb 3 '18 at 20:45
YasYas
15111
15111
wow! thanks it was really amazing.
– ViaSat
Mar 23 '18 at 0:29
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
add a comment |
wow! thanks it was really amazing.
– ViaSat
Mar 23 '18 at 0:29
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
wow! thanks it was really amazing.
– ViaSat
Mar 23 '18 at 0:29
wow! thanks it was really amazing.
– ViaSat
Mar 23 '18 at 0:29
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
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
add a comment |
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 ./
1
But then your temp.tar archive contains.
as a root folder.
– cgrim
Oct 2 '18 at 8:46
add a comment |
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 ./
1
But then your temp.tar archive contains.
as a root folder.
– cgrim
Oct 2 '18 at 8:46
add a comment |
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 ./
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 ./
answered Apr 18 '18 at 10:40
Guan YANGGuan YANG
191
191
1
But then your temp.tar archive contains.
as a root folder.
– cgrim
Oct 2 '18 at 8:46
add a comment |
1
But then your temp.tar archive contains.
as a root folder.
– cgrim
Oct 2 '18 at 8:46
1
1
But then your temp.tar archive contains
.
as a root folder.– cgrim
Oct 2 '18 at 8:46
But then your temp.tar archive contains
.
as a root folder.– cgrim
Oct 2 '18 at 8:46
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f168357%2fcreating-a-tar-archive-without-including-parent-directory%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown