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"
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"
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
arguments.
-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
-r
--re-interval
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.
grep -w -- -r
won't work with every grep
implementation, in particular BSD's which are the ones that introduced -w
in 2BSD in the late 70s, where grep -w x
is the same as grep '\<x\>'
. \<
matches on the transition between a non-word character and a word character. As -
is not a word character, grep -w -- -anything
will never match there.
– Stéphane Chazelas
Feb 02 '22 at 17:58
find
is already recursive, you don't need the -r
-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