Computing.Net > Forums > Unix > find a file in a directory

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

find a file in a directory

Reply to Message Icon

Name: Mano
Date: September 3, 2003 at 10:40:46 Pacific
OS: aix
CPU/Ram: 2x355/512
Comment:

I have a small problem @ work. I am trying to write a script that will seach a directory for a file with the contents 'IS314.1'. Then move that file 2 a new directory. This is what I have but I am not sure I am using the right approach.
(I am using SH because that is all I am familiar with.)

ls -l | sort +4 | sort +5 > tempfile.txt
cat tempfile.txt | while read file
do
filename=${file}
echo $filename
if $filename = uv*
then what?

I am not sure I am takign the right approach.
Basically I need to find string IS314.1 in a file uv* and cp file to another directory as IS312.1

Please help!



Sponsored Link
Ads by Google

Response Number 1
Name: Mano
Date: September 3, 2003 at 13:15:15 Pacific
Reply:

I know I am doing something wrong but so far I have modded what I have above to:
1. wrote a script to do:
ls -t uv* > listspl.txt
2. wrote a script to run after the main program runs (the one that generates IS314.1 - this cuts down on the files we need to look at)
ls -t uv* > listspl2.txt
diff -n listspl.txt listspl2.txt > listspl3.txt
cat listspl3.txt | grep uv > listsplfin.txt

3. This part is breaking, I know something is feshmekeled but I can't figure out what I am doing wrong.

cat /u1/uvspool/listsplfin.txt | while read file
do
filename=${file}
cat /u1/uvspool/$filename | while read test
do
testme=${test}
if $testme = "IS314.1"
then
cp /u1/uvspool/$filename /u2/dbms/MHCDATA/DWNLD/IS314.1
fi
done
done


0

Response Number 2
Name: Mano
Date: September 3, 2003 at 13:27:40 Pacific
Reply:

oh yea and the "IS314.1" string will be found within the first 10 lines of the file so it is not necessary to continue beyond that.


0

Response Number 3
Name: Mano
Date: September 3, 2003 at 14:22:04 Pacific
Reply:

Ok I think I have step3 working now but if someone has a better (i.e. more ellegant solution) please let me know -
This is what I have:

cat /u1/uvspool/listsplfin.txt | while read file
do
filename=${file}
if(grep IS314.1 /u1/uvspool/$filename)
then
cp /u1/uvspool/$filename /u2/dbms/MHCDATA/DWNLD/IS314.1
break
fi
done
cd /u1/dbms/LIVE.MHC
exit 0


0

Response Number 4
Name: David Perry
Date: September 4, 2003 at 05:08:57 Pacific
Reply:

#!/bin/sh
for file in `find . -exec grep -l IS314.1 {} \; ` ; do
cat $file >> /u2/dbms/MHCDATA/DWNLD/IS312.1
# you may have more than one occurance.
# should this overwrite or append?
done


0

Response Number 5
Name: Mano
Date: September 4, 2003 at 09:40:03 Pacific
Reply:

actually I would like to keep the most reacent version. (mtime comes ot mind - but where do you put the command?) - There will only be one version on the day the script is run.

Also why do yo need the {}\;`;

This is all new to me so please explain. Also why grep -l?

I can't believe that you took all those lines and got it done in 2! I knew there was a better way but I am not that good at putting the commands togethers yet.

Thanks,
Mano


0

Related Posts

See More



Response Number 6
Name: David Perry
Date: September 4, 2003 at 10:20:48 Pacific
Reply:

grep -l will print a file name where a match occurs and not the contents of the file. You can use this to generate a list of files that contain a substring you are looking for.

A find command that does more than the default print needs to be terminated bye "\;".

The "{}" represent the file matched by find.

To find and keep the most recent file, the find command would need to be piped through sort and head -1. I'll work on the details an post more of an answer. If there is only one version when the file is run you can do a cp instead of cat.

cp $file /u2/dbms/MHCDATA/DWNLD/IS312.1


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: find a file in a directory

delete files in specific directory www.computing.net/answers/unix/delete-files-in-specific-directory/7357.html

Finding large files! www.computing.net/answers/unix/finding-large-files/3037.html

count the occurence of a char in a string www.computing.net/answers/unix/count-the-occurence-of-a-char-in-a-string/8516.html