how to prevent "find" from dive deeper than current directory
I have many directory with lots of files inside them.
I've just compressed that directory respectively become filename.tar.gz
, someothername.tar.gz
, etc.
After compressing, I use this bash to delete everything except file name contains .tar.gz
:
find . ! -name '*.tar.gz*' | xargs rm -r
But the problem is find
will dive too deep inside the directory. Because the directory has been deleted but find
will dive deep in each directory, many messages displayed, such as:
rm: cannot remove `./dirname/index.html': No开发者_JS百科 such file or directory
So how to prevent find
from dive deeper than this level (current directory)?
You can use ls
instead of find
for your problem:
ls | grep -v .tar.gz | xargs rm -rf
You can tell find the max depth to recurse:
find -maxdepth 1 ....
精彩评论