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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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
Method 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.
Method 3
find /path/to/dir -type f -name ".*" -delete
Method 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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0