The -r flag for directories seems to be useless now

0

cp, scp, rm and other command needs to the -r flag to perform an action on a directory, compared to just a file.

can't copy a directory because WHOOPS you forgot the -r flag

I don't feel this is relevant anymore. these programs should just be written to conditionally alias the old command. if d then its a directory and do the recursive directory moving command

Is there another reason why this should not be the default? Some other combination of flags that make you need the -r flag or else it won't work?

1blockologist

Posted 2020-02-11T04:45:13.663

Reputation: 1

Question was closed 2020-02-12T15:34:17.140

Answers

1

Is there another reason why this should not be the default?

If cp/rm were that "smart", a simple typo in one command could recursively process some "random" directory when you just wanted to copy or remove just one file. Yes, a typo can still cause the wrong file to be removed, but "one file" vs "entire directory tree" can make a huge difference.

It doesn't have to be a typo; it can be an (erroneously) empty variable in a command like this:

rm "/mountpoint/bigdir/$smallfile"

Even cp/scp are not entirely safe in case of similar mishap because they can overwrite target files. So it's not like you can always just remove the unwanted copy and it's all fixed.

Note this argumentation could apply to mv as well. This particular tool is different for historical reasons:

Some historical versions of mv have been able to move directories, but not to a different file system. The standard developers considered that this was an annoying inconsistency, so this volume of POSIX.1-2017 requires directories to be able to be moved even across file systems. There is no -R option to confirm that moving a directory is actually intended, since such an option was not required for moving directories in historical practice. Requiring the application to specify it sometimes, depending on the destination, seemed just as inconsistent.

(source)

Moving a file (possibly a directory) inside a single (inode-based) filesystem is not recursive at all. Only the parent directories (the new one and the old one (they can be the same)) are altered. This most basic and the oldest usage of mv is not recursive even when it moves a directory. I think where the linked article reads "sometimes, depending on the destination" it means -R is justified when the destination is in a different filesystem but completely unjustified when it's in the same filesystem. The choice was made not to introduce -R (or -r or whatever). This way mv follows your postulate by design.


these programs should just be written to conditionally alias the old command

Note when there's a single file specified, -r is still allowed. Therefore "conditionally" is not needed and you can simply alias the commands in question to their forms with -r, if this is really what you want.

Kamil Maciorowski

Posted 2020-02-11T04:45:13.663

Reputation: 38 429