Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to view tar file content?

Status
Not open for further replies.

suhaimi

Technical User
Aug 3, 2001
71
US
Hi all,
I have thousands of txt files and tar-ed them.
How do I count the total lines of each file or all files without having to untar them to temp directory?

Thanks,
Suhaimi
 
GNU tar allows

Code:
tar Oxf t1.tar | wc -l

"Oxf" means extract tar file to stdout
where it is then piped to 'wc' to count the lines
 
Hi,
Let me rephrase my question. I do not wish to know how many files inside the tar file. What I want is the number of lines IN EACH text files without doing untar.

Rgds,
Suhaimi
 
If you have GNU tar then...

for file in $(tar tf archive.tar)
do
tar Oxf archive.tar $file | wc -l
done

Otherwise tar tvf will give file size in bytes (not lines).

 
I'm sorry. I don't have GNU tar. Is there an alternative???

Thanks,
Suhaimi
 
> What I want is the number of lines IN EACH text files without doing untar.
You can't.

Every answer so far relies on untarring to stdout, and using 'wc' to count the lines.

If your tar doesn't have a 'stdout' mode of some sort (go read the manual) then you're stuck - you'll have to untar to a temp directory first (why is this a bad thing?).

Alternatively, get a verbose list
Code:
tar tvf file.tar
and divide each file size by say 50 to get an approximate count of the number of lines in each file.
 
Tell you what. I have 20,000 plus txt files inside the tar.

Rgds,
Suhaimi
 
How about updating your process to count the lines in all the files and write a 'catalog' file into the tar file at the point you create the tar file. Then later, when you need this information, you can just go look at the catalog file inside the tarfile.

> I have 20,000 plus txt files inside the tar.
Are you saying that you no longer have the file system space to untar the files?

Have you actually tried to untar them and do your line count?

What is the real reason for "without having to untar them to temp directory". Is it lack of space, or some idea that you have that it will perform badly?
 
Yes, I don't have much space in my machine to hold those files. Space is the constraint. Don't tell me I need to extract a file at a time. It is the last resort I could think of.

Rgds
 
Well then you have three choices
1. read your manual for your local tar command, to see if it has a 'output to stdout' mode. If it doesn't, then step 2

2. Ask your system administrator to load GNU TAR onto your system, so that you can use the 'output to stdout' mode.

3. If all else fails, use
Code:
for file in `tar tf archive.tar`
do
   tar xf archive.tar $file
   wc -l $file
   rm $file
done
Which is Ygor's answer, but extracting each one to a file, then removing it (to save space)
 
Thanks guys,
I'll ask my admin to load GNU tar.

Rgds,
Suhaimi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top