Operating System - HP-UX
1843977 Members
1979 Online
110226 Solutions
New Discussion

Supplying find command with 1 txt file containing a list of files to be located

 
Danny Fang
Frequent Advisor

Supplying find command with 1 txt file containing a list of files to be located

HI,

Last week, I posted a question about supplying the find command with multiple file names, in which thanks to the help of the those who responded, yielded the script below.

Thread:
http://forums13.itrc.hp.com/service/forums/questionanswer.do?threadId=1285274

SCRIPT:
find . -depth -type f \( -name "*.sh" -o -name "*.csh" -o -name "*.perf" -o -name "checkComp*" -o -name "waitfor" -o --name "stopcomp" -o -name "*.list" \) | while read file
do
echo "Copying $file ... \c"
cp -p "$file" "$file.ORIG"
dos2unix $file.ORIG > $file
done
echo "Finished converting files to UNIX format ...\c"


However, I'm attempting to list these txt file names into a file, and supply the txt file containing these file names into find and dos2unix.

Since the files are located in different subdirectories within the main directory tree, I attempted this script:
for i in `cat fileList.txt` do
find . -depth -type f name $i -exec dos2unix {} {} \;
done

But I'd want to know how I can supply a text file containing the list of files to undergo a dos2unix operation, through the find (since files are located in different sub-directories)?

Could anyone point out how this can be done?

Thanks
Danny
5 REPLIES 5
OldSchool
Honored Contributor

Re: Supplying find command with 1 txt file containing a list of files to be located

"However, I'm attempting to list these txt file names into a file, and supply the txt file containing these file names into find and dos2unix.

Since the files are located in different subdirectories within the main directory tree, I attempted this script:
for i in `cat fileList.txt` do
find . -depth -type f name $i -exec dos2unix {} {} \;
done"
================================

A) If you are using the same list of files as the first example, this is going to be extremely wasteful, as you are going execute the "find" once for each "pattern" in your "fileList.txt"....
B) Syntax of the "dos2unix" may be incorrect. The HP-UX command is "dos2ux" and it can't do "in-place" conversions (ie. source and destination file the same.)
C) If you check, you will find that "{}" lists the relative path to the file found from the starting place {".") you specified.

================================

"But I'd want to know how I can supply a text file containing the list of files to undergo a dos2unix operation, through the find (since files are located in different sub-directories)?"

================================

The above is not clear, can you elaborate? I'm not sure how "find / subdirectories" enters into the above (see "C" above



Could anyone point out how this can be done?

James R. Ferguson
Acclaimed Contributor

Re: Supplying find command with 1 txt file containing a list of files to be located

Hi Danny:

If you specify an absolute path as the 'find' directory argument, I think you will have what you need:

# find /path -depth ...

...instead of:

# cd /path
# find . -depth ...

You can't supply a text file containing the list of files as an argument to 'find', though.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Supplying find command with 1 txt file containing a list of files to be located

If you have a file of basenames of files, you need to decide which takes more time.

It may be faster to do a find then grep your list of files. I.e. you are searching for a few files in a directory tree with lots of files:
find path -type -f | grep -f fileList.txt | xargs dos2unix_type_script

>JRF: You can't supply a text file containing the list of files as an argument to 'find', though.

Only if you supply the file to a script that builds a find command.
Peter Nikitka
Honored Contributor

Re: Supplying find command with 1 txt file containing a list of files to be located

Hi,

having just the leaf names or pattern in your listfile, I suggest to create the corresponding find-pattern in your script:

while read pat
do
findopt=${findopt:+$findopt -o -name $pat}
findopt=${findopt:--name $pat}
done
find . -type f \( $findopt \) |
while read ff
do
...
done

mfG Peter

PS: untested
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Dennis Handly
Acclaimed Contributor

Re: Supplying find command with 1 txt file containing a list of files to be located

>Peter: PS: untested

It looks like it does what you say.