Is it possible to run a tar command in directory A which creates a tar archive in directory B out of a file...












0















Here's what I'm starting with:



ls "$A"
<current_directory>
ls "$B"
<nothing>
ls "$C"
my_file


Here's what I want to end up with:



tar -vczf <magic> "${B}/my_archive.tar.gz" "${C}/my_file"
ls "$B"
my_archive.tar.gz # contains my_file at root of archive (no directory)
pwd
<still in A>


I've read through the tar man page several times now. My only conclusion is that it would be easier to read if the flags were listed alphabetically instead of grouped by function. ;-| I feel like this problem should be solvable with some incantation involving the -C flag, but that only seems to help with obtaining the file in C. I've also been trying to overcome tar's limitations with various iterations of:



pushd "$B"
tar -vczf <magic> "my_archive.tar.gz" "${C}/my_file"
popd


A last resort approach would be:



pushd "$C"
tar -vczf my_archive.tar.gz my_file
mv my_archive.tar.gz "$B"
popd


This is the only thing that's worked so far. 4 commands though!? There has to be a better way.



Relevant XKCD.










share|improve this question


















  • 2





    Concerning the XKCD: tar --help would need less than 10s and is a completly valid tar command.

    – Eugen Rieck
    Feb 23 at 11:48
















0















Here's what I'm starting with:



ls "$A"
<current_directory>
ls "$B"
<nothing>
ls "$C"
my_file


Here's what I want to end up with:



tar -vczf <magic> "${B}/my_archive.tar.gz" "${C}/my_file"
ls "$B"
my_archive.tar.gz # contains my_file at root of archive (no directory)
pwd
<still in A>


I've read through the tar man page several times now. My only conclusion is that it would be easier to read if the flags were listed alphabetically instead of grouped by function. ;-| I feel like this problem should be solvable with some incantation involving the -C flag, but that only seems to help with obtaining the file in C. I've also been trying to overcome tar's limitations with various iterations of:



pushd "$B"
tar -vczf <magic> "my_archive.tar.gz" "${C}/my_file"
popd


A last resort approach would be:



pushd "$C"
tar -vczf my_archive.tar.gz my_file
mv my_archive.tar.gz "$B"
popd


This is the only thing that's worked so far. 4 commands though!? There has to be a better way.



Relevant XKCD.










share|improve this question


















  • 2





    Concerning the XKCD: tar --help would need less than 10s and is a completly valid tar command.

    – Eugen Rieck
    Feb 23 at 11:48














0












0








0








Here's what I'm starting with:



ls "$A"
<current_directory>
ls "$B"
<nothing>
ls "$C"
my_file


Here's what I want to end up with:



tar -vczf <magic> "${B}/my_archive.tar.gz" "${C}/my_file"
ls "$B"
my_archive.tar.gz # contains my_file at root of archive (no directory)
pwd
<still in A>


I've read through the tar man page several times now. My only conclusion is that it would be easier to read if the flags were listed alphabetically instead of grouped by function. ;-| I feel like this problem should be solvable with some incantation involving the -C flag, but that only seems to help with obtaining the file in C. I've also been trying to overcome tar's limitations with various iterations of:



pushd "$B"
tar -vczf <magic> "my_archive.tar.gz" "${C}/my_file"
popd


A last resort approach would be:



pushd "$C"
tar -vczf my_archive.tar.gz my_file
mv my_archive.tar.gz "$B"
popd


This is the only thing that's worked so far. 4 commands though!? There has to be a better way.



Relevant XKCD.










share|improve this question














Here's what I'm starting with:



ls "$A"
<current_directory>
ls "$B"
<nothing>
ls "$C"
my_file


Here's what I want to end up with:



tar -vczf <magic> "${B}/my_archive.tar.gz" "${C}/my_file"
ls "$B"
my_archive.tar.gz # contains my_file at root of archive (no directory)
pwd
<still in A>


I've read through the tar man page several times now. My only conclusion is that it would be easier to read if the flags were listed alphabetically instead of grouped by function. ;-| I feel like this problem should be solvable with some incantation involving the -C flag, but that only seems to help with obtaining the file in C. I've also been trying to overcome tar's limitations with various iterations of:



pushd "$B"
tar -vczf <magic> "my_archive.tar.gz" "${C}/my_file"
popd


A last resort approach would be:



pushd "$C"
tar -vczf my_archive.tar.gz my_file
mv my_archive.tar.gz "$B"
popd


This is the only thing that's worked so far. 4 commands though!? There has to be a better way.



Relevant XKCD.







shell tar






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Feb 23 at 11:34









Alex JohnsonAlex Johnson

1033




1033








  • 2





    Concerning the XKCD: tar --help would need less than 10s and is a completly valid tar command.

    – Eugen Rieck
    Feb 23 at 11:48














  • 2





    Concerning the XKCD: tar --help would need less than 10s and is a completly valid tar command.

    – Eugen Rieck
    Feb 23 at 11:48








2




2





Concerning the XKCD: tar --help would need less than 10s and is a completly valid tar command.

– Eugen Rieck
Feb 23 at 11:48





Concerning the XKCD: tar --help would need less than 10s and is a completly valid tar command.

