Last Updated: September 11, 2022
·
16.8K
· dpecos

Delete all files (including hidden ones) with just one rm

This will match any regular file or directory, plus hidden ones, excluding '.' and '..':

rm -rf {,.[!.],..?}*

And remember: with great power comes great responsibility

11 Responses
Add your response

rm -rf *

over 1 year ago ·

@offirmo, that does not delete hidden files (i.e .htaccess)

over 1 year ago ·

You can't delete . or .. so trying does not matter.

rm -rf * .*
over 1 year ago ·

@lokiastari Of course it matters! If it's part of an script you have to handle errors, and your command ends with an error:

/tmp/test$ rm -rf * .*
rm: cannot remove directory: `.'
rm: cannot remove directory: `..'
/tmp/test$ echo $?
1

Whereas my version of rm finishes with a success error code:

/tmp/test$ rm -rf {,.[!.],..?}*
/tmp/test$ echo $?
0
over 1 year ago ·

If you write that in a script that has to be maintained you should be fired.

Have to disagree. Its two ugly to remember for a simple command line
On the command line:

rm -rf . .*

This is just intuitively obvious (unlike the reg-ex monstrosity you concocted) and the error messages are logical. So are not a problem.

In a script (both are stupid but you can correct for the error message):

(rm -rf . .* || echo "Delete * .* Done") 2> /dev/null

This is still so much more readable than your original code.

Though you would still never do that. If this is a repeatable script that you are tidying up from then you are going to do something is easy to read and maintain:

# Clean up the current directory (which we are in)
# This directory holds some state and thus must be cleaned
# So scour it absolutely clean before use.
function scourDirectory {
    dir=`pwd`
    local=${dir##*/}
    cd ..
    rm -rf ${local}
    mkdir ${local}
    cd ${local}
}

Here verbosity is your friend not a hindrance.

over 1 year ago ·

@lokiastari Well, I don't quite understand what you're saying about maintaining an script and being fired or not (are scripts written once and remain intact forever without a minimal maintenance?? lol ). I'm not going to enter into that discussion but try to focus on the actual subject.

Anyway, your latest version works fine, but you are not handling exit codes properly. In fact, you don't seem to care about if the rm could or could not delete anything, it just prints "Done" and you swallow all error messages: that's not handling errors, that's covering them. Good luck with that!

I will stay with my version, a little more complex than using an OR but simpler to handle. At least I could known if the rm did in fact delete something or not. As I said in my initial post with great power comes great responsibility

over 1 year ago ·

OK. What I am saying is that you would never write either in a script.

So the only place you use this is on a command line.
Given that. Yours is just way too unreadable for general usage and the error messages mine produce are quite fine (or ignorable). In other-words this is not a tip (or even useful).

over 1 year ago ·

@lokiastari I needed this command while creating a deployment script for a webapp to a pristine directory structure, so rsync was not a suitable solution.

That's why I needed to empty a directory path completely before making the deployment.

I find it's quite usefull and fits great in the script, and also great used as an alias in the command line.

over 1 year ago ·

What about this one?

find ./ -mindepth 1 -maxdepth 1 -exec rm -rf {} \;

over 1 year ago ·

Cheers this is exactly what I wanted, both for bash scripts and command line. I've alias'd it :)

alias rma='rm -rf {,.[!.],..?}*'

over 1 year ago ·

Oh my, I'd never alias a "rm rf" even if someone put a pistol to my head.

over 1 year ago ·