check a file is exists in multiple tar file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting check a file is exists in multiple tar file?
# 1  
Old 03-02-2011
check a file is exists in multiple tar file?

I have list of ‘tar' format files in a folder. I want to check a particular file is exists in the tar files. Currently I am using the below command to check the scenario.

Code:
tar -tvf first.tar | grep ‘myfile.txt'

The command able to searched a single ‘tar' file only. But I want to search multiple ‘tar' (*.tar) files. Could please suggest some command line utility to check the multiple tar file.
# 2  
Old 03-02-2011
Example for all tar archives in the current directory (not recursive)

Code:
for f in *.tar; do
    tar -tvf $f | grep 'myfile.txt'
done

Recursive:
Code:
find . -type f -name "*.tar"| xargs -I {} tar -tvf {} | grep 'myfile.txt'

This User Gave Thanks to zaxxon For This Post:
# 3  
Old 03-02-2011
Thanks @ Zaxxon. I able to get the expected result for your solution. And also i tried the below commands that gives the same result.
Code:
ls *.tar | awk '{print "tar -tvf "$1}'| sh | grep 'my_match'

Thanks
# 4  
Old 03-02-2011
Code:
find . -type f -name "*.tar" -exec tar tvf {} \; |grep "myfile.txt"

This User Gave Thanks to rdcwayx For This Post:
# 5  
Old 03-02-2011
I have one more another request for the same thing. I am able to got what i expect. But i doesn't identify the matching file name having which tar file. Is there anyway to get the tar file name as well as the matching file name?Thanks.
# 6  
Old 03-02-2011
You may not need to bother with grep. I tested 3 versions of tar on three different platforms, and each returned a non-zero exit status when asked to list a non-existent member.

Perhaps the following is sufficient for your needs:
Code:
for t in *.tar; do
    if tar -tf "$t" "$f" >/dev/null 2>&1; then
        echo "$t" contains "$f"
    fi
done

Recursively using find:
Code:
find . -type f -name '*.tar' -exec sh -c '
    f=$1
    t=$2
    if tar -tf "$t" "$f" >/dev/null 2>&1; then
        echo "$t" contains "$f"
    fi
' sh "$filename" {} \;

A much more efficient way, if your find supports -exec {} +:
Code:

find . -type f -name '*.tar' -exec sh -c '
    f=$1
    shift
    for t; do
        if tar -tf "$t" "$f" >/dev/null 2>&1; then
            echo "$t" contains "$f"
        fi
    done
' sh "$filename" {} +

Regards,
Alister
# 7  
Old 03-03-2011
Hi Alister, I tried your suggested code 3. Here i modified the "$filename" to my regex only( 'plugin.*.so' - I want list the so file which start with plugin). But, I doesn't got any results.Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To check if file exists

Hi, I have the below code written. However I am not getting the desired output I am checking if the particular path has file in it. #!/bin/bash ls -l /IRS2/IRS2_ODI/INFILE/*LS* 1>/dev/null 2>/dev/null if then echo $? echo "File Exists" fi ... (3 Replies)
Discussion started by: Shanmugapriya D
3 Replies

2. UNIX for Dummies Questions & Answers

UNIX command to check if file name ends with .tar OR if the file is a tar file

Hello Team, Would you please help me with a UNIX command that would check if file is a tar file. if we dont have that , can you help me with UNIX command that would check if file ends with .tar Thanks in advance. (10 Replies)
Discussion started by: sanjaydubey2006
10 Replies

3. Shell Programming and Scripting

Check if file exists or not

Hi, I want to check if the file exists or not in the directory. i am trying below code but not working. File="/home/va59657/Account_20090213*.dat" echo "$File" if ]; then echo "file found" else echo "file not found" fi However i am getting file not found even if file exits as... (5 Replies)
Discussion started by: Vivekit82
5 Replies

4. UNIX for Dummies Questions & Answers

How to check if the same file exists multiple times?

Hi Team , Is there a way I can check to see if the same file say , test.dat exists multiple times in the directory path ? Please help. Thanks Megha (5 Replies)
Discussion started by: megha2525
5 Replies

5. Shell Programming and Scripting

how to check to see if a file exists?

I want to write a script to see if various files exist. What I want to do is have the script search in various directories if a file exist, and if not, then output something like "/path/file does not exist". I don't actually know of how to check and see if a file exists or not. What I have in mind... (2 Replies)
Discussion started by: astropi
2 Replies

6. Shell Programming and Scripting

Script to check for the file existence, if file exists it should echo the no of modified days

Hi, I am looking for a shell script with the following. 1. It should check whether a particular file exists in a location #!/bin/sh if ; then echo "xxx.txt File Exists" else echo "File Not Found" fi 2. If file exists, it should check for the modified date and run a command... (2 Replies)
Discussion started by: karthikeyan_mac
2 Replies

7. Shell Programming and Scripting

Check to see if a file exists?

Hi. I'd like to have an IF-Then-Else statement where I can check to see if a file exists? We have the Bourne Shell by default. I'm looking for the syntax to do something like this: if myfile.txt exists then ...my code else ...my code end if Any help would be greatly... (5 Replies)
Discussion started by: buechler66
5 Replies

8. UNIX for Dummies Questions & Answers

Check if file exists, and create file

hi i have 2 questions: 1. how can i check if file exist in vi? i tryed to do: if ; then echo file exist but i get from the compiler if: Expression Syntax. 2. how can i create file in vi? when im using cat - so the script stop and wait for me to write somthing into the new file when im... (2 Replies)
Discussion started by: nirnir26
2 Replies

9. Shell Programming and Scripting

Check if file exists, create new file?

Hi everyone, I'm brand new to shell scripts (and UNIX in general) and I've been able to figure everything out except this... I want to write a script that will take a filename as an argument, append the date to it (IE Dec 24 2008) and make sure it doesn't already exist, if so, add an integer to... (4 Replies)
Discussion started by: solarnoise
4 Replies

10. Shell Programming and Scripting

Check File Exists and compare to previous day file script

We have data files that are ftp'd every morning to a SUN server. The file names are exactly the same except for that each has the date included in its name. I have to write script to do 2 things: STEP 1) Verify that the file arrived in morning. STEP 2) Compare the file size of the current... (3 Replies)
Discussion started by: rbknisely
3 Replies
Login or Register to Ask a Question