– Eugen Rieck
Feb 23 at 11:48










1 Answer
1






active

oldest

votes


















1














Assuming $A, $B and $C are absolute paths, something along the lines of



tar -C "$C" -czf "$B/my_archive.tar.gz" "my_file"


should work: the -C parameter translates to "Change to DIR before performing any operations. This option is order-sensitive, i.e. it affects all options that follow."



You would need $(realpath $A) and friends to fix relative paths.






share|improve this answer


























  • Trying this out now. I believe that should be -czf though since I'm creating the archive, right?

    – Alex Johnson
    Feb 23 at 11:48











  • Good catch! c and x are side-by-side on the keyboard ...

    – Eugen Rieck
    Feb 23 at 11:50











  • I'd been trying -C after the other flags, which wasn't working. This does!

    – Alex Johnson
    Feb 23 at 11:54













Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1408743%2fis-it-possible-to-run-a-tar-command-in-directory-a-which-creates-a-tar-archive-i%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Assuming $A, $B and $C are absolute paths, something along the lines of



tar -C "$C" -czf "$B/my_archive.tar.gz" "my_file"


should work: the -C parameter translates to "Change to DIR before performing any operations. This option is order-sensitive, i.e. it affects all options that follow."



You would need $(realpath $A) and friends to fix relative paths.






share|improve this answer


























  • Trying this out now. I believe that should be -czf though since I'm creating the archive, right?

    – Alex Johnson
    Feb 23 at 11:48











  • Good catch! c and x are side-by-side on the keyboard ...

    – Eugen Rieck
    Feb 23 at 11:50











  • I'd been trying -C after the other flags, which wasn't working. This does!

    – Alex Johnson
    Feb 23 at 11:54


















1














Assuming $A, $B and $C are absolute paths, something along the lines of



tar -C "$C" -czf "$B/my_archive.tar.gz" "my_file"


should work: the -C parameter translates to "Change to DIR before performing any operations. This option is order-sensitive, i.e. it affects all options that follow."



You would need $(realpath $A) and friends to fix relative paths.






share|improve this answer


























  • Trying this out now. I believe that should be -czf though since I'm creating the archive, right?

    – Alex Johnson
    Feb 23 at 11:48











  • Good catch! c and x are side-by-side on the keyboard ...

    – Eugen Rieck
    Feb 23 at 11:50











  • I'd been trying -C after the other flags, which wasn't working. This does!

    – Alex Johnson
    Feb 23 at 11:54
















1












1








1







Assuming $A, $B and $C are absolute paths, something along the lines of



tar -C "$C" -czf "$B/my_archive.tar.gz" "my_file"


should work: the -C parameter translates to "Change to DIR before performing any operations. This option is order-sensitive, i.e. it affects all options that follow."



You would need $(realpath $A) and friends to fix relative paths.






share|improve this answer















Assuming $A, $B and $C are absolute paths, something along the lines of



tar -C "$C" -czf "$B/my_archive.tar.gz" "my_file"


should work: the -C parameter translates to "Change to DIR before performing any operations. This option is order-sensitive, i.e. it affects all options that follow."



You would need $(realpath $A) and friends to fix relative paths.







share|improve this answer














share|improve this answer



share|improve this answer








edited Feb 23 at 11:49

























answered Feb 23 at 11:45









Eugen RieckEugen Rieck

11.2k22429




11.2k22429













  • Trying this out now. I believe that should be -czf though since I'm creating the archive, right?

    – Alex Johnson
    Feb 23 at 11:48











  • Good catch! c and x are side-by-side on the keyboard ...

    – Eugen Rieck
    Feb 23 at 11:50











  • I'd been trying -C after the other flags, which wasn't working. This does!

    – Alex Johnson
    Feb 23 at 11:54





















  • Trying this out now. I believe that should be -czf though since I'm creating the archive, right?

    – Alex Johnson
    Feb 23 at 11:48











  • Good catch! c and x are side-by-side on the keyboard ...

    – Eugen Rieck
    Feb 23 at 11:50











  • I'd been trying -C after the other flags, which wasn't working. This does!

    – Alex Johnson
    Feb 23 at 11:54



















Trying this out now. I believe that should be -czf though since I'm creating the archive, right?

– Alex Johnson
Feb 23 at 11:48





Trying this out now. I believe that should be -czf though since I'm creating the archive, right?

– Alex Johnson
Feb 23 at 11:48













Good catch! c and x are side-by-side on the keyboard ...

– Eugen Rieck
Feb 23 at 11:50





Good catch! c and x are side-by-side on the keyboard ...

– Eugen Rieck
Feb 23 at 11:50













I'd been trying -C after the other flags, which wasn't working. This does!

– Alex Johnson
Feb 23 at 11:54







I'd been trying -C after the other flags, which wasn't working. This does!

– Alex Johnson
Feb 23 at 11:54




















draft saved

draft discarded




















































Thanks for contributing an answer to Super User!


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




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1408743%2fis-it-possible-to-run-a-tar-command-in-directory-a-which-creates-a-tar-archive-i%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

How do I know what Microsoft account the skydrive app is syncing to?

When does type information flow backwards in C++?

Grease: Live!