Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to retrieve a .gz file from a directory based on original file name. Please let me know how to do this.

In the below example, I know only original file name (02.txt), and I need to retrieve the file abc02.gz from that directory.

GZ File Name Original File Name  
abc01.gz     01.txt  
abc02.gz     02.txt  
abc03.gz     03.txt   
share|improve this question
Please edit your question. Do you just want to convert filename '02.txt' to 'abc02.gz', or do you want to search for the .gz file that contains 02.txt? And what is that line with all the file names supposed to mean? – Jan Doggen Aug 27 '13 at 7:01
Hi Jan Doggen, I want to search for the .gz file that contains 02.txt. – Praveen Aug 27 '13 at 7:39
up vote 1 down vote accepted

I assume you mean .tar.gz and .tgz files and not .gz files, as .gz files are normally just single files with the same filename but with .gz appended.

Here's one way of searching for all .tgz and .tar.gz files that contains files named "02.txt":

for f in *.tgz *.tar.gz; do tar ztf "$f" | grep -q '/02.txt$' && echo "$f"; done
share|improve this answer
Thanks Alexander, I meant .gz file only. I slightly modified your solution and achieve my requirement. Once again Thanks for your help. for f in *.gz; do gunzip -Nl "$f" | grep 02.txt && echo "$f"; done – Praveen Aug 28 '13 at 1:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .