File Grep in unix

Posted on 2011-10-31
Last Modified: 2012-05-12

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.
Question by:new_perl_user
    LVL 9

    Expert Comment

    Here is a sample....

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

    Open in new window


    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!


    Author Comment

    instead of printing the matching list can we do the non-matching list and I just want to find no need to replace anything.
    LVL 9

    Accepted Solution

    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!
    LVL 37

    Expert Comment

    by:Gerwin Jansen
    pritamdutt's code works just fine

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

    Open in new window

    make the script above executable and call like this:

    ./script basedir file

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    Title # Comments Views Activity
    Ldapsearch all expiring passwords in OID 1 42
    unix scripting question 1 70
    Find non-UTF Filenames 1 41
    Remote install of MSI file 4 18
    Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
    Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now