• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 239
  • Last Modified:

File Grep in unix

Hi,

Can anyone help me with a code snippet to search for a file  recursively in Dir/sub dir and if it is not then print that sub dire name.
0
new_perl_user
Asked:
new_perl_user
  • 2
1 Solution
 
pritamduttCommented:
Here is a sample....


find / -type d '!' -exec test -f '{}/.qmail' ';' -print

Open in new window


where

find - is find command in unix
/ - is the path to search in
-type d - search for directories only
'!' - negate the output of -exec command
-exec - execute sub command
test -f '{}/.qmail' - where it will test for existence of .qmail filename... replace with your desired filename
-print - prints matching list

Hope this helps!

Regards,
0
 
new_perl_userAuthor Commented:
instead of printing the matching list can we do the non-matching list and I just want to find no need to replace anything.
0
 
pritamduttCommented:
In this case use of '!' will print directories not containing .qmail file. As specified by you in your requirement.

Also, note there is no replacement being done.


Hope you have tried the command to check the outcome!
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
pritamdutt's code works just fine

Can be put into a script like this:
if [ $# != 2 ]
then
	echo "$0 <dir> <file>"
else
	find $1 -type d '!' -exec test -f "{}/$2" ';' -print
fi

Open in new window

make the script above executable and call like this:

./script basedir file
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now