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 β€” arg is negative, or it's greater than the maximum allowable value (check the discussion of RLIMIT_NOFILE in getrlimit(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 by dup2(2) are different from those returned by F_DUPFD. Don't assume they're the same, bestie, they are NOT the same. πŸ’…

🌈 SEE ALSO 🌈

dup2(2), fcntl(2)


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