Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cygwin: difference between '\rm -fr' and 'rm -fr' command?

I have one shell script running on windows environment on cygwin environment. This script have one purging function which deletes certain folder on the system bases on certain condition.

I prepare the list of all the folder that I want to delete and then use following command:

rm -rfv $purge (where purge is the list of directories I want to delete)

Now when I tested this script, the directories are not getting deleted at all. First I thought there is some issue with by purge list, but on debugging I came to know that purge list is fine.

After lots of debugging and trials I just made small change in command:

\rm -rfv $purge 

It just a kind of hit and trial and script starts working fine. Now as far as I know \rm and rm -f both means forceful delete.

Now how can I justify this that why 'rm -f' what now working earlier but '\rm -f' did. I want to know the basic difference between these two commands.

like image 720
Dhruv Bansal Avatar asked Jun 03 '12 13:06

Dhruv Bansal


2 Answers

The rm can be (in theory) one of:

  • shell builtin command (however I don't know any shell with such builtin)
  • external command (most likely /bin/rm)
  • a shell function
  • an alias

If you put \ before it (or quote any part of it, for example "rm" or even 'r'm) shell will ignore all aliases (but not functions).

As jlliagre mentioned, you can ask shell what rm is and what is \rm using type builtin.

Experiment:

$ type rm
rm is /bin/rm
$ rm() { echo "FUNC"; command rm "$@"; }
$ type rm
rm is a function
$ alias rm='echo ALIAS; rm -i'
$ type rm
rm is aliased to `echo ALIAS; rm -i'

Now, we have alias rm, function rm and original external rm command: Let's see how to call each other:

$ rm   # this will call alias, calling function calling real rm
$ rm
ALIAS
FUNC
rm: missing operand
$ \rm  # this will ignore alias, and call function calling real rm
FUNC
rm: missing operand
$ command rm  # this will ignore any builtin, alias or function and call rm according to PATH
rm: missing operand

To understand it deeply, see help builtin, help command, help alias and man sh.

like image 168
Michał Šrajer Avatar answered Sep 30 '22 12:09

Michał Šrajer


That means your rm command is aliased or a function. Backslashing it tells the shell to use the real rm command.

Edit: You can tell what rm refers to with the type command, eg:

$ type rm
rm is /bin/rm

.

$ type rm
rm is aliased to `rm -i'

.

$ type rm
rm is a function
...
like image 26
jlliagre Avatar answered Sep 30 '22 14:09

jlliagre