Computing.Net > Forums > Unix > Shell script to find files

Shell script to find files

Reply to Message Icon

Original Message
Name: higgies
Date: August 8, 2003 at 19:05:54 Pacific
Subject: Shell script to find files
OS: Unix
CPU/Ram: NA
Comment:

Hello!

I'm trying to write a shell script, but don't know where to start. I need to do the following:

- go into each directory, check for a file named "text"
- if the file exists in that directory, I need to run an awk program I wrote.
- this also needs to check any subdirectories.

I'm not even sure where to start except perhaps something like:

find . -name 'text' | exec {} vnmr.awk

I don't know....

Thanks for any help in advance!


Report Offensive Message For Removal

Response Number 1
Name: nails
Date: August 8, 2003 at 20:53:42 Pacific
Subject: Shell script to find files
Reply: (edit)

Hi:

Here's one way:

find . -type f -name "text" -print| while read obj
do
yourawkprogram $obj
done
# end script

Regards,

Nails



Report Offensive Follow Up For Removal

Response Number 2
Name: WilliamRobertson
Date: August 9, 2003 at 07:18:31 Pacific
Subject: Shell script to find files
Reply: (edit)

> find . -name 'text' | exec {} vnmr.awk

"-exec" is part of "find", not a separate command, and "{}" stands for the current file name returned by find. The syntax for this would therefore be:

find . -name 'text' -exec vnmr.awk {} \;


Report Offensive Follow Up For Removal

Response Number 3
Name: higgies
Date: August 9, 2003 at 10:02:25 Pacific
Subject: Shell script to find files
Reply: (edit)

One thing I should've mentioned. I need to get the one output file from the awk program in each of the directories where the text file was found. Nails, you're suggestion worked well, but it only created a single file in the dir where I ran the script from. Any more ideas? I appreciate the help!

Higgies


Report Offensive Follow Up For Removal

Response Number 4
Name: Nails
Date: August 9, 2003 at 11:15:35 Pacific
Subject: Shell script to find files
Reply: (edit)

Higgies:

I'm still not certain what you want to do, but how about getting the basename of the file object and passing that to your awkscript to build an output file?

The output of find looks like this:

./dir1/file1
./dir2/file2

Basename gives you:

./dir1
./dir2

You don't mention which shell you're using, but this gets you the dirname under sh:

find . -type f -name "text" -print| while read obj
do
f=`dirname $obj`
echo $f
done

If you use ksh, this uses ksh pattern matching operators to do the same thing:

find . -type f -name "text" -print| while read obj
do
f=${obj%/*}
echo $f
done

Regards,

Nails


Report Offensive Follow Up For Removal

Response Number 5
Name: higgies
Date: August 9, 2003 at 15:04:17 Pacific
Subject: Shell script to find files
Reply: (edit)

That's exactly what I'm trying to do. I need to go into each subdirectory, see if there is a file named 'text'. If there is, then execute the awk program on it and redirect the output to a file 'text.out' in the same directory as the 'text' file.

The problem I'm having is if I use something like you gave me...

f=${obj%/*}

then f = ./dir1/filename
not ./dir1

I tried string manipulation to remove the last 5 characters from that...but it didn't work. is there some other syntax which would get me that?

I'm using BASH to develop this, but it will likely be run in ksh. They are pretty compatible.

Thanks for your help again.



Report Offensive Follow Up For Removal


Response Number 6
Name: higgies
Date: August 9, 2003 at 15:49:28 Pacific
Subject: Shell script to find files
Reply: (edit)

Nails! You da man!

Ok, I tried it again, this time I redirected output from the awk command to:

> ${obj%/*}/text.out

And it works exactly as I needed it to!

I appreciate the responses!

Higgies


Report Offensive Follow Up For Removal

Response Number 7
Name: nails
Date: August 9, 2003 at 21:01:02 Pacific
Subject: Shell script to find files
Reply: (edit)

Higgies:

Glad I could help. Thanks for the compliment.

Regards,

Nails


Report Offensive Follow Up For Removal

Response Number 8
Name: Heba El-Tahan
Date: September 1, 2003 at 06:16:40 Pacific
Subject: Shell script to find files
Reply: (edit)

I have a problem with shell programming as well.. are there any string manipulation functions? for example I read a line: "different ./temp".. "temp" is a file name and I want to get that name separately in order to do some separate processing on it, can I do that?
something similar to "substring" may be?

I'd really appreciate the response....
regards,
heba


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: Shell script to find files

Comments:

 


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