cp: -r or -R?
Thread
Loading the complete thread in the background. This saved snapshot is available now. Refresh
Unofficial Hacker News client; not affiliated with Y Combinator.
cp: -r or -R?
Loading the complete thread in the background. This saved snapshot is available now. Refresh
Unofficial Hacker News client; not affiliated with Y Combinator.
bsoqk · · focus · HN ↗
Waterluvian · · focus · HN ↗
Yaqub_W · · focus · HN ↗
Waterluvian · · focus · HN ↗
greatgib · · focus · HN ↗
Is it the case in another country to have a localized name for that?
amenghra · · focus · HN ↗
It is common for brands to localize their names. E.g. Axe (the deodorant) in some countries is branded as Lynx.
317070 · · focus · HN ↗
jolmg · · focus · HN ↗
Is it the case that it would've been pronounced differently without the written accent in French?
Dylan16807 · · focus · HN ↗
swiftcoder · · focus · HN ↗
[deleted] · · focus · HN ↗
[deleted]
NaiveBayesian · · focus · HN ↗
andrewshadura · · focus · HN ↗
mqus · · focus · HN ↗
olowe · · focus · HN ↗
hnfong · · focus · HN ↗
JdeBP · · focus · HN ↗
And the received wisdom about long options in the BSDs is a quarter of a century out of date. When the BSDs gained a getopt_long() in their C libraries thanks to Klausner and Baron, long options quietly started appearing. This process has been gradually and quietly on-going for the whole of the 21st century.
throw0101a · · focus · HN ↗
"NextBSD" was first released in 2015:
* <a href="https://en.wikipedia.org/wiki/NextBSD" rel="nofollow">https://en.wikipedia.org/wiki/NextBSD
Over a decade after macOS/Mac OS X was initially released:
> macOS (previously OS X and originally Mac OS X) is a proprietary Unix[7][8] operating system, derived from OPENSTEP for Mach and FreeBSD, which has been marketed and developed by Apple since 2001.
* <a href="https://en.wikipedia.org/wiki/MacOS" rel="nofollow">https://en.wikipedia.org/wiki/MacOS
> Darwin is the core Unix-like operating system of macOS, iOS, watchOS, tvOS, iPadOS, audioOS, visionOS, and bridgeOS. It previously existed as an independent open-source operating system, first released by Apple in 2000. It is composed of code derived from NeXTSTEP, FreeBSD[3] and other BSD operating systems,[7] Mach, and […]
* <a href="https://en.wikipedia.org/wiki/Darwin_(operating_system)" rel="nofollow">https://en.wikipedia.org/wiki/Darwin_(operating_system)
I remember reading release notes for FreeBSD in the '00s and seeing the exact same lines in the release notes for earlier versions of OS X.
JdeBP · · focus · HN ↗
hnfong · · focus · HN ↗
<a href="https://www.reddit.com/r/freebsd/comments/1g07sdm/comment/lr6rizo/" rel="nofollow">https://www.reddit.com/r/freebsd/comments/1g07sdm/comment/lr...
Essentially, macOS was indeed based on NeXTSTEP originally, but over the years they copied quite a bit of BSD code over (mostly FreeBSD I think). I don't think the Unixy parts of the original NeXTSTEP survives much in modern macOS.
alwillis · · focus · HN ↗
macOS isn't "Unix-like"; it's an Open Group certified UNIX™ [1].
[1]: <a href="https://www.opengroup.org/openbrand/register/brand3725.htm" rel="nofollow">https://www.opengroup.org/openbrand/register/brand3725.htm
toast0 · · focus · HN ↗
> And the received wisdom about long options in the BSDs is a quarter of a century out of date.
Incidentally, so is much of the FreeBSD code. Many things were taken once, never updated from upstream again. I think a lot of the userland did get an update somewhere around 2014... all of a sudden, cal would highlight the current date, years after that happened upstream. But the IP stack hasn't gotten any updates from upstream AFAIK, probably because Apple customized it and it's too much work to merge in improvements that probably won't be noticed on client systems (things like syncookies).
dotancohen · · focus · HN ↗
here_to_learn · · focus · HN ↗
[dead]
drhagen · · focus · HN ↗
dotancohen · · focus · HN ↗
<a href="https://unix.stackexchange.com/questions/82485/when-wouldnt-one-want-cp-to-be-recursive" rel="nofollow">https://unix.stackexchange.com/questions/82485/when-wouldnt-...
It seems that recursive by default would have been much more intuitive.
JdeBP · · focus · HN ↗
Yes, seriously. When you read about the supposed evils of cat -v from the Unix nostalgia people, remember that it was the same people who gave cat its -v option who also gave cp its -r option, in 4.2BSD.
It took over half a decade to percolate out of the BSD world, too. AT&T Unix System 5 did not have an -r option to cp. Here's Brandon S. Allbery explaining in 1987 how one copies directories on AT&T Unix System 5 Releases 2/3 by combining find and cpio -p:
* <a href="https://groups.google.com/g/comp.unix.questions/c/XiumTgkcYRE/m/kJwkU5SlOasJ" rel="nofollow">https://groups.google.com/g/comp.unix.questions/c/XiumTgkcYR...
Originally we read directories as raw byte streams and liked it, you know. (-:
Joker_vD · · focus · HN ↗
Then the people who complained about cat -v went ahead and made better Unix, called Plan 9, in which moving (as opposed to merely renaming) a directory is impossible: instead, you're supposed to do mkdir && dircp && rm -r. Which is quite a choice, if I say so myself: there is simply no low-level primitive (syscall or 9p message) for moving files across directories, even on the same file server. Their version of mv can move files across the directories, but it's still done with internal equivalent of cp+rm.
ButlerianJihad · · focus · HN ↗
I believe that my motivation was that the Unix wars were still raging hotly; as a sysadmin I needed portable skills and I wasn't tied or certified to one vendor's Unix, and therefore I sought out idioms that worked on SunOS, HP/UX, AIX, BSD, you name it. "cp -r" didn't enjoy that stable support at the time.
JdeBP · · focus · HN ↗
I use pax -r -w most of the time.
alwillis · · focus · HN ↗
[1]: <a href="https://keith.github.io/xcode-man-pages/ditto.1.html" rel="nofollow">https://keith.github.io/xcode-man-pages/ditto.1.html
5555watch · · focus · HN ↗
aulin · · focus · HN ↗
wanick · · focus · HN ↗
pluc · · focus · HN ↗
amelius · · focus · HN ↗
IshKebab · · focus · HN ↗
brewmarche · · focus · HN ↗
mjmas · · focus · HN ↗
GrantMoyer · · focus · HN ↗
0xpgm · · focus · HN ↗
mkl · · focus · HN ↗
Not sure why you wouldn't want to preserve timestamps, links, etc. by default.
JdeBP · · focus · HN ↗
It's also not even completely covering the weird case of -r and -R for the cp command. On HP-UX, for example, the twain were different, but not in the way that they were in old GNU Core Utilities. That would be too easy. (-:
The AIX manual for cp explains its difference between -r and -R:
* <a href="https://ibm.com/docs/en/aix/7.1.0?topic=c-cp-command" rel="nofollow">https://ibm.com/docs/en/aix/7.1.0?topic=c-cp-command
Illumos also treats the two differently, but in a subtly different way:
* <a href="https://illumos.org/man/1/cp" rel="nofollow">https://illumos.org/man/1/cp
Joker_vD · · focus · HN ↗
The "metadata is atomically copied" part would support very nicely the usual text editor's idiom of rename(2)ing a temporary file over the source after fully writing it out — you still need to accurately replicate the permissions and extended attributes. And just as shells are important enough programs to have fork(2) almost exactly suited for them, it would make sense to have copy(2), suited for the text editors.
JdeBP · · focus · HN ↗
However, I should note that possibly the first company to invent what you describe was Microsoft.
Novell Netware 386 had an NCOPY command which invoked a Netware extension to the DOS API that told the server to perform the entire copy on the server.
But even earlier, OS/2 1.x had a proper DosCopy() system call. Since it could be passed down to the installable filesystem drivers for intra-volume copies, something like the Netware client for OS/2 could in theory turn it into the same protocol call that did server-side copies. There was a NET COPY command in LAN Manager (and LAN Server, if memory serves) that did the same optimization.
* <a href="https://www.edm2.com/index.php/DosCopy_(OS/2_1.x)" rel="nofollow">https://www.edm2.com/index.php/DosCopy_(OS/2_1.x)
* <a href="https://www.edm2.com/index.php/FS_COPY" rel="nofollow">https://www.edm2.com/index.php/FS_COPY
Joker_vD · · focus · HN ↗
Eh, when fork was invented, most (virtual) memory systems did not have the first clue about copy-on-write either. And honestly, it's really not that difficult to support — it's essentially hard links, just with slightly different semantics.
collinfunk · · focus · HN ↗
Linux has FICLONE [1], which I prefer because it operates on two file descriptors, allowing you to safely modify file metadata after the fact. macOS has clonefile, clonefileat, and fclonefileat [2]. Sadly, there is no way to operate on two file descriptors. The best you get is fclonefileat, which operates on a source file descriptor. Solaris has reflink and reflinkat, which operate on two paths, the latter relative to file descriptors [3]. In that case, one needs to be careful opening the destination to make changes to the metadata.
GNU coreutils has support for reflinks on Linux and macOS. But I've been thinking about adding support for Solaris as of late [4]. Sadly, I don't use it enough to test it as much as I would like.
Hopefully, a few years down the line the interfaces converge, and it is as simple as hard linking.
[1] <a href="https://man7.org/linux/man-pages/man2/FICLONE.2const.html" rel="nofollow">https://man7.org/linux/man-pages/man2/FICLONE.2const.html [2] <a href="https://www.manpagez.com/man/2/clonefile/" rel="nofollow">https://www.manpagez.com/man/2/clonefile/ [3] <a href="https://docs.oracle.com/cd/E86824_01/html/E54766/reflinkat-3c.html" rel="nofollow">https://docs.oracle.com/cd/E86824_01/html/E54766/reflinkat-3... [4] <a href="https://lists.gnu.org/archive/html/coreutils/2026-09/msg00121.html" rel="nofollow">https://lists.gnu.org/archive/html/coreutils/2026-09/msg0012...
emmelaich · · focus · HN ↗
Anyway, just use rsync.
dspillett · · focus · HN ↗
You still need to specify --archive (or --times for the individual option) to preserve mtime in the target copy.
pasc1878 · · focus · HN ↗
trucks-refinish · · focus · HN ↗
groestl · · focus · HN ↗
rincebrain · · focus · HN ↗
groestl · · focus · HN ↗
Flimm · · focus · HN ↗
<a href="https://github.com/RsyncProject/rsync/issues/119#issuecomment-3769076742" rel="nofollow">https://github.com/RsyncProject/rsync/issues/119#issuecommen...
trucks-refinish · · focus · HN ↗
5555watch · · focus · HN ↗
CoastalCoder · · focus · HN ↗
Also, I always have this vague fear that I'll rsync in the wrong direction, or accidentally blow away unrelated files in rsync's efforts to fully synchronize two directories (can't remember if this is a valid concern).
I'm sure these concerns would go away if I used it regularly, but I just don't. ‘cp' or ’scp’ almost always meet my needs.
toast0 · · focus · HN ↗
It's the same direction as cp/scp ... You could easily do it wrong, but you must be afraid to do anything other than maybe cat < source > dest because that's clear?
> or accidentally blow away unrelated files in rsync's efforts to fully synchronize two directories (can't remember if this is a valid concern).
I think you need --delete for this. As I recall, it's sometimes more tricky to get it to delete the files you wanted it to delete and you're more likely to end up with extra files than not.
groestl · · focus · HN ↗
bariumbitmap · · focus · HN ↗
> Historic versions of the cp utility had an -r option. This implementation supports that option; however, its use is strongly discouraged, as it does not correctly copy special files, symbolic links or FIFOs.
<a href="https://man.openbsd.org/cp" rel="nofollow">https://man.openbsd.org/cp
embedding-shape · · focus · HN ↗
The snippet of code makes it very clear what the difference is, no?
flag_copy_as_regular = 1 VS flag_copy_as_regular = 0, where regular would be a "regular" and not "special" file.
maxlin · · focus · HN ↗
Modified3019 · · focus · HN ↗
rezonant · · focus · HN ↗
wodenokoto · · focus · HN ↗
rakel_rakel · · focus · HN ↗
from the lstat man page:
symbolic link example: For posterity, -R with added -Lbdavbdav · · focus · HN ↗
jwrallie · · focus · HN ↗
bdavbdav · · focus · HN ↗
throwaway2046 · · focus · HN ↗
WhyNotHugo · · focus · HN ↗
I didn't even know -r was a thing until today.
spijdar · · focus · HN ↗
kijin · · focus · HN ↗
tosti · · focus · HN ↗
kijin · · focus · HN ↗
laxd · · focus · HN ↗
collinfunk · · focus · HN ↗
The oldest version on the GNU ftp server is fileutils-3.13 [1]. I vaguely remember having some links to older versions, probably somewhere in my archived mail. But I don't remember if it was fileutils-3.9 or earlier.
I co-maintain GNU coreutils, so I am interested in reading them. If you have them, you can email me privately or on the public mailing list. Both are listed on the homepage [2].
[1] <a href="https://ftp.gnu.org/old-gnu/fileutils/" rel="nofollow">https://ftp.gnu.org/old-gnu/fileutils/ [2] <a href="https://www.gnu.org/software/coreutils/" rel="nofollow">https://www.gnu.org/software/coreutils/
tosti · · focus · HN ↗
collinfunk · · focus · HN ↗
tosti · · focus · HN ↗
eichin · · focus · HN ↗
[dead]
Sophira · · focus · HN ↗
The original GNU release of fileutils-3.6 is there too (but be careful; one of the two files appears to include Makefiles from a ./configure run, while the other doesn't), though 3.7 and 3.8 don't appear to be. You'll need to sift through to find what you need in some cases, but it's an incredibly valuable research tool.
Note: To download a file, you'll need to click on the filename, and then click the small arrow at the top that looks like a red arrow pointing down to a white box with red/green lights. (Which I assume is their representation of a desktop computer.)
[0] <a href="https://discmaster.textfiles.com/" rel="nofollow">https://discmaster.textfiles.com/
[1] <a href="https://discmaster.textfiles.com/search?q=fileutils-3.9.tar.gz" rel="nofollow">https://discmaster.textfiles.com/search?q=fileutils-3.9.tar....
krn1p4n1c · · focus · HN ↗
Rygian · · focus · HN ↗
dspillett · · focus · HN ↗
(or ssh <target> prepended rather than sandwiched, to copy from remote)
Rygian · · focus · HN ↗
krn1p4n1c · · focus · HN ↗
krn1p4n1c · · focus · HN ↗
dspillett · · focus · HN ↗
jhallenworld · · focus · HN ↗
xrd · · focus · HN ↗
Feels like this would be exactly the kind of thing they would get wrong. Fur exactly, the training set isn't trained to know the context of execution (FreeBSD vs macos vs Linux), right?
saidnooneever · · focus · HN ↗
appearently i am not :') never knew there was -r
hirvi74 · · focus · HN ↗
BobMontgomeryJr · · focus · HN ↗
BobMontgomeryJr · · focus · HN ↗
[dead]
dataflow · · focus · HN ↗
turtledragonfly · · focus · HN ↗
You may also notice that some of these utilities will allocate memory, then never free it. They just let the OS handle that on program shutdown. That is also generally not recommended in arbitrary code. But again, it's fine for a one-and-done sort of program.
For these cases, the whole program is essentially one function call, and the global namespace is essentially the "body" of that function (hand waving a bit).
It's all a bit subjective, of course (:
Bjartr · · focus · HN ↗
chasil · · focus · HN ↗
We'd prefer Rust versions with great forethought, but it's irrelevant to the testing.
There is no current version of Linux [uswrland] that maintains certified POSIX compliance. Only Apple, IBM, HP, and SCO are current?
<a href="https://www.opengroup.org/openbrand/register/" rel="nofollow">https://www.opengroup.org/openbrand/register/
kelnos · · focus · HN ↗
seba_dos1 · · focus · HN ↗
It's a good rule of thumb to avoid either of them, especially when you're inexperienced yet, but they still have their uses.
comex · · focus · HN ↗
The main issue with this is that the programs are usually still single-threaded. For some key utilities like find and grep, this has allowed more modern alternatives to leapfrog them in performance. (To be fair, it’s hard to add parallelism without breaking compatibility somewhat.)
But in most other respects, there’s nothing wrong with being a little old-fashioned. Using global variables is an example of that. It’s a problem in larger programs but at this scale it’s fine.
souvlakee · · focus · HN ↗
chasil · · focus · HN ↗
<a href="https://pubs.opengroup.org/onlinepubs/9699919799/utilities/" rel="nofollow">https://pubs.opengroup.org/onlinepubs/9699919799/utilities/
mzhaase · · focus · HN ↗
bitwize · · focus · HN ↗
Sophisticated Pooh: cp -a
ddosmax556 · · focus · HN ↗
midtake · · focus · HN ↗
jasoneckert · · focus · HN ↗
However, some tools like cp starting using both -r and -R to mean recursive, and I imagine there are many newer tools where the author chose -r for recursive only.
Things change over time.