TEMPFILE(1)

โœจ NAME โœจ

Okay bestie, meet tempfile from the TEMPFILE(1) manual page ๐Ÿงš. Its whole job is to make a temporary file in a safe way ๐Ÿ”

๐ŸŽ€ SYNOPSIS ๐ŸŽ€

tempfile [-d DIR] [-p STRING] [-s STRING] [-m MODE] [-n FILE]
[--directory=DIR] [--prefix=STRING] [--suffix=STRING] [--mode=MODE]
[--name=FILE] [--help] [--version]

๐Ÿ’– DESCRIPTION ๐Ÿ’–

okay so tempfile creates a temporary file the safe way ๐Ÿ’… It uses mkstemps(3) to pick the name, then opens it with O_RDWR | O_CREAT | O_EXCL. Once it's made, the filename gets printed on standard output ๐Ÿฆ‹

The directory where the file gets created is picked in this order, no cap:

a) If the environment variable TMPDIR exists and holds the name of a valid directory, that wins ๐Ÿ‘‘

b) Otherwise, if the --directory argument is given and it's valid, that gets used ๐ŸŒธ

c) Otherwise, P_tmpdir (as defined in <stdio.h>) gets used when it makes sense ๐Ÿซถ

d) Finally, an implementation-defined directory (/tmp) might get used โœจ

๐ŸŒˆ OPTIONS ๐ŸŒˆ

  • -d, --directory DIR โ€” put the file inside DIR ๐Ÿ“
  • -m, --mode MODE โ€” open the file with MODE instead of the default 0600 ๐Ÿ”’
  • -n, --name FILE โ€” use FILE as the name instead of going through tempnam(3). Heads up: -d, -p, and -s get ignored if you pass this one ๐Ÿ™ˆ
  • -p, --prefix STRING โ€” grab up to five letters of STRING to build the name ๐Ÿง
  • -s, --suffix STRING โ€” make the file with STRING tacked on as the suffix ๐Ÿ“
  • --help โ€” prints a usage message on standard output and exits successfully ๐Ÿ’œ
  • --version โ€” prints version info on standard output and exits successfully ๐Ÿ”ฅ

๐Ÿงš RETURN VALUES ๐Ÿงš

An exit status of 0 means the temp file got created successfully โœจ Literally any other exit status means something went wrong ๐Ÿ’€

๐Ÿ™ˆ BUGS ๐Ÿ™ˆ

โš ๏ธ Exclusive creation is not guaranteed when you're making files on NFS partitions. Be careful bestie!

tempfile can't make temporary directories, so don't even try ๐Ÿ˜ญ Also, tempfile is deprecated, so you should use mktemp(1) instead ๐Ÿ’…

๐Ÿ’… EXAMPLE ๐Ÿ’…

Here's how you'd use it in a shell script, periodt:

#!/bin/sh
#[...]
t=$(tempfile) || exit
trap "rm -f -- '$t'" EXIT
#[...]
rm -f -- "$t"
trap - EXIT
exit

๐Ÿฆ‹ SEE ALSO ๐Ÿฆ‹

tempnam(3), mktemp(1) ๐Ÿซถ


Bye bestie, that's the whole TEMPFILE(1) tea ๐Ÿ’– Debian, 27 Jun 2012 vibes forever ๐ŸŒˆโœจ