I was told by my friend that one can work with -r switch to find files recursively in directories and subdirectories.Please tell me the error in the given statement it does not work

find / -type f -r -name "abc.txt"
  • 3
    Please always include the actual error messages you receive in your answer."It doesn't work" is not very informative. – terdon Sep 27 '13 at 4:21

The reason it doesn't work is because find has no -r option. While it is true that for many programs the -r flag means 'recursive', this is not the case for all and it is not the case for find. The job of find is to search for files and directories, it is not very often that you don't want it to be recursive.

You can check the options of most programs with the man command. For example, man find. Since the manual of find, is huge, you might want to search it for the -r option:

$ man find | grep -w -- -r

The -- just tells grep to stop reading options, without it, the -r would be passed as an option to grep. Also, you can search within the man page by hitting / then writing what you want to search for, then enter.

That command returns nothing, compare it with this one which searches the manual of cp:

$ man cp | grep -w -- -r
   -R, -r, --recursive

Since find is always recursive, what it does have is the inverse, a flag that lets you choose how many subdirectories it should descend into:

   -maxdepth levels
          Descend at most levels (a non-negative integer) levels of direc‐
          tories below the command line arguments.  -maxdepth 0
           means only apply the tests and  actions  to  the  command  line

   -mindepth levels
          Do  not apply any tests or actions at levels less than levels (a
          non-negative integer).  -mindepth  1  means  process  all  files
          except the command line arguments.

So, whenever you have doubts about a command, read its man page because you never know what a particular option might do. For example:

$ man sort | grep -w -- -r
   -r, --reverse
$ man mount | grep -w -- -r,
   -r, --read-only
$ man awk | grep -A 8 -w -- -r
          Enable the use of interval  expressions  in  regular  expression
          matching (see Regular Expressions, below).  Interval expressions
          were not traditionally available in the AWK language.  The POSIX
          standard  added them, to make awk and egrep consistent with each
          other.  They are enabled by default, but this option remains for
          use with --traditional.
$ man sed | grep -w -- -r
   -r, --regexp-extended
$ man xterm | grep -w -- -r
   -r      This option indicates that reverse video should be simulated by
           swapping the foreground and background colors. 

You get the idea.

| improve this answer | |

find is already recursive, you don't need the -r

| improve this answer | |
  • so isnt there any -r switch in find statement and if there is then what is its need? – chinmay Sep 27 '13 at 4:31
  • @chinmay: There is not. There are it's opposites, the -depth switch which limits search to given depth and -prune command which tells it not to recurse in given directory. – Jan Hudec Sep 27 '13 at 11:51

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.