Untar single file to desired name

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
4
down vote

favorite












When I tar a single file, I can give the tar file a desired name:



$ tar -cvfj A B


I would like to do the same when I untar a single file.



$ tar -xvjf A tmp


(ie. extract A into tmp).



I was trying to do this using mv:



$ tar -xvjf $1 | mv $1 tmp


That didn't work. $1 can be an arbitrary name, but the tar files will always be a single file, no folders (this is guaranteed).







share|improve this question






















  • Are you just using tar for compression? If so, don't do that; just use the compression program directly (bzip2 in your case)
    – Fox
    Sep 2 at 20:27










  • @Fox yes, I am. What difference does it make?
    – Spent Death
    Sep 2 at 20:28






  • 3




    If your local tar implementation does not create a file named j in your first example, it is broken.
    – schily
    Sep 2 at 20:31






  • 1




    Use -C : tar -xvjf $1 -C tmp
    – GAD3R
    Sep 2 at 20:43














up vote
4
down vote

favorite












When I tar a single file, I can give the tar file a desired name:



$ tar -cvfj A B


I would like to do the same when I untar a single file.



$ tar -xvjf A tmp


(ie. extract A into tmp).



I was trying to do this using mv:



$ tar -xvjf $1 | mv $1 tmp


That didn't work. $1 can be an arbitrary name, but the tar files will always be a single file, no folders (this is guaranteed).







share|improve this question






















  • Are you just using tar for compression? If so, don't do that; just use the compression program directly (bzip2 in your case)
    – Fox
    Sep 2 at 20:27










  • @Fox yes, I am. What difference does it make?
    – Spent Death
    Sep 2 at 20:28






  • 3




    If your local tar implementation does not create a file named j in your first example, it is broken.
    – schily
    Sep 2 at 20:31






  • 1




    Use -C : tar -xvjf $1 -C tmp
    – GAD3R
    Sep 2 at 20:43












up vote
4
down vote

favorite









up vote
4
down vote

favorite











When I tar a single file, I can give the tar file a desired name:



$ tar -cvfj A B


I would like to do the same when I untar a single file.



$ tar -xvjf A tmp


(ie. extract A into tmp).



I was trying to do this using mv:



$ tar -xvjf $1 | mv $1 tmp


That didn't work. $1 can be an arbitrary name, but the tar files will always be a single file, no folders (this is guaranteed).







share|improve this question














When I tar a single file, I can give the tar file a desired name:



$ tar -cvfj A B


I would like to do the same when I untar a single file.



$ tar -xvjf A tmp


(ie. extract A into tmp).



I was trying to do this using mv:



$ tar -xvjf $1 | mv $1 tmp


That didn't work. $1 can be an arbitrary name, but the tar files will always be a single file, no folders (this is guaranteed).









share|improve this question













share|improve this question




share|improve this question








edited Sep 3 at 4:00









slm♦

237k65486659




237k65486659










asked Sep 2 at 20:11









Spent Death

254




254











  • Are you just using tar for compression? If so, don't do that; just use the compression program directly (bzip2 in your case)
    – Fox
    Sep 2 at 20:27










  • @Fox yes, I am. What difference does it make?
    – Spent Death
    Sep 2 at 20:28






  • 3




    If your local tar implementation does not create a file named j in your first example, it is broken.
    – schily
    Sep 2 at 20:31






  • 1




    Use -C : tar -xvjf $1 -C tmp
    – GAD3R
    Sep 2 at 20:43
















  • Are you just using tar for compression? If so, don't do that; just use the compression program directly (bzip2 in your case)
    – Fox
    Sep 2 at 20:27










  • @Fox yes, I am. What difference does it make?
    – Spent Death
    Sep 2 at 20:28






  • 3




    If your local tar implementation does not create a file named j in your first example, it is broken.
    – schily
    Sep 2 at 20:31






  • 1




    Use -C : tar -xvjf $1 -C tmp
    – GAD3R
    Sep 2 at 20:43















