Short answer
No. However, I really like your creativity of piping commands together. Still, yes|rm -r
is a nice example of UUOC (useless use of cat) - an acronym (or better jargon) for command line constructs that only provide a function of convenience to the user.
Long Answer
I really like your question, as it can be answered in more than one dimension.
Technical dimension
The rm
command is used to delete files. The option -r
or in its longer form --recursive
is used to delete the files (as well as directories) recursively. The -f
(long: --force
) option is used to ignore non-existent files and arguments and to never prompt for confirmation. See the man page for more details, I linked it below.
So in case you want to delete a directory recursively, than rm -rf
is the correct way to do it.
Linguistic dimension
You heard many times that issuing rm -rf
is dangerous, and that is correct. The missing point is that no one has told why this is the case.
You already provided the answer as part of your initial question: It is not the command itself that is dangerous. It is the user who types something mistaken and then is confronted with deleted files or directories that he should never have wanted to delete in the first place.
So what is meant by this warnings you heard is: Think twice if you really want to delete what you have entered, before you hit the enter button
Your question implies that you did not fully understand it the way it was meant, but you understood instead: "The command itself is dangerous, so I need to find another way to delete my files and directories".
Of course doing so is possible with Unix commands, as there is always more than one way to reach a goal. With the warnings you have heard, however, is meant to truly reflect on, so the (creative) pipeline construct yes|rm -r
does not help you thinking.
That's the reason why it is not less dangerous as a rm -rf
.
Historical dimension
All Unix commands were built once using the mantra: "Do one job, and do this job perfectly".
However, this is confusing for people who are used to be patronized by their operating system. In Windows, for instance, it is still quite normal for the user to be asked after initiating a action: "Do you really want to do this or that?"
This is not the case if you are using Unix commands, unless you explicitly request to be asked to confirm. The rm
command is no exception and knows such a parameter: -i
or --interactive
.
The trouble started around 2004, when a lot of new Linux users arrived very rapidly, caused by the rise of Ubuntu as easy-to-use operating system. All of them had a relatively small understanding how Linux works under the hood. And many of them made a mistake, which resulted in many systems being deleted due to a mistyped input as rm -rf /
. Sad times...
The mistake made back then was the attempt to "protect" those users, much like the way Windows does. So aliases were introduced by distributors like Ubuntu, that automatically turned an rm
command into an rm -i
. It would have been better, in my opinion, to teach the Unix way to new users, instead of patronizing them.
Nevertheless, the bottom line is that some distributions still use such alias definitions, therefore many people think that interactive querying is default to the rm
command. However, this is not the case.
References
rm -rf
is "considered dangerous", but it is not documented as such. Usingrm
at all could delete the wrong file, and is equally "dangerous". – NiKiZe Aug 29 '21 at 12:03rm -rf
andyes | rm -r
as documented by manuals or source code. – Aug 29 '21 at 13:18echo rm -rf ...
if you're using any glob patterns. This will show you the expanded arguments rm will get. If all looks good, type control-p to get the last input line back, deleteecho
, and hit return. – Dale Hagglund Aug 30 '21 at 07:27rm
(only possible with some implementations) is to put the flags at the end:rm /some/stuff -rf
. Not foolproof, but does at least prevent therm -rf /etc<enter>
type mistakes. – SeamusJ Aug 30 '21 at 22:51yes|rm -ir
? The-i
may come from a bash alias, which some distributions add to the default.bashrc
. – allo Aug 31 '21 at 13:47