adbdkb
adbdkb

Reputation: 2161

Using rm -rf with a directory

I tried to search on SO, but not able to find the difference between the following commands. if I have a directory named dir, how the below commands differ?

Also how do the user permissions on the directory affect the outcome, if the id running the command is not the owner or not even in the group of the owner?

I am adding the command to do rm -rf in a shell script I am working on and need help in understanding the difference between the above commands.

Upvotes: 3

Views: 29538

Answers (1)

AtomHeartFather
AtomHeartFather

Reputation: 959

  • rm -rf dir/*

    Removes files within the directory (without removing the directory itself). Note, hidden files won't be removed.

  • rm -rf dir/

    Trailing slash indicates that dir is a directory. If it was a file, it wouldn't get removed. In your case this is identical to rm -rf dir, but in general it differs (see below)

  • rm -rf dir

    In your case, identical to the one above.

In general, tools such as rm usually follow IEEE/OpenGroup standards when it comes to pathname resolution, which means that dir/ is equivalent to dir/.. One implication of that is that if diris a symlink to a directory rm -rf dir/ will remove the content of the directory (including the hidden files) but not the link or the directory itself, whereas rm -rf dir will only remove the symlink.

You need to have write permissions on a file or directory that you are removing, plus exec permissions on a directory that rm needs to traverse to remove files. You can read more about Unix filesystem permissions here.

Upvotes: 11

Related Questions