简体   繁体   中英

Bash - Extract only the first file in a tarball

I have an LTO tape with a series of tarballs, each one contains ~300 tiff files. I don't know the contents of the tarballs in advance.

I need to extract the first file from each tarball and compare it's checksum against the same file that exists on my computer.

At the moment, after parking on each tarball I run...

tar -b 1024 -tvf /dev/nst0

... to list the contents and then get the first filename. But I'm wasting time waiting for -tvf to finish.

Is there a way to get the name of the first file only?

firstfile="$(tar -b 1024 -tvf /dev/nst0 | while read item ; do [[ "$item" =~ ^- ]] && echo "$item" && break ; done )"

“ head -n 1”是不够的,因为“ tar -tvf”可能以目录开头。

As tars man page is quite long I asume you read it and there's no tar option matching your problem. I have two ideas that work, but are not very elegant hacking:

1) Start tar -tvf in background, save the PID, check the output until it is at least one line and kill the tar process.

2) This one is very short, but kind of a dirty solution: Since tarballs can be concatenated, you can also head the tarball itself instead of the output. The tar -tv of the truncated tarball gives you the names of the files in the first lines of the binary plus an error/abort warning. To be sure to get only one filename another head -n1 of the ouput can be appended.

head /dev/nst0 | tar -tv 2> /dev/null

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM