Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using find in bash script - how to handle case where find returns "file or directory does not exist"

Tags:

find

bash

I'm using the find command in my bash script like so

for x in `find ${1} .....`;
do
    ...
done

However, how do I handle the case where the input to my script is a file/directory that does not exist? (ie I want to print a message out when that happens)

I've tried to use -d and -f, but the case I am having trouble with is when ${1} is "." or ".."

When the input is something that doesn't exist it does not enter my for loop.

Thanks!

like image 722
Ken Avatar asked Nov 04 '22 06:11

Ken


1 Answers

Bash gives you this out of the box:

if [ ! -f ${1} ];
then
    echo "File/Directory does not exist!"
else
    # execute your find...
fi
like image 163
Anew Avatar answered Nov 15 '22 05:11

Anew