2

I am trying to extract one file out of the whole tar.gz and it's not working. Below are the commands i tried to simulate the issue

mkdir test
touch test/version.txt
echo "1.0.2" > test/version.txt 
tar zcvf rootfs.tar -C test .
gzip  -f -9 -n -c rootfs.tar > rootfs.tar.gz
tar xf rootfs.tar.gz version.txt

tar: version.txt: Not found in archive
tar: Exiting with failure status due to previous errors

Can you please provide the reason, there is version.txt file present in the above tar.gz

3
  • You compressed the archive twice. Once with tar's z option, once with gzip. Could this cause the problem? – berndbausch Jun 21 at 7:22
  • Even if i remove the gzip command, i am still getting the error – md.jamal Jun 21 at 7:34
  • 1
    You have to extract ./version.txt, not version.txt (I see that Gilles has added this to his answer as well). By the way, to double-check the archive's content, just use tar tf rootfs.tar.gz. – berndbausch Jun 21 at 7:40
7

rootfs.tar is a tar archive compressed with gzip. rootfs.tar.gz is a tar archive compressed with gzip twice. Tar seems to be confused by the double compression and treats the file as an empty archive instead of reporting an error.

Compressing twice is pointless, so remove this extra gzip step. To avoid confusion, call the compressed archive rootfs.tar.gz instead of rootfs.tar. And since the path you're passing to tar starts with ., you need to pass a path starting with ./ when extracting: tar does not treat ./ as a no-op in paths.

mkdir test
touch test/version.txt
echo "1.0.2" > test/version.txt 
tar zcvf rootfs.tar.gz -C test .
tar xf rootfs.tar.gz ./version.txt

If you want to avoid the ./ prefix in file names, you can use --transform when creating the archive.

tar zcvf rootfs.tar.gz --transform='s!^\./!!' -C test .
tar xf rootfs.tar.gz version.txt
5
  • I am still getting the error with above commands – md.jamal Jun 21 at 7:33
  • @md.jamal You need to be consisted with the file name as well: either ./version.txt both times or version.txt both times. See my edit. – Gilles 'SO- stop being evil' Jun 21 at 7:41
  • Also, shouldn't that be "tar zxf" not "tar xf"? I don't think expecting tar to autodetect a compressed archive is portable. – abligh Jun 21 at 19:29
  • @abligh At least GNU tar and FreeBSD tar recognize compressed archives automatically if the archive is in a regular file (not if it's read from a pipe or other non-seekable input). It's not the case with every tar implementation that recognizes the z flag, for example with BusyBox you need to pass it explicitly, but it is the case with the most prevalent implementations on a machine where you would typically build something called rootfs. – Gilles 'SO- stop being evil' Jun 21 at 19:58
  • Please note that tere are tar implementations that automatically strip leading ./ from path names, wile creating an archive. Also note: star automatically detects compressed archives 25 years, Solaris tar does the same since 11 years and gtar/libarchive seem to do this since approx. the same time. – schily Jun 22 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.