5

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?

  • 2
    Try bash substitution: ls .* vs ls .[^.]* – Ipor Sircer Sep 19 '16 at 4:53
  • 1
    Ipor is rigth. Put an echo in front and see what is going to happen: echo rm -rf .* versus echo rm -rf .[^.]* – hschou Sep 19 '16 at 8:46
4

.* 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
  • Finally, someone explained to me what I needed to know. That "." and ".." are actually FILES. It's not intuitive, since those two refer to directories. So unless you're a seasoned Linux user, how would you know that? And that's the reason why I made that mistake. – Gregory Sep 20 '16 at 3:57
  • . 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. – coteyr Sep 20 '16 at 5:19
  • 1
    @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. – Gilles Sep 20 '16 at 9:36
0
find /path/to/dir -type f -name ".*" -delete
3

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.

  • 1
    You can use .[^.]* to match .a and .aa and avoid . and .. – glenn jackman Sep 19 '16 at 19:42
1

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.

  • 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? – Gregory Sep 19 '16 at 5:29
  • 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. – coteyr Sep 19 '16 at 7:31
  • 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. – Gregory Sep 19 '16 at 15:30
  • @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. – coteyr Sep 19 '16 at 15:52
  • 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. – Gregory Sep 20 '16 at 3:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.