Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid error `cannot remove 'XXXXX': is a directory`

Tags:

bash

I'm new in BASH and I'm trying to make a simple script. I would like to run the script from a different directory, and then the script should delete all my files in my current directory. (ONLY FILES) So the function is:

eraseAllFiles()
{   
    rm * 
    echo "Files deleted!"
    sleep 1.3
}

So the command rm * delete all my files but then I get this error:

cannot remove 'XXXXX': is a directory.

My question is how can I avoid this error?

like image 340
user3075653 Avatar asked Sep 19 '25 08:09

user3075653


1 Answers

Instead of rm use find -type f to delete only files:

eraseAllFiles() {   
    find . -maxdepth 1 -type f -delete 
    echo "Files deleted!"
    sleep 1.3
}
like image 188
anubhava Avatar answered Sep 20 '25 22:09

anubhava