4

Most of the files are gone, but I'm still left with these two files:

".RData" and ".Rhistory"

Why is this the case? I'm working with R, but I don't know what those files are.

Afterwards, I can individually remove them without needing to use sudo.

3
  • does that happen with only these two files or with all hidden files ?
    – Rahul
    Jun 13, 2016 at 6:48
  • just those two files. I was able to remove other hidden directories Jun 13, 2016 at 6:49
  • Sorry, that was a mistake. I had copied from a previous directory and thought I copied all hidden files, but they weren't actually copied. Jun 13, 2016 at 6:56

2 Answers 2

24

* only includes visible files. If you want to delete both those and the hidden ones, use:

rm -rf * .*

The dotglob option

With bash, we can change this behavior and unhide files. To illustrate, let's create two files, one hidden and one not:

$ touch unhidden .hide1
$ ls *
unhidden

As you can see, only the unhidden one is shown by ls *. Now let's set the dotglob option:

$ shopt -s dotglob
$ ls *
.hide1  unhidden

Both files appear now. We can, of course, turn dotglob off if we want:

$ shopt -u dotglob
$ ls *
unhidden

Documentation

From man bash:

When a pattern is used for pathname expansion, the character "." at the start of a name or immediately following a slash must be matched explicitly, unless the shell option dotglob is set. When matching a pathname, the slash character must always be matched explicitly. In other cases, the ``.'' character is not treated specially. See the description of shopt below under SHELL BUILTIN COMMANDS for a description of the nocaseglob, nullglob, failglob, and dotglob shell options.

In other words, pathname expansion ignores files whose names begin with . unless the . is explicitly specified.

Safety issues

To avoid unpleasant surprises, rm will refuse to remove the current directory . and the parent directory .. even if you specify them on the command line:

$ rm -rf .*
rm: refusing to remove ‘.’ or ‘..’ directory: skipping ‘.’
rm: refusing to remove ‘.’ or ‘..’ directory: skipping ‘..’
2
  • @Rahul Yes, good point. Answer updated with rm's warnings.
    – John1024
    Jun 13, 2016 at 7:08
  • 1
    Can I explicitly exclude the . and .. directories?
    – kiltek
    Jul 28, 2020 at 19:27
-1

Make sure the directory is correct then

sudo rm -rf * .*

5
  • 2
    how does that differ from @John1024's answer ?
    – Rahul
    Jun 13, 2016 at 6:55
  • sudo for permission based issues. Jun 13, 2016 at 7:04
  • 11
    Some people get into the habit of using sudo on every command, or at least every command that has ever failed (regardless of whether the failure was permissions-based). Other people get into the habit of aiming loaded firearms at their feet. These habits are highly similar: they both create a high probability that you will eventually shoot yourself in the foot. Don't use sudo unless you understand the reason why the command will work with sudo and fail without it. Jun 13, 2016 at 7:18
  • What stops .* from expanding to .. ? and then you blow away the directory one level up and everything in it ?
    – Criggie
    Jun 13, 2016 at 8:33
  • 2
    @Criggie See John1024's answer. In short, rm special cases . and .. and doesn't remove them. Jun 13, 2016 at 8:55

Not the answer you're looking for? Browse other questions tagged .