Are you just using tar for compression? If so, don't do that; just use the compression program directly (bzip2 in your case)
– Fox
Sep 2 at 20:27




Are you just using tar for compression? If so, don't do that; just use the compression program directly (bzip2 in your case)
– Fox
Sep 2 at 20:27












@Fox yes, I am. What difference does it make?
– Spent Death
Sep 2 at 20:28




@Fox yes, I am. What difference does it make?
– Spent Death
Sep 2 at 20:28




3




3




If your local tar implementation does not create a file named j in your first example, it is broken.
– schily
Sep 2 at 20:31




If your local tar implementation does not create a file named j in your first example, it is broken.
– schily
Sep 2 at 20:31




1




1




Use -C : tar -xvjf $1 -C tmp
– GAD3R
Sep 2 at 20:43




Use -C : tar -xvjf $1 -C tmp
– GAD3R
Sep 2 at 20:43










2 Answers
2






active

oldest

votes

















up vote
7
down vote



accepted










Since you seem to be using tar with bzip2 compression as a way of just compressing a single file, you may want to consider using bzip2 directly:



To compress a file (creates filename.bz2, deletes filename):



bzip2 filename


To decompress a file (creates filename, deletes filename.bz2):



bunzip2 filename.bz2


To compress to a specific filename (creates newfilename.bz2, keeps filename):



bzip2 -c filename >newfilename.bz2


To decompress to a specific filename (creates newfilename, keeps filename.bz2):



bunzip2 -c filename.bz2 >newfilename


See also the manual for bzip2 on your system (man bzip2).






share|improve this answer






















  • I like this solution. However, there is a file that has spaces. How can my bash script handle this? ./decomp "blah blah blah.bz2" gets all messed up. When I used bunzip2 directly, it's all good.
    – Spent Death
    Sep 2 at 21:47






  • 1




    @SpentDeath Make sure that you double quote the variable expansion in your script.
    – Kusalananda
    Sep 2 at 21:52










  • what does that mean?
    – Spent Death
    Sep 2 at 21:59






  • 1




    @SpentDeath That means that rather than $1, you'd use "$1" so that your arguments don't get split
    – Fox
    Sep 3 at 0:22

















up vote
7
down vote













Here is one solution:



tar -xjOf my.tar > out


This uses the -O option of tar




-O, --to-stdout: extract files to standard output




and redirects standard output to a file called out. If the archive has more than one file, out will be all of the files in the archive concatenated.



You should probably just be using bzip2 directly, as during compression it will not store a file tree. Instead, it will simply store the data within the file.






share|improve this answer
















  • 3




    Caveat of this approach is you do not preserve metadata like permissions and symlinks.
    – Bob
    Sep 3 at 5:07










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',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f466445%2funtar-single-file-to-desired-name%23new-answer', 'question_page');

);

Post as a guest






























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
7
down vote



accepted










Since you seem to be using tar with bzip2 compression as a way of just compressing a single file, you may want to consider using bzip2 directly:



To compress a file (creates filename.bz2, deletes filename):



bzip2 filename


To decompress a file (creates filename, deletes filename.bz2):



bunzip2 filename.bz2


To compress to a specific filename (creates newfilename.bz2, keeps filename):



bzip2 -c filename >newfilename.bz2


To decompress to a specific filename (creates newfilename, keeps filename.bz2):



bunzip2 -c filename.bz2 >newfilename


See also the manual for bzip2 on your system (man bzip2).






share|improve this answer






















  • I like this solution. However, there is a file that has spaces. How can my bash script handle this? ./decomp "blah blah blah.bz2" gets all messed up. When I used bunzip2 directly, it's all good.
    – Spent Death
    Sep 2 at 21:47






  • 1




    @SpentDeath Make sure that you double quote the variable expansion in your script.
    – Kusalananda
    Sep 2 at 21:52










  • what does that mean?
    – Spent Death
    Sep 2 at 21:59






  • 1




    @SpentDeath That means that rather than $1, you'd use "$1" so that your arguments don't get split
    – Fox
    Sep 3 at 0:22














