How to check if find command didn't find anything?

后端 未结 6 432
抹茶落季
抹茶落季 2021-01-01 14:11

I know that is possible to use for loop with find command like that

for i in `find $something`; do (...) done

but I want to use find comma

6条回答
  •  太阳男子
    2021-01-01 14:40

    Iterating over the output of find might be dangerous if your filenames contain spaces. If you're sure they don't, you can store the result of find to an array, and check its size to see there were any hits:

    results=( $(find -name "$something") )
    if (( ${#results[@]} )) ; then
        echo Found
    else
        echo Not found
    fi
    for file in "${results[@]}" ; do
       # ...
    done
    

提交回复
热议问题