Linux can't delete files

前端 未结 3 523
既然无缘
既然无缘 2021-01-22 06:49

I have a problem with removing files when I find them. Task : must find files with spaces and remove them

my try :)

rm $(find -L /root | grep -i \' \')
<         


        
3条回答
  •  梦毁少年i
    2021-01-22 07:37

    I'm guessing you are finding files with spaces in, or quotes. Try this:

    find /test/path -print0 | xargs -0 rm
    

    What this will do is send the filenames to stdout separate by NULL bytes, which xargs will take as delimiters. This allow spaces, quotes and other such fun in the output.

    Now, if you are removing directories, rm is not going to work. So you might want to add a -type f to the above.

    Note that gnu find itself has a -delete operator which will delete files for you, but you wanted to know why. Hence a shorter route would be:

    find /test/path -delete
    

    This will deal with directories too if you do not add -type f. It will also handle deleting the deepest things first (think about why this is needed).

提交回复
热议问题