How to delete all files in a current directory starting with a dot?

16,329

Solution 1

.* matches all files whose name starts with .. Every directory contains a file called . which refers to the directory itself, and a file called .. which refers to the parent directory. .* includes those files.

Fortunately for you, attempting to remove . or .. fails, so you get a harmless error.

In zsh, .* does not match . or ... In bash, you can set

GLOBIGNORE='.:..:*/.:*/..'

and then * will match all files, including dot files, but excluding . and ...

Alternatively, you can use a wildcard pattern that explicitly excludes . and ..:

rm -rf .[!.]* ..?*

or

rm -rf .[!.] .??*

Alternatively, use find.

find . -mindepth 1 -delete

Solution 2

When you want to delete all dot files, the convention is to use this command:

rm .??*

This will delete all files starting with a dot containing at least two other characters, thus leaving . and .. intact. Granted, it will also miss file names with only one letter after the dot, but these should be rare.

Solution 3

find /path/to/dir -type f -name ".*" -delete

Solution 4

You can ignore the error for your purposes.

You used wildcard matching. Not to be confused with regex. You said delete all files that start with a . and have nothing, or something after that .

Now your error message is because that would include . and ... . is a shortcut for current directory, and '..' is a shortcut for parent directory. (Technically they mean something else, but for this conversation it works).

You can not delete the directory your currently in, else where would you be? And you can not delete your parent directory, else where would current directory go. Now this is not strictly true, you can delete your current directory and your parent directory, but the tools (rm in this case) are setup to make it a bit harder, because doing so is a bit counter intuitive.

is what went wrong and if what I did could really cause any serious damage to the system in general?

Based on the info in your question, my answer is yes you did something wrong and yes you could have caused some pretty serous damage to your system.

First and foremost you ran that command with elevated privileges. You should NEVER EVER do anything recursively with elevated privileges unless you already know the outcome. You should certainly never delete ANYTHING with elevated privileges unless you know exactly what your doing. Next time use mv. That way if you do mess something up you can just move it back.

As to what damage you actually caused, being that you were deleting a trash bin, probably not much, but you really could have, specially when running that command with elevated privileges.

Share:
16,329

Related videos on Youtube

Gregory
Author by

Gregory

Updated on September 18, 2022

Comments

  • Gregory
    Gregory over 1 year

    I use Ubuntu 14.04 and in a terminal I became root with sudo su and I wanted to delete root's trash manually. It deleted everything except for a few files that start with a dot. Like .htaccess etc. So I went to that directory (which is "files") and I ran this command:

    rm -rf .*
    

    It did delete those files, BUT I also got an error message that the system couldn't delete "." and ".." What does it mean? Like if I tried to delete the whole directory tree? Like I said, when I was running that command I was in the lowest directory. This one to be exact: /root/.local/share/Trash/files/ I shot down my PC and then turned it on. Everything seems to be normal at first glance. So now I want to ask is what went wrong and if what I did could really cause any serious damage to the system in general? In other words, should I be worried now or everything is OK?

    • Admin
      Admin over 7 years
      Try bash substitution: ls .* vs ls .[^.]*
    • Admin
      Admin over 7 years
      Ipor is rigth. Put an echo in front and see what is going to happen: echo rm -rf .* versus echo rm -rf .[^.]*
  • Gregory
    Gregory over 7 years
    Looks like I confused DNS "." with a Linux "." So in my case it wasn't the whole directory tree, starting from above, but only the current directory and the one above it, i.e. "files" and "Trash", correct? And then I got an error, since the system couldn't delete those two, considering my current location then. So those are good news then. No damage is done. I checked those two directories and they do exist now. As per elevated privileges, but how can I delete root's trash without becoming root myself first?
  • SarahG
    SarahG over 7 years
    You can't, but root's trash shouldn't have anything in it anyway. You shouldn't be becoming root often enough to leave behind trash. But that's a whole different question.
  • Gregory
    Gregory over 7 years
    Most stuff I do in Linux requires root privileges. That's why I have to delete stuff as root. But that's beyond the scope of this question.
  • SarahG
    SarahG over 7 years
    @Gregory Your correct that it's beyond scope, but you should spend 99.999999 % of your time as an unprivileged user. If that's not true, then "you're doing it wrong" and you should look into fixing that.
  • Angel Todorov
    Angel Todorov over 7 years
    You can use .[^.]* to match .a and .aa and avoid . and ..
  • Gregory
    Gregory over 7 years
    I often add or delete files in /var/www/html folder and without root privileges I can't do it. Likewise, when I use Truecrypt containers and want to delete something there, I can't do it as a regular user, etc. etc. So I don't know what 99,99999% you're talking about here, with all due respect. And that's what do mostly on Linux. Web development and server infrastructure. And I use Truecrypt for online backups. I don't need Linux to use Photoshop and play FarCry 4. And I ALWAYS have backups of everything, Ubuntu system included. Though it is a waste of time to restore everything.
  • SarahG
    SarahG over 7 years
    If you are frequently changing stuff in /var/www/html then you should add yourself to the www group (or what ever group those files are in). Same with Truecrypt containers though Truecrypt is depricated, and has (or could have) some major security flaws. wiki.archlinux.org/index.php/TrueCrypt
  • SarahG
    SarahG over 7 years
    . and .. predate Linux. They predate all modern OS in use. There are a small handful of OSes that don't use them, but all the ones your likely to run into do.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 7 years
    @Gregory A directory is a type of file. The fact that * omits all dot files and not just . and .. in fact originates from a programming error (in ls, whose behavior the * wildcard reproduces): the authors meants to skip . and .. but the code ended up skipping all files whose name beginning with . and they made the dubious-in-hindsight decision that this was a useful behavior and kept it.
  • Peter - Reinstate Monica
    Peter - Reinstate Monica over 2 years
    @Gilles'SO-stopbeingevil' Fascinating bit about the bug turning into the feature. Can one read that story somewhere?
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 2 years
  • Peter - Reinstate Monica
    Peter - Reinstate Monica over 2 years
    @Gilles'SO-stopbeingevil' Cool, thanks! Dot-files seem so reasonable (also because DOS/Windows has hidden files) but true, the config fikes should go into a ~/cfg or such.