up vote
7
down vote



accepted










Since you seem to be using tar with bzip2 compression as a way of just compressing a single file, you may want to consider using bzip2 directly:



To compress a file (creates filename.bz2, deletes filename):



bzip2 filename


To decompress a file (creates filename, deletes filename.bz2):



bunzip2 filename.bz2


To compress to a specific filename (creates newfilename.bz2, keeps filename):



bzip2 -c filename >newfilename.bz2


To decompress to a specific filename (creates newfilename, keeps filename.bz2):



bunzip2 -c filename.bz2 >newfilename


See also the manual for bzip2 on your system (man bzip2).






share|improve this answer






















  • I like this solution. However, there is a file that has spaces. How can my bash script handle this? ./decomp "blah blah blah.bz2" gets all messed up. When I used bunzip2 directly, it's all good.
    – Spent Death
    Sep 2 at 21:47






  • 1




    @SpentDeath Make sure that you double quote the variable expansion in your script.
    – Kusalananda
    Sep 2 at 21:52










  • what does that mean?
    – Spent Death
    Sep 2 at 21:59






  • 1




    @SpentDeath That means that rather than $1, you'd use "$1" so that your arguments don't get split
    – Fox
    Sep 3 at 0:22












up vote
7
down vote



accepted







up vote
7
down vote



accepted






Since you seem to be using tar with bzip2 compression as a way of just compressing a single file, you may want to consider using bzip2 directly:



To compress a file (creates filename.bz2, deletes filename):



bzip2 filename


To decompress a file (creates filename, deletes filename.bz2):



bunzip2 filename.bz2


To compress to a specific filename (creates newfilename.bz2, keeps filename):



bzip2 -c filename >newfilename.bz2


To decompress to a specific filename (creates newfilename, keeps filename.bz2):



bunzip2 -c filename.bz2 >newfilename


See also the manual for bzip2 on your system (man bzip2).






share|improve this answer














Since you seem to be using tar with bzip2 compression as a way of just compressing a single file, you may want to consider using bzip2 directly:



To compress a file (creates filename.bz2, deletes filename):



bzip2 filename


To decompress a file (creates filename, deletes filename.bz2):



bunzip2 filename.bz2


To compress to a specific filename (creates newfilename.bz2, keeps filename):



bzip2 -c filename >newfilename.bz2


To decompress to a specific filename (creates newfilename, keeps filename.bz2):



bunzip2 -c filename.bz2 >newfilename


See also the manual for bzip2 on your system (man bzip2).







share|improve this answer














share|improve this answer



share|improve this answer








edited Sep 2 at 21:06

























answered Sep 2 at 20:52









Kusalananda

105k14209326




105k14209326











  • I like this solution. However, there is a file that has spaces. How can my bash script handle this? ./decomp "blah blah blah.bz2" gets all messed up. When I used bunzip2 directly, it's all good.
    – Spent Death
    Sep 2 at 21:47






  • 1




    @SpentDeath Make sure that you double quote the variable expansion in your script.
    – Kusalananda
    Sep 2 at 21:52










  • what does that mean?
    – Spent Death
    Sep 2 at 21:59






  • 1




    @SpentDeath That means that rather than $1, you'd use "$1" so that your arguments don't get split
    – Fox
    Sep 3 at 0:22
















  • I like this solution. However, there is a file that has spaces. How can my bash script handle this? ./decomp "blah blah blah.bz2" gets all messed up. When I used bunzip2 directly, it's all good.
    – Spent Death
    Sep 2 at 21:47






  • 1




    @SpentDeath Make sure that you double quote the variable expansion in your script.
    – Kusalananda
    Sep 2 at 21:52










  • what does that mean?
    – Spent Death
    Sep 2 at 21:59






  • 1




    @SpentDeath That means that rather than $1, you'd use "$1" so that your arguments don't get split
    – Fox
    Sep 3 at 0:22















