shell - Is there a way to show "File not found" or a message alike when using the `find` command?

07
2014-07
  • jim

    I'm trying to check the existence of several files, without knowing in advance where the files are located. So, I thought of find as the de facto utility to do this... It works as expected, but the problem is that it does not acknowledge the non-existence of files, that is, if a file does not exists under the searched directory then, as you will expect, it's not showed in the results.

    I guess this is fine, as the find command is meant to find files, which actually exists in the first place (d'oh). But I wonder if there's a way for the find command to return a message like "File not found" or alike, to inform that the file is not there, instead of failing (or succeeding) silently.

    I thought that maybe I could workaround the problem by using find's return code by querying $?, but even when the file is not found the return code is 0.

    Just an example of what I have...

    find . -name foo.sh 
    find . -name bar.sh 
    

    and what it returns in case the only file in there is foo.sh:

    ./directory/foo.sh
    

    What I would like to receive is:

    ./directory/foo.sh
    bar.sh not found
    

    Does anyone knows of a find flag or any other workaround I can use?
    Thanks!

  • Answers
  • user322483

    you could try find . -name foo | grep \/ to set the exit code.

    grep looks for any / in the output and returns exit code 1 if none is found

  • user3631881

    I don't think find has an option to say 'what you looked for was not found'.

    You can count the number of lines returned by find and if it's 0 print the 'not found' message. Something like:

    #/bin/bash
    
    # put find output to a temp file
    find . -name 'foo.sh' > /var/tmp/find.tmp.$$
    
    # count the number of lines
    COUNT=`wc -l /var/tmp/find.tmp.$$ | awk '{ print $1 }'`
    
    
    if [ $COUNT -eq 0 ]
    then
       # find didn't return any results, so print message
       echo "foo.sh not found"
    else
       # find returned results, print them
       cat /var/tmp/find.tmp.$$
    fi
    # remove temp file
    rm /var/tmp/find.tmp.$$
    

    That will print the output of the find command if there were any results or 'foo.sh not found' when no results found.


  • Related Question

    shell - find files within a specific directory structure but a variable parent directory structure
  • Rohit Banga

    I want to use the find command in linux to find a specific file nested within a specific directory structure, say dir1/dir2/reqdfile.

    But this directory structure can itself be nested within any parent directory structure.

    Is it possible to a search like?

    find directory_to_search -name "**/dir1/dir2/reqdfile"
    

    What is the exact syntax?


  • Related Answers
  • Dennis Williamson

    Use -path instead of -name:

    find directory_to_search -path "*/dir1/dir2/reqdfile"
    

    Note that there's only one asterisk.

  • Jeremy Sturdivant

    In general, a quick and dirty alternative would be to use grep. Though it's not as clean for find specifically, thanks to the -path option, many similar cases can be solved like so:

    find directory | grep "/dir1/dir2/reqdfile$"