4

I have a command which will create a new .tar.gz file from the existing one,

 sudo tar -zcvf Existing.tar.gz New.tar.gz

this command will create a new New.tar.gz file from the existing Existing.tar.gz file.

Can anyone tell me, is there any way to rename the exiting file without creating the new one?

Thanks.

Madhu
  • 2,643
  • 6
  • 19
  • 34
  • 2
    What about `mv Existing.tar.gz New.tar.gz` ? – arkascha Jun 15 '17 at 07:54
  • 2
    And, btw, that command you gave does _not_ unpack anything... It will try to create an archive `Existing.tar.gz` and place the file `New.tar.gz` in it... Which _certainly_ is not what you want. – arkascha Jun 15 '17 at 07:55
  • @arkascha this worked, can u write it as the answer, thank you. – Madhu Jun 15 '17 at 07:58
  • @arkascha Hi, in my scenario i just wanted to rename the existing file. – Madhu Jun 15 '17 at 08:00
  • 2
    Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. Also see [Where do I post questions about Dev Ops?](http://meta.stackexchange.com/q/134306) – jww Jun 15 '17 at 17:42

1 Answers1

5

The easiest is to simply rename ("move") the file:

mv Existing.tar.gz New.tar.gz
arkascha
  • 41,620
  • 7
  • 58
  • 90
  • Thank you. this rename the existing file like how I wanted. – Madhu Jun 15 '17 at 08:01
  • 3
    Sure, you are welcome. And maybe a "pro tip" for you: take a look at the `apropos` utility: it allows you to discover commands you do not already know, so here the `mv` command. Try a `apropos rename` and look through the result list. For each of those commands you can open the manual page with the `man` utility, so here a `man mv` to find out about the commands usage and details... – arkascha Jun 15 '17 at 08:04
  • btw, renaming suffices only because gzip ignores by default the existing metadata about the original file name: https://superuser.com/questions/859785/is-gzip-supposed-to-honor-original-filename-during-decompress – Felipe G. Nievinski Mar 18 '23 at 06:32
  • @FelipeG.Nievinski Not sure what you are trying to say with that comment ... The `mv` command used to move or rename a file in a unixoid system is completely independent from the contents of the file. It operates on a different lavel. So no, renaming the file that way works reliably, regardless of what the `gzip` algorithm or utility might do or ignore. It is not involved here. – arkascha Mar 19 '23 at 12:13
  • @arkascha the proposed solution doesn't work when using gunzip with the option -N or --name (see the link provided for details): `gunzip -N New.tar.gz` will create Existing.tar. – Felipe G. Nievinski Mar 22 '23 at 02:06
  • @FelipeG.Nievinski Sure, that is correct, since that is what is packed into the container. Renaming the container file certainly does not change the content of said contained. Looks like we have a different understanding of what the OP actually asked. – arkascha Mar 22 '23 at 08:19
  • @arkascha your answer works in the OP's environment (Ubuntu with default options), I was just alerting users of other gzip/gunzip implementations. – Felipe G. Nievinski Mar 23 '23 at 12:42
  • @FelipeG.Nievinski Nope, we are talking about two different things. This answer just renames the compressed file. You are talking about the file _content_ , not the file _name_ . Two different things. – arkascha Mar 23 '23 at 14:38
  • @arkascha it's not me, it's the OP who explicitly asked about the "_contents_" -- they were not asking how to rename an arbitrary file, they asked about a tar.gz, which is meant to be uncompressed eventually. – Felipe G. Nievinski Mar 24 '23 at 15:28