Operating System - HP-UX
1827422 Members
4118 Online
109965 Solutions
New Discussion

Re: Question using the find command -

 
SOLVED
Go to solution
Rob Johnson_3
Regular Advisor

Question using the find command -

I want to do a find on several text files on a system. When the files are found I want to cat those files out and write them to a file.

How can I do this?
15 REPLIES 15
Steven E. Protter
Exalted Contributor

Re: Question using the find command -

find / -exec grep -l 'criteria' {} \; > filelist

This will check the contents of text files and create a lsit which you can cat later or cat with a script.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Rob Johnson_3
Regular Advisor

Re: Question using the find command -

I think that command will search through files for a certain string of text.

I don't think that's what I want to do.

Here's another tip on what I want to do.

There are several thousand files on the machine. I need to be able to find certain files amongst those several thousand and cat the results to another file.

for i in `cat /home/me/filestocatout'
do
find $i (against those devices)
cat $i (cat the found files to another file)
Bill Hassell
Honored Contributor

Re: Question using the find command -

There is no such thing in Unix as a text file (or binary file or data file, etc). In other words, all files except for special files like pipes and device files are simply files. Unlike PCs where most programs will assign suffix or extension and then use that extension to start an appropriate program, Unix does not maintain any content flag anywhere. The closest you can come to identifying a text file is to use the file command. It will actually read a portion of the file, then run through a series of tests listed in the /etc/magic file.

Now if you already have a list of known-to-be -text files, then your script would look like this:

for MYFILE in $(cat /home/me/filestocatout)
do
cat $MYFILE >> another_file
done

But if this list, filestocatout, contains untested files, your script would have to be changed to something like this:

for MYFILE in $(cat /home/me/filestocatout)
do
file $MYFILE | grep -q text
if [ $? -eq 0 ]
then
cat $MYFILE >> another_file
fi
done

It turns out that most tests in /etc/magic will return the word "text" if the file looks like it contains ASCII text.


Bill Hassell, sysadmin
Rob Johnson_3
Regular Advisor

Re: Question using the find command -

The CiscoWorks application runs on this unix box. The configuration files for each router and switch are stored in various directories on a filesystem in text format.

There are approximately 4000 files total. I need to be able to search through all of these files for specific files. I would like to be able to say something like "for all of the files in the list I specify, do a find on the filename, once found, print the contents of all the files to one large text file so I can search through it for certain command strings".
Bill Hassell
Honored Contributor

Re: Question using the find command -

I think you mean that the filenames are predefined ir at least have something in common like xxxx-cw.log or something similar, correct? If so, you can ask find to locate the file by using the -name option, something like this:

find /somedirectory -type f -name "*-cw.log"

Now you always want to do the above command to determine if the files that were found are indeed the correct ones (and none are overlooked), then you can accomplish the entire task with one line:

find /somedirectory -type f -name "*-cw.log" -exec cat {} \; >> /var/tmp/bigfile

If you know what criteria you need to find within each file, you can add that to the command line:

find /somedirectory -type f -name "*-cw.log" -exec cat {} \; | grep -i -e err -e warn >> /var/tmp/bigfile


Bill Hassell, sysadmin
lawrenzo
Trusted Contributor

Re: Question using the find command -

do you know part of the string to be searched within the file or part of the file name required?
hello
Rob Johnson_3
Regular Advisor

Re: Question using the find command -

OK. How can I use the find command you gave

find /somedirectory -type f -name "*-cw.log" -exec cat {} \; >> /var/tmp/bigfile

to somehow read from a file a list of device names I want to search for. For example, I may need to search for these files which is the hostname of the device with a .cfg extension-

host1.cfg
host2.cfg
host99.cfg
lcartr1.cfg
lcaswitch99.cfg
oregoncat1.cfg
oregonsw99.cfg

The hostnames of the devices don't follow a pattern so I can't say *rtr.cfg or *switch.cfg
Bill Hassell
Honored Contributor

Re: Question using the find command -

I'm not clear whether your task is to find all files that end with .cfg in which case the command would be:

find /somedirectory -type f -name "*.cfg" -exec cat {} \; >> /var/tmp/bigfile

If you already have a list of the filenames or perhaps a list of the hostnames, then you can use the for-do-done construct to enumerate the filenames and have find locate the specific filename.


Bill Hassell, sysadmin
Rob Johnson_3
Regular Advisor

Re: Question using the find command -

I have a list of filenames (aka hostnames). I want to search through the 4000 or so files and find the files that match the list then write the contents to a file.

How on earth would I do what you suggested by using the for-do-done constuct to enumerate the filenames and have the find locate the specific filename then write the contents of the file to largefile?
Rob Johnson_3
Regular Advisor

Re: Question using the find command -

I guess it's gonna look something like:

for i in `cat /home/listofhosts`
do
find /somedirectory -type f -name $i -exec cat {} \; >> /var/tmp/bigfile
done

lawrenzo
Trusted Contributor
Solution

Re: Question using the find command -

so if you know the host names you could do this.

Create a file with each host name in ie

cat /home/listofhosts

host.1
host.2
host.3

then

for i in `cat /home/listofhosts |awk '{print $1}'`
do
echo "details for host $i" >> /var/tmp/file
find /somedirectory -type f -name "$i.cfg" -exec cat {} \; >> /var/tmp/file
done

HTH
hello
Rob Johnson_3
Regular Advisor

Re: Question using the find command -

Ok. Thanks everyone for your input. I think I can make this work. I don't have a unix box here at home so I can't test this out until Monday.

Again, Thank you!
Mark Ellzey
Valued Contributor

Re: Question using the find command -

Rob,

One small point to mention; if you are going to cat somewhere around 4000 files to /onebigfile, you better be sure you have the disk space available. In other words, don't try to save the result in /onebigfile. Filling up / is not good.

Just my 2cts.

Mark
Howard Marshall
Regular Advisor

Re: Question using the find command -

Rob,

If I understand what you are trying to do I may have a better idea for you.

You have several file names that are scattered about in a file system with 4000 other files.

You want to find all the files with those names and cat them all into one big file.

If thatâ s the case, you donâ t have to find each file individually you can just find all 4000 files once and grep out the names you are looking for and cat those files to your big file

Find . -xdev -print | grep -e "file1" -e "file2" | while read filename
Do
Cat $filename >> $grut.big.file
Done

Or if you have many file names list them in a pattern file and user grep -f patternfile

If thatâ s not what you are trying to do, then ignore this post.

H
Rob Johnson_3
Regular Advisor

Re: Question using the find command -

Thanks for your response. Actually, I was able to run put a script together from the previous posts and it worked quiet well. I'm gonna stick with it.
I love this forum because each time I need to do something like this, I can come here then come away with a solution.

Again thanks for your response.