32

It often happens that I want to apply an operation recursively. Some commands, such as grep, use a lowercase r to indicate recursion. For example

grep -r foo .

Other commands seem to prefer an uppercase R:

chmod -R 755 .

I am constantly getting these the wrong way around and forgetting which is which. Is there any logic behind the selection of case for these arguments?

  • 1
    "Because the coders thought it was sensible." (not saying this is a good reason, but it's probably the truth) – HalosGhost Sep 9 '14 at 14:39
  • 1
    For what it's worth, although not many commands seem to understand --recursive, they're not likely to do the wrong thing. – Tanner Swett Sep 9 '14 at 17:36
  • 4
    in the case of chmod, it's because -r means to unset the read bit for all levels – HorusKol Sep 10 '14 at 3:32
37

Most POSIX commands that have recursive directory traversal option (ls, chmod, chgrp, chmod, cp, rm) have -R for that.

rm also has -r because that's what it was initially, long before POSIX.

Now, the behaviour varies when symlinks are found in walking down the tree. POSIX tried to make things consistent by adding the -L/-H/P options to give the user a chance to decide what to do with symlinks leaving the default when none is provided unspecified.

POSIX grep has no -r or -R.

GNU grep initially had neither. -r was added in 1998. That was following symlinks.

-R was added as a synonym in 2001 for consistency with the other utilities. That was still following symlinks.

In 2012 (grep 2.12), -r was changed so it no longer followed symlinks, possibly because -L, -H were already used for something else.

BSDs grep were based on GNU grep for a long time. Some of them have rewritten their own and kept more or less compatibility with GNU grep. Apple OS/X addressed the symlink issue differently. -r and -R are the same and don't follow symlinks. There's a -S option however that acts like chmod/cp/find's -L option to follow symlinks.

  • 13
    TL;DR: History. – Sammitch Sep 10 '14 at 0:37
  • 10
    Early history: rm had a recursive option before the others. It was -r. Then cp gained a matching -r. Then ls wanted to have a recursive option, but ls -r already meant "reverse sort" so it had to be -R. There the tension between -R and -r began. -R was the only one that could be added consistently to every relevant utility, but rm -r was already the well-known traditional usage. Then GNU came along and said "consistency and tradition are for squares, man!" – user41515 Sep 10 '14 at 3:37
8

None whatsoever. It simply depends on what the developers chose. It is often because both -r and -R are valid options. In the programs you quoted, for example:

  • recent versions of GNU grep:

    -r, --recursive
          Read all files  under  each  directory,  recursively,  following
          symbolic  links  only  if they are on the command line.  This is
          equivalent to the -d recurse option.
    
    -R, --dereference-recursive
          Read all files under each directory,  recursively.   Follow  all
          symbolic links, unlike -r.
    
  • chmod has no -r option so, presumably, the devs prefer -R. However, of course, -r is a valid permissions string (as pointed out by @Arkadiusz Drabczykso) so it can't really be used there.

  • What is your grep version? GNU grep 2.12, -r and -R are the same. With chmod, -R is defined by POSIX. – cuonglm Sep 9 '14 at 14:42
  • @Gnouc grep (GNU grep) 2.15 and I remember seeing this in previous versions. Are you sure it's not the case in yours? They provide essentially the same results in most cases, they only behave differently with links. As for chmod, it may well be defined by POSIX but that is still, presumably, because the original chmod devs chose R over r. – terdon Sep 9 '14 at 14:47
  • 2
    @Gnouc D'oh! Obviously, they couldn't use -r since that is already a valid permissions string. – terdon Sep 9 '14 at 14:49
3

Mostly, it comes down to a developer personal preference. Sometimes, however, an uppercase option is chosen if a preferred lowercase option is taken for something else that developers believe is more important than, for example, operating recursively. In case of chmod -r is a valid mode. For example:

$ ll FILE
-rw-r--r-- 1 ja ja 0 Sep  9 16:42 FILE
$ chmod -r FILE
$ ll FILE
--w------- 1 ja ja 0 Sep  9 16:42 FILE
1

I am constantly getting these the wrong way around and forgetting which is which.

Where possible, use the GNU versions of these utilities and then you can use the longer names for options.

command --recursive

Is there any logic behind the selection of case for these arguments?

No.

Or not much. The Unix utilities were developed piecemeal and the command options reflect the individual choices of their lead or sole developers. There are only 26 ASCII lower-case option letters available, which is the preferred set (commands are generally in lower case for ease of typing), and this limited set leads to mnemonic conflicts. Conflicts lead to inconsistency as new versions of commands/utilities gained new features.

-1

20 years ago when I had learnt UNIX, my mentor told me like this: "You'd better to type the recursive option in uppercase R always. Because some commands has different meaning on lowercase r option, but uppercase R mostly works as recursive option everywhere. And it will give you a good habit to be cautious on [rm * -Rf] by annoying pressing shift-key on typing R."

  • 2
    It would have been better if he'd told you to read the manual for the command on the particular system you're on, as non-standard options may well be different between implementations of tools. – Kusalananda Jul 1 at 10:00

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.