F_DUPFD(2const)
π F_DUPFD β ok bestie, let's duplicate a file descriptor π
Hiii welcome to the F_DUPFD page from the 2const bit of the Linux man-pages, where we talk about that one fcntl(2) family that clones your file descriptors for you. No cap, it's giving β¨copy-paste energyβ¨ but for kernel handles.
πΈ NAME πΈ
F_DUPFD, F_DUPFD_CLOEXEC β duplicate a file descriptor π«Ά
π LIBRARY π
Standard C library (libc, -lc) β yes girl, you just link it and go π
β¨ SYNOPSIS β¨
#include <fcntl.h>
int fcntl(int fd, F_DUPFD, int arg);
int fcntl(int fd, F_DUPFD_CLOEXEC, int arg);
Like, those are the literal synonyms you call. Periodt.
π DESCRIPTION π
F_DUPFD
Okay so what this does is it duplicates the file descriptor fd, and it picks the lowest-numbered available file descriptor that is greater than or equal to arg for the fresh one. And bestie, notice how that is different from dup2(2), which is out here using exactly the file descriptor you specify, no flexing, no searching. π
When it works? The new file descriptor gets returned to you. That's it, that's the gift. π
Wanna know more deets? Go read dup(2), it's got the lore.
F_DUPFD_CLOEXEC
Literally the same as F_DUPFD, but it also sets the close-on-exec flag on the duplicate file descriptor. The way this is useful is that it lets a program skip an extra F_SETFD(2const) operation just to set the FD_CLOEXEC flag β one syscall instead of two, we love efficiency π
And if you're sitting there like "why do I even care about close-on-exec", the explanation lives in the description of O_CLOEXEC in open(2). Go check it, iykyk.
π₯ RETURN VALUE π₯
The new file descriptor. Iconic. π
On error, -1 is returned and errno is set to tell you what went wrong. π
π ERRORS π
Also go peek at fcntl(2) for the rest of them, but specifically:
EINVALβargis negative, or it's greater than the maximum allowable value (check the discussion ofRLIMIT_NOFILEingetrlimit(2)for that number bestie).EMFILEβ the per-process limit on the number of open file descriptors has been reached. Too many open files, the ultimate "oops" π
π¦ VERSIONS π¦
POSIX.1-2024 specifies F_DUPFD_CLOFORK, but Linux does not support it. Not a thing here, sorry not sorry π
π STANDARDS π
POSIX.1-2024. Slay.
β HISTORY β
F_DUPFD
SVr4, 4.3BSD, POSIX.1-1988. Ancient. Eldest sibling energy π΅β¨
F_DUPFD_CLOEXEC
Linux 2.6.24. POSIX.1-2008. And heads up, to even get this definition you need to define _POSIX_C_SOURCE with the value 200809L or greater, or _XOPEN_SOURCE with the value 700 or greater. Otherwise it's just not there and you'll be confused, and we don't want that π₯Ί
β οΈ CAVEATS β οΈ
The errors returned bydup2(2)are different from those returned byF_DUPFD. Don't assume they're the same, bestie, they are NOT the same. π
π SEE ALSO π
And that's the whole page, mwah π This was brought to you by Linux man-pages 6.20, dated 2026-01-05. Go duplicate something responsibly π«Άπβ¨
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π the page's license: Linux-man-pages-copyleft
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms