Ignore ‘cannot remove `dir`: Is a directory message

deleted-filesdirectoryrm

I'm setting up an automation process where I am deleting files in a directory which contains sub-directories. I only want to delete the files in the directory, and want to keep the sub-directories intact. So right now I am just using rm * to delete the files in that directory. However, this command throws the message: cannt remove 'dir': Is a directory. I know I'm being knit-picky, but I don't want that message to repeatedly appear in my logs. Is there a better command I can use for deletion or a way that I can tell rm to ignore the sub-directories?

Best Answer

You can just throw away the error messages:

rm * 2>/dev/null

That'll throw away all errors. If you want to see other potential errors then we can do something more complicated:

rm * 2>&1 | grep -v 'cannot remove .*: Is a directory'

In this way other errors will still be logged.