General

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?