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

后端 未结 6 434
抹茶落季
抹茶落季 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:39

    You want to use find command inside an if condition , you can try this one liner :

     [[ ! -z `find 'YOUR_DIR/' -name 'something'` ]] && echo "found" || echo "not found"
    

    example of use :

     [prompt] $ mkdir -p Dir/dir1 Dir/dir2/ Dir/dir3                 
     [prompt] $ ls Dir/
     dir1  dir2  dir3
     [prompt] $ [[ ! -z `find 'Dir/' -name 'something'` ]] && echo "found" || echo "not found"
     not found
     [prompt] $ touch Dir/dir3/something
     [prompt] $ [[ ! -z `find 'Dir/' -name 'something'` ]] && echo "found" || echo "not found"
     found
    

提交回复
热议问题