Operating System - HP-UX
1830167 Members
5485 Online
109999 Solutions
New Discussion

Find all ascii text files

 
SOLVED
Go to solution
Scott Lindstrom_2
Regular Advisor

Find all ascii text files

Is there some combination of commands that will allow me to somewhat efficiently find all ascii text files on a server such that I can grep them for a command?

I see the find command will let me limit the search to regular files, but that would be too broad of a search.

Scott
7 REPLIES 7
Pete Randall
Outstanding Contributor

Re: Find all ascii text files

Scott,

I think the find command is your only hope. You can break it down to a find command for each file system or something to reduce the size of the task, but I know of no other way. "find /start_dir -type f |xargs grep "command" > /tmp/find_start_dir.output


Pete

Pete
Geoff Wild
Honored Contributor

Re: Find all ascii text files

sure - use

find / -type f

Do a
man find
for more info.


Rgds...Geofff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Patrick Wallek
Honored Contributor

Re: Find all ascii text files

I would use a combination of the find command and the file command.

First I would find all files (find -type f), create a list, and then run the file command on each file to make sure it isn't a binary /executable of some sort. Then from that list you could execute your grep.

# find /dir -type f -print >> /tmp/list

# for i in $(cat list)
do
VAR=$(file $i | grep text | wc -l)
if [ $VAR = 1 ] ; then
COMM=$(grep sometext $i | wc -l)
if [ $COMM = 1] ; then
echo "Command found in $i"
fi
fi
done

The above is not guaranteed to work, but hopefully you get the idea.
H.Merijn Brand (procura
Honored Contributor

Re: Find all ascii text files

# perl -MFile::Find -le'find(sub{-f&&-T&&print$File::Find::name},@ARGV)' .

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
G. Vrijhoeven
Honored Contributor
Solution

Re: Find all ascii text files

Hi Scott,

Here is mine:

for i in `find . -type f`
do
file $i | grep -qi as
if [ $? -eq 0 ]
then
echo $i
fi
done >>/tmp/asciilist.txt

Gideon
H.Merijn Brand (procura
Honored Contributor

Re: Find all ascii text files

mmmm, maybe too terse

from 'man perlfunc:

-f File is a plain file.
-d File is a directory.
-l File is a symbolic link.
-p File is a named pipe (FIFO), or Filehandle is a pipe.
-S File is a socket.
-b File is a block special file.
-c File is a character special file.
-t Filehandle is opened to a tty.

-u File has setuid bit set.
-g File has setgid bit set.
-k File has sticky bit set.

-T File is an ASCII text file (heuristic guess).
-B File is a "binary" file (opposite of -T).

-M Script start time minus file modification time, in days.
-A Same for access time.
-C Same for inode change time (Unix, may differ for other pl
atforms)

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Chris Watkins_1
Respected Contributor

Re: Find all ascii text files

Yeah, that's a big ugly one-liner, for sure. Put this all on one line:
(where "cmd" is the command you want to grep for)

grep "cmd" `find . -type f -exec file {} \; |grep -v jpg |grep -v gif
|grep -v outfile.txt |grep text |awk -F: '{print $1}'` >outfile.txt



Sometimes jpg and gif show up as "text", so we have to ignore.
There may be others, but those two stuck out.
Not without 2 backups and an Ignite image!