I like this solution. However, there is a file that has spaces. How can my bash script handle this? ./decomp "blah blah blah.bz2" gets all messed up. When I used bunzip2 directly, it's all good.
– Spent Death
Sep 2 at 21:47




I like this solution. However, there is a file that has spaces. How can my bash script handle this? ./decomp "blah blah blah.bz2" gets all messed up. When I used bunzip2 directly, it's all good.
– Spent Death
Sep 2 at 21:47




1




1




@SpentDeath Make sure that you double quote the variable expansion in your script.
– Kusalananda
Sep 2 at 21:52




@SpentDeath Make sure that you double quote the variable expansion in your script.
– Kusalananda
Sep 2 at 21:52












what does that mean?
– Spent Death
Sep 2 at 21:59




what does that mean?
– Spent Death
Sep 2 at 21:59




1




1




@SpentDeath That means that rather than $1, you'd use "$1" so that your arguments don't get split
– Fox
Sep 3 at 0:22




@SpentDeath That means that rather than $1, you'd use "$1" so that your arguments don't get split
– Fox
Sep 3 at 0:22












up vote
7
down vote













Here is one solution:



tar -xjOf my.tar > out


This uses the -O option of tar




-O, --to-stdout: extract files to standard output




and redirects standard output to a file called out. If the archive has more than one file, out will be all of the files in the archive concatenated.



You should probably just be using bzip2 directly, as during compression it will not store a file tree. Instead, it will simply store the data within the file.






share|improve this answer
















  • 3




    Caveat of this approach is you do not preserve metadata like permissions and symlinks.
    – Bob
    Sep 3 at 5:07














up vote
7
down vote













Here is one solution:



tar -xjOf my.tar > out


This uses the -O option of tar




-O, --to-stdout: extract files to standard output




and redirects standard output to a file called out. If the archive has more than one file, out will be all of the files in the archive concatenated.



You should probably just be using bzip2 directly, as during compression it will not store a file tree. Instead, it will simply store the data within the file.






share|improve this answer
















  • 3




    Caveat of this approach is you do not preserve metadata like permissions and symlinks.
    – Bob
    Sep 3 at 5:07












up vote
7
down vote










up vote
7
down vote









Here is one solution:



tar -xjOf my.tar > out


This uses the -O option of tar




-O, --to-stdout: extract files to standard output




and redirects standard output to a file called out. If the archive has more than one file, out will be all of the files in the archive concatenated.



You should probably just be using bzip2 directly, as during compression it will not store a file tree. Instead, it will simply store the data within the file.






share|improve this answer












Here is one solution:



tar -xjOf my.tar > out


This uses the -O option of tar




-O, --to-stdout: extract files to standard output




and redirects standard output to a file called out. If the archive has more than one file, out will be all of the files in the archive concatenated.



You should probably just be using bzip2 directly, as during compression it will not store a file tree. Instead, it will simply store the data within the file.







share|improve this answer












share|improve this answer



share|improve this answer










answered Sep 2 at 20:41









Cebtenzzre

711




711







  • 3




    Caveat of this approach is you do not preserve metadata like permissions and symlinks.
    – Bob
    Sep 3 at 5:07












  • 3




    Caveat of this approach is you do not preserve metadata like permissions and symlinks.
    – Bob
    Sep 3 at 5:07







3




3




Caveat of this approach is you do not preserve metadata like permissions and symlinks.
– Bob
Sep 3 at 5:07




Caveat of this approach is you do not preserve metadata like permissions and symlinks.
– Bob
Sep 3 at 5:07

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f466445%2funtar-single-file-to-desired-name%23new-answer', 'question_page');

);

Post as a guest













































































Comments

Popular posts from this blog

Long meetings (6-7 hours a day): Being “babysat” by supervisor

Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

Confectionery