Computing.Net > Forums > Unix > find command and tar files

find command and tar files

Reply to Message Icon

Original Message
Name: newone
Date: July 8, 2003 at 08:54:55 Pacific
Subject: find command and tar files
OS: solaris
CPU/Ram: n/a
Comment:

Hi:
1. I have a list of files in a directory. This
directory can contain a normal file or a
tar file of the format *.tar.*, where * is
wild character.

Now to find out whether the file is a tar
file or not and list its contents, I was
checking the pattern matching as shown.

for i in `cat dirname`
do
if [[ $i = "*.tar.*" ]]
then
echo "tar file"
tar tvf $i >outputfile
fi
done

whats wrong with [[ $i = "*.tar.*" ]] ?

2. I am using find command to check for a file
name "fname" in a list of sub directories
as follows.

find . -type f -name "fname"

Now in the list of subdirectories to be
searched, there may be some tar files
in them.

If there are any such tar files of the
form *.tar.*, how can I list the files
within those tar files using
tar tvf command and concatenate
it to my above find command?



Report Offensive Message For Removal


Response Number 1
Name: WilliamRobertson
Date: July 8, 2003 at 10:32:44 Pacific
Subject: find command and tar files
Reply: (edit)

> whats wrong with [[ $i = "*.tar.*" ]] ?

Lose the quotes, i.e:
[[ $i = *.tar.* ]]

I don't follow what the `cat dirname` bit is doing. If you want the contents of directory dirname then just

for f in dirname/*
do

Though you might as well go one further:

for f in dirname/*.tar.*

Similarly, for the find question how about:

for f in $(find .-type f -name '*.tar.* -print)
do
tar tvf $f > outfile
done

You might need another step to deal with compressed tarfiles though, e.g. somefile.tar.gz.


Report Offensive Follow Up For Removal

Response Number 2
Name: newone
Date: July 8, 2003 at 10:47:59 Pacific
Subject: find command and tar files
Reply: (edit)

Thank you for your answer. The dirname is not a directory here.
It is just a file with directory names in it.



Report Offensive Follow Up For Removal

Response Number 3
Name: newone
Date: July 8, 2003 at 11:44:30 Pacific
Subject: find command and tar files
Reply: (edit)


I have a file "file1" whose contents are

./abc.efg.tar.1234
./bcd.efg.tar.1

Now in order to check whether the contents in the file1 are tar files or not,
I am using the following logic.

for i in `cat file1`
do
if [[$i=*.tar.*]];
then
tar tvf $i >>outfile
fi
done

But [[$i=*.tar.*]] pattern matching condition
is not working. Is there any better way of
doing this.


Report Offensive Follow Up For Removal

Response Number 4
Name: WilliamRobertson
Date: July 8, 2003 at 16:15:51 Pacific
Subject: find command and tar files
Reply: (edit)

You just need more spaces:

if [[$i=*.tar.*]]

should be

if [[ $i = *.tar.* ]]


Report Offensive Follow Up For Removal

Response Number 5
Name: Jimbo
Date: July 8, 2003 at 18:12:29 Pacific
Subject: find command and tar files
Reply: (edit)

How about this:

for i in `grep tar file_1`; do
echo $i is a tar file
done

You could also use the 'file' command instead of the if [[ $i = *.tar.* ]] test:

if file $i | grep tar ; then
echo $i is a tar file
....
....
fi

This is probably the most accurate way to determine a tar file since regular files could contain the string 'tar' or your tar files could be named w/o a .tar extension and still be caught. This could be modified easily to pick out compressed files as well.
Just a few thoughts..

-jim


Report Offensive Follow Up For Removal


Response Number 6
Name: newone
Date: July 9, 2003 at 09:31:51 Pacific
Subject: find command and tar files
Reply: (edit)

Thank you both


Report Offensive Follow Up For Removal

Response Number 7
Name: newone
Date: July 9, 2003 at 09:54:07 Pacific
Subject: find command and tar files
Reply: (edit)

Hi,
The if condition test [[ "$i" = "abc*" ]]
does not work for checking whether my
variable i has name starting with abc.

Am I missing some syntax for wildcard *



Report Offensive Follow Up For Removal

Response Number 8
Name: WilliamRobertson
Date: July 9, 2003 at 10:36:27 Pacific
Subject: find command and tar files
Reply: (edit)

Lose the quotes, i.e:
[[ $i = abc* ]]


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: find command and tar files

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software