Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Manipulating find output 1

Status
Not open for further replies.

teser

Technical User
Mar 6, 2001
194
US
I have a find script that works but I was wondering how I can get the output to tell me if the data is a directory.

Example of my output:
./wer
./wer/zzz/wer

if the first 'wer' is a directory I want it to output like this:
./wer IS A DIRECTORY
./wer/zzz/wer

Here is my script and I know I can fetch a directory with something like this:
(substr($1,1,1) == "d") but I dont know how to put it in my script.


[tt]
#!/bin/ksh
echo "Enter the file you want to find"
read dat
echo "Searching `pwd` for $dat...."
sleep 1

find . -name "$dat" -print > rock
i=`ls -l rock | awk '{print $5}'`

if [ $i == 0 ]
then
print "\nNot found\n"
else
print
cat rock
print
fi
print "End of search"
rm rock [/tt]
 



echo "Enter the file you want to find"
read dat

echo "Searching $PWD for $dat ..."
sleep 1

find . -name "$dat" -exec ls -Fd {} \; |
awk '{
last = substr($0,length($0),1) ;
if (last != "/")
print $0 ;
else
print substr($0,1,length($0)-1) "IS A DIRECTORY" ;
}
END {
if (NR == 0) print "\nNot found\n"
} '



Jean Pierre.
 
Would anyone know how to control my output of showing one page at a time?
Some of my finds have over 200 records and I want to display them a page at a time or just 20 at a time then hit Carriage Return and get the next 20 records.
Is this possible using awk in this script?

[tt]
echo "Enter the file you want to find"
read dat

echo "Searching $PWD for $dat ..."
sleep 1

find . -name "$dat" -exec ls -Fd {} \; |
awk '{
last = substr($0,length($0),1)
#Would I put a diplay counter in the if/else statements and how would I
#accept the input for displaying page by page?

if (last != "/")
{b++;print substr($0,1,length($0)-1)} #COUNTS HOW MANY FILES
else
{i++;print substr($0,1,length($0)-1) " IS A DIRECTORY "} #COUNTS DIRECTORIES
}
END{
if (NR != 0)
print "Total record count = " NR "\nDirectory count = " i++ "\nFile count = " b++
else
print "No data found\n"
} [/tt]
 
Just pipe the result of awk into more

find .... | awk '......' | more Jean Pierre.
 
Thanks again aigles.

Just one final question. How would I eliminate the files that come up in my output that say "Permission denied". I seem to be getting alot of those on some of my outputs.

I have tried some of the /dev/null options but both are not working:
[tt]find . -name "$dat" -exec ls -Fd > /dev/null {} \; |
awk '{....

find . -name "$dat" -exec ls -Fd {} \; > /dev/null 2>&1 |
awk '{.....[/tt]

Any suggestions?


 
Just awk scripting and one little cheat.

awk ' BEGIN {
cmd = "ls $PWD/"
printf "Glob pat: "
getline pat < &quot;-&quot;

while ((cmd pat | getline) > 0) {
if ($0 ~ /^.*:$/) {
d_arr[x++] = $0
} else {
f_arr[a++] = $0
}
}

for (all in d_arr) {
print &quot;Directories matching&quot;, d_arr[all]
}

for (allf in f_arr) {
print &quot;File matches: &quot;, f_arr[allf]
}
}'


This is more fun than the shell.
 
find ... | grep -v 'Permission denied' | awk '...'

The 'grep -v' command exclude all lines with 'Permission denied' inside Jean Pierre.
 
find . |while read x
do
a=$(file $x)
set $a
if [ &quot;$2&quot; = &quot;directory&quot; ] ;then
echo $x is a directory
else
echo $x is a file
fi
done

To short? OK
bla bla bla bla Gregor.Weertman@mailcity.com
 
find . |while read x
do
if [ -d &quot;$x&quot; ] ;then
echo $x is a directory
else
echo $x is a file
fi
done
Jean Pierre.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top