Sample Header Ad - 728x90

Unix & Linux Stack Exchange

Q&A for users of Linux, FreeBSD and other Unix-like operating systems

Latest Questions

12 votes
7 answers
3379 views
Idiomatic way of generating a unique filename?
In a script I'm writing, I want to create something temporary on my filesystem, but - it's not in `/tmp` but elsewhere, and may not be a file nor a directory (e.g. maybe it's a named pipe or a symbolic link). The point is - I'll have do the creation myself. Now, I want to use a unique filename for m...
In a script I'm writing, I want to create something temporary on my filesystem, but - it's not in /tmp but elsewhere, and may not be a file nor a directory (e.g. maybe it's a named pipe or a symbolic link). The point is - I'll have do the creation myself. Now, I want to use a unique filename for my temporary, so that future invocations of the utility plus any other running code won't try to use the same name. If I were just creating a temporary file or directory in /tmp, I could use mktemp. But - what do I do when I only want to generate the name?
einpoklum (10753 rep)
Apr 27, 2025, 09:08 AM • Last activity: Apr 30, 2025, 08:14 AM
0 votes
1 answers
45 views
Using mktemp over SSH
I want to create a temp folder on a remote machine using mktemp, then move some files on the remote machine to that directory. I tried the following: ssh "$target" "mv $HOME/scripts/* $(mktemp -d -t scripts.XXXXXX)" But get an error from the remote machine that no such file was created. Any ideas?
I want to create a temp folder on a remote machine using mktemp, then move some files on the remote machine to that directory. I tried the following: ssh "$target" "mv $HOME/scripts/* $(mktemp -d -t scripts.XXXXXX)" But get an error from the remote machine that no such file was created. Any ideas?
EmberNeurosis (5 rep)
Apr 12, 2025, 07:11 PM • Last activity: Apr 12, 2025, 07:26 PM
8 votes
1 answers
3358 views
Why is mktemp -u considered "unsafe"?
I read the `--help` text for `mktemp` recently (the `man` page wasn't available) and came upon this: -u, --dry-run do not create anything; merely print a name (unsafe) Why is this "unsafe"? Is there any specific reason for this being marked as such?
I read the --help text for mktemp recently (the man page wasn't available) and came upon this: -u, --dry-run do not create anything; merely print a name (unsafe) Why is this "unsafe"? Is there any specific reason for this being marked as such?
S.S. Anne (552 rep)
Sep 8, 2019, 07:50 PM • Last activity: Mar 25, 2025, 02:29 PM
0 votes
2 answers
48 views
Building a sample tree using `mktemp` in a script produces errors "mktemp: failed to create file via template" and " ambiguous redirect"
I'm trying to make a test case for an NFS-related bug, where [`rm -rf` fails with "Directory not empty"][1] when it is empty whenever I inspected it. However a simple test script cannot reproduce the error, so I tried to write a script that creates some directory tree, populates it with files, and t...
I'm trying to make a test case for an NFS-related bug, where rm -rf fails with "Directory not empty" when it is empty whenever I inspected it. However a simple test script cannot reproduce the error, so I tried to write a script that creates some directory tree, populates it with files, and then removes the tree. As I suspect the problem being related to some filename caching, I'm using mktemp to create random names like this: ~~~lang-bash #!/bin/sh set -x readonly LEVELS=6 readonly BASE_DIR=test-dir DIR="$BASE_DIR" for (( level=0; level 0 )); then DIR="$(mktemp -p "$DIR")" || exit else mkdir "$DIR" || exit fi for (( i=0; i $(mktemp -p "$DIR") || exit done sleep 0.5 done rm -rf "$BASE_DIR" ~~~ However the script fails in a way I don't understand, like this: ~~~lang-bash > ./test-script.sh + readonly LEVELS=6 + LEVELS=6 + readonly BASE_DIR=test-dir + BASE_DIR=test-dir + DIR=test-dir + (( level=0 )) + (( level 0 )) + mkdir test-dir + (( i=0 )) + (( i 0 )) ++ mktemp -p test-dir + DIR=test-dir/tmp.iIbUu2ZDDN + (( i=0 )) + (( i ll test-dir/ total 16 -rw------- 1 windl dvmed 32 Mar 13 12:03 tmp.gbslFgK2eD -rw------- 1 windl dvmed 0 Mar 13 12:03 tmp.iIbUu2ZDDN -rw------- 1 windl dvmed 32 Mar 13 12:03 tmp.lR5NM8zLtb -rw------- 1 windl dvmed 32 Mar 13 12:03 tmp.p5Xcfqa3Xr -rw------- 1 windl dvmed 32 Mar 13 12:03 tmp.YRELcRCk6G ~~~ My guess is that the "ambiguous redirect" is due to mktemp failing to create a valid filename.
U. Windl (1715 rep)
Mar 13, 2025, 11:38 AM • Last activity: Mar 13, 2025, 11:56 AM
27 votes
2 answers
4085 views
Why is there no mktemp command in POSIX?
One of the most common things shell scripts need to do is to create and manipulate temporary files. Doing so safely is a pain, since you need to avoid name clashes, avoid race conditions, make sure the file has the correct permissions, etc. (See the [GNU Coreutils manual](https://www.gnu.org/softwar...
One of the most common things shell scripts need to do is to create and manipulate temporary files. Doing so safely is a pain, since you need to avoid name clashes, avoid race conditions, make sure the file has the correct permissions, etc. (See the [GNU Coreutils manual](https://www.gnu.org/software/coreutils/manual/html_node/mktemp-invocation.html#mktemp-invocation) and [this Signs of Triviality blog post](https://www.netmeister.org/blog/mktemp.html) for a more detailed discussion of these issues.) Most Unix-like operating systems solve this problem by providing a mktemp command that takes care of all these gotchas. However, [the syntax and semantics of these mktemp commands are not standardized](https://stackoverflow.com/a/2792789/906070) . If you really want to create a temp file both safely _and_ portably, you have to resort to [ugly kludges](https://unix.stackexchange.com/a/181996/37849) such as the following:
tmpfile=$(
  echo 'mkstemp(template)' |
    m4 -D template="${TMPDIR:-/tmp}/baseXXXXXX"
) || exit
(This workaround exploits the fact that the macro processor m4 is part of POSIX, and m4 exposes the C standard library function mkstemp() which is also defined by POSIX.) Given all this, why hasn't POSIX standardized a mktemp command, guaranteeing its presence and at least certain aspects of its behaviour? Is this a glaring oversight on the part of the POSIX committee, or has the idea of standardizing a mktemp actually been discussed by the committee and rejected for some technical or other reason?
Psychonaut (974 rep)
Oct 16, 2020, 09:17 AM • Last activity: Feb 28, 2025, 09:00 AM
0 votes
2 answers
135 views
"Traditionally, many shell scripts take the name of the program with the pid as a suffix and use that as a temporary file name"
`man mktemp`: > The mktemp utility is provided to allow shell scripts to safely use temporary files. **Traditionally, many shell scripts take the name of the program with the pid as a suffix and use that as a temporary file name.** This kind of naming scheme is predictable and the race condition it...
man mktemp: > The mktemp utility is provided to allow shell scripts to safely use temporary files. **Traditionally, many shell scripts take the name of the program with the pid as a suffix and use that as a temporary file name.** This kind of naming scheme is predictable and the race condition it creates is easy for an attacker to win. Could anybody show an example of such a file name? And where can I find such filenames "in real life"?
jsx97 (1347 rep)
Aug 6, 2024, 08:51 AM • Last activity: Aug 6, 2024, 04:13 PM
0 votes
1 answers
357 views
How many "X"'s should I use to create temporary files safely and portably with `mktemp`?
From time to time, I create temporary files from shell scripts by invoking the command `mktemp` with 6 or more `X`'s in the argument `TEMPLATE`. However, I am unsure if that amount will work with other implementations of `mktemp` because I have found commands with different amounts (e.g., [here](htt...
From time to time, I create temporary files from shell scripts by invoking the command mktemp with 6 or more X's in the argument TEMPLATE. However, I am unsure if that amount will work with other implementations of mktemp because I have found commands with different amounts (e.g., [here](https://github.com/moby/moby/blob/1ffc120558bdfd528fc5740d6b6ea7c49f402639/Makefile#L252) , [here](https://github.com/Asana/kubernetes/blob/486ec2b7c9f95a43a856d1d71077382bf6b23afa/cluster/common.sh#L512) , and [here](https://github.com/torvalds/linux/blob/f5837722ffecbbedf1b1dbab072a063565f0dad1/tools/testing/selftests/net/big_tcp.sh#L7)) . I have researched a little bit and found the following: - The GNU implementation of mktemp requires three or more X's. - The OpenBSD implementation of mktemp appears to require six X's. However, I have little to no clue what the [underlying code](https://github.com/openbsd/src/blob/master/usr.bin/mktemp/mktemp.c#L80) does. - The manuals for the FreeBSD implementation of the [mktemp](https://man.freebsd.org/cgi/man.cgi?query=mktemp&sektion=1&n=1) command and [function](https://man.freebsd.org/cgi/man.cgi?query=mktemp&sektion=3&apropos=0&manpath=FreeBSD+14.0-RELEASE+and+Ports) are rather vague with how many X's the template should have.
qqqq (56 rep)
Dec 28, 2023, 09:47 PM • Last activity: Dec 29, 2023, 10:39 PM
3 votes
1 answers
1598 views
Why creating files in /dev/shm is not faster than creating files in /tmp?
Let's say I have two bash files. The first one is called `diskFile.bash`: ```bash for i in {1..10000} do file=$(mktemp) cat > $file $file <<- "SIGN" "Hello World" SIGN done echo "finished" ``` On Linux, files created inside `/dev/shm` are files inside the RAM memory of the system. But if I try execu...
Let's say I have two bash files. The first one is called diskFile.bash:
for i in {1..10000}
do
	file=$(mktemp)
	cat > $file  $file <<- "SIGN"
		"Hello World"
SIGN
done
echo "finished"
On Linux, files created inside /dev/shm are files inside the RAM memory of the system. But if I try executing the first file with time bash diskFile.bash, I get:
finished
15.56user 5.74system 0:20.25elapsed 105%CPU (0avgtext+0avgdata 6324maxresident)k
And with time bash ramFile.bash:
finished
15.20user 5.37system 0:19.45elapsed 105%CPU (0avgtext+0avgdata 6380maxresident)
The difference of time doesn't look like a significant one considering /dev/shm should be using RAM memory. Why creating temporary files inside /dev/shm is not faster than creating temporary files inside /tmp? Is there any way of speeding up the process of creating files using bash?
raylight (531 rep)
Mar 22, 2022, 03:28 PM • Last activity: Nov 27, 2023, 08:35 PM
-1 votes
1 answers
79 views
bash script explanation
I have below script which I am unable to understand. Can someone explain the same. ``` sh #!/bin/sh skip=14 tmpdir=`/bin/mktemp -d ${TMPDIR:-/tmp}/gzexe.XXXXXXXXXX` || exit 1 prog="${tmpdir}/`echo \"$0\" | sed 's|^.*/||'`" if /usr/bin/tail -n +$skip "$0" | "/bin"/gzip -cd > "$prog"; then /bin/chmod...
I have below script which I am unable to understand. Can someone explain the same.
sh
#!/bin/sh
skip=14
tmpdir=/bin/mktemp -d ${TMPDIR:-/tmp}/gzexe.XXXXXXXXXX || exit 1

prog="${tmpdir}/echo \"$0\" | sed 's|^.*/||'"

if /usr/bin/tail -n +$skip "$0" | "/bin"/gzip -cd > "$prog"; then
  /bin/chmod 700 "$prog"
  trap '/bin/rm -rf $tmpdir; exit $res' EXIT
  "$prog" ${1+"$@"}; res=$?
else
  echo "Cannot decompress $0"
  /bin/rm -rf $tmpdir
  exit 1
fi;
exit $res
kcmakwana (37 rep)
Mar 16, 2023, 04:23 AM • Last activity: Mar 16, 2023, 05:29 AM
1 votes
1 answers
62 views
Redirect spawned process to tempfile with the pid in its filename
Suppose `myprogram` is spawned through the terminal (bash) and gets pid of 1234 (different everytime). I want to redirect, both stdout and stderr, to a tempfile named `abc-$PID` (if the PID is 1234, use tempfile `abc-1234`. The code looks like this now: ``` myprogram > /tmp/abc-$! 2>&1 ``` it doesn'...
Suppose myprogram is spawned through the terminal (bash) and gets pid of 1234 (different everytime). I want to redirect, both stdout and stderr, to a tempfile named abc-$PID (if the PID is 1234, use tempfile abc-1234. The code looks like this now:
myprogram > /tmp/abc-$! 2>&1
it doesn't work Please help.
sudoer (65 rep)
Oct 29, 2022, 12:28 AM • Last activity: Oct 29, 2022, 01:11 AM
0 votes
2 answers
253 views
How to design reusable script handling temporary files lifecycle for other scripts
I need to create a reusable ("utility") script handling entire set of operations with temporary files for any of my other ("application") scripts: 1. creation 2. tracking of temporaries created 3. trapping exit (regular completion and interruption by user) and removing everything created Each of app...
I need to create a reusable ("utility") script handling entire set of operations with temporary files for any of my other ("application") scripts: 1. creation 2. tracking of temporaries created 3. trapping exit (regular completion and interruption by user) and removing everything created Each of application scripts need to "include" utility script on their own, not rely that some "main" script will call it once for all subscripts it uses. The key requirement is that application scripts using the utility script can call each other, each of them in turn also need to use temporary files. Call between scripts are both ./some_script.sh and . ./some_script.sh Some scripts exit with error code; others just finish without explicit exit. The application scripts themselves can be called from command line using subshells, like: echo "Parameter" | ./feed_parameter_to_script.sh script_using_tmps.sh Ideally application scripts could invoke creating temporaries from subshells within its own code, but that's not mandatory. The only solution I found is [for each script to perform its tmp removal on its own](https://superuser.com/a/1226160) , which does not fit item 3. in my requirements. What is best practice for requirements like mine?
wass rubleff (163 rep)
Jul 20, 2022, 02:03 PM • Last activity: Jul 20, 2022, 04:50 PM
0 votes
1 answers
3621 views
Using mktemp in makefile
I want to create a temporary file and pipe some text into it using a makefile. In bash, I can create a temp file and pipe text into it like so: temp_file=$(mktemp) echo "text goes into file" > ${temp_file} cat ${temp_file} rm ${temp_file} Output when running it (as expected): text goes into file Whe...
I want to create a temporary file and pipe some text into it using a makefile. In bash, I can create a temp file and pipe text into it like so: temp_file=$(mktemp) echo "text goes into file" > ${temp_file} cat ${temp_file} rm ${temp_file} Output when running it (as expected): text goes into file When using the same code in a makefile, I get the following output: makefile: test: temp_file=$(mktemp) echo "text goes into file" > ${temp_file} cat ${temp_file} rm ${temp_file} $make test echo "text goes into file" > /bin/sh: -c: line 1: syntax error near unexpected token newline' /bin/sh: -c: line 1: echo "text goes into file" > ' make: *** [makefile:18: test] Error 2 Any idea what I'm doing wrong here or if I'm missing any special makefile syntax rules?
karpfen (155 rep)
Feb 17, 2021, 10:29 AM • Last activity: Dec 9, 2021, 05:25 AM
0 votes
1 answers
307 views
How to safely create file in bash
I have a script that creates a temporary file as a flag to guard against the script being run simultaneously. Currently it uses `tempfile`, e.g. ```bash if ! tempfile -n /tmp/updating > /dev/null; then echo 'Another synchronization is currently running' >&2 exit 1 fi ``` The `tempfile` program is no...
I have a script that creates a temporary file as a flag to guard against the script being run simultaneously. Currently it uses tempfile, e.g.
if ! tempfile -n /tmp/updating > /dev/null; then
	echo 'Another synchronization is currently running' >&2
	exit 1
fi
The tempfile program is now deprecated, it suggests using mktemp instead, but mktemp doesn't seem to have an option similar to -n. I'm using Ubuntu 21.04. So how should I safely create a flag file?
Steve Piner (41 rep)
Sep 26, 2021, 08:26 AM • Last activity: Sep 26, 2021, 10:24 AM
0 votes
1 answers
340 views
mkstemp failing with RHEL 8 OS non-root user
I have small script which creates a tmp files using mkstemp. My script was created using root user , but if I logged in as someother user other than root then my scripts runs but msktemp fails and says permission denied > sh: /tmp/tmpFile-khB5hH: Permission denied Same thing while doing in RHEL 7 wo...
I have small script which creates a tmp files using mkstemp. My script was created using root user , but if I logged in as someother user other than root then my scripts runs but msktemp fails and says permission denied > sh: /tmp/tmpFile-khB5hH: Permission denied Same thing while doing in RHEL 7 works and no Permission denied error, what is changed in RHEL 8. looks like RHEL 8 is checking logged on user vs effective user. I tested this up in RHEL 7.5 and RHEL 8.4. If someone know change in RHEL 8 which leads to permission problems please suggest. Thanks
cjoshi (1 rep)
Aug 13, 2021, 04:33 PM • Last activity: Aug 17, 2021, 12:01 PM
7 votes
3 answers
1876 views
Is there some interactive analogue of `mktemp` that helps to organize throw-away directories?
I often want a temporary directly where I can unpack some archive (or create a temporary project), see around some files. It is unpredictable in advance for how much time a particular directory may be needed. Such directories are often clutter home directory, `/tmp`, project directories. They often...
I often want a temporary directly where I can unpack some archive (or create a temporary project), see around some files. It is unpredictable in advance for how much time a particular directory may be needed. Such directories are often clutter home directory, /tmp, project directories. They often have names like some weak passwords, like qqq, 1, test that become undescriptive a month after. Is there some shell command or external program that can help manage such throw-away directories, so that they get cleaned up automatically when I lose interest in them, where I don't need to invent a name for them, but that can be given a name and made persistent easily? If there is no such tool, is it a good idea to create one?
Vi. (5985 rep)
Jul 13, 2021, 10:30 PM • Last activity: Jul 21, 2021, 09:27 AM
4 votes
2 answers
4297 views
How to create a temporary file that has "normal" permissions?
In a build script I want to create a temporary file that has a unique generated name, then write some data to it, and then rename that file to its final name (the reason for this procedure is that I want the renaming to happen (mostly) atomic). But if I use `mktemp`, the new file will have very rest...
In a build script I want to create a temporary file that has a unique generated name, then write some data to it, and then rename that file to its final name (the reason for this procedure is that I want the renaming to happen (mostly) atomic). But if I use mktemp, the new file will have very restrictive permissions: > Files are created u+rw, and directories u+rwx, minus umask > restrictions. ... and these permissions will be preserved when the file is renamed. However, I want the resulting file to have "normal" permissions; that is, it should have the same permissions as files created by touch or by a shell redirection. What is the recommended way to achieve this? Is there a (common) alternative to mktemp; or is there a simple and reliable way to change the file permissions to the normal umask permissions afterwards?
oliver (468 rep)
May 25, 2021, 09:18 AM • Last activity: May 25, 2021, 03:05 PM
1 votes
1 answers
783 views
mktemp creates file in pwd rather than in /tmp
Previously mktemp used to create files in `/tmp`. Today I noticed that mktemp is creating file in `$PWD` rather than in `/tmp`? ### Did I change something inadvertently? Any suggestions to fix this? --- Note: I am able to do: `ls /tmp` ``` $ mktemp --version mktemp (GNU coreutils) 8.30 ``` ``` $ ech...
Previously mktemp used to create files in /tmp. Today I noticed that mktemp is creating file in $PWD rather than in /tmp? ### Did I change something inadvertently? Any suggestions to fix this? --- Note: I am able to do: ls /tmp
$ mktemp --version
mktemp (GNU coreutils) 8.30
$ echo $TMPDIR

$
Porcupine (2156 rep)
May 6, 2021, 05:30 PM • Last activity: May 6, 2021, 08:23 PM
13 votes
1 answers
3986 views
mktemp on macOS not honouring $TMPDIR
I've noticed this before, but it was brought up again as I was answering "https://unix.stackexchange.com/questions/555047": The `mktemp` utility on macOS does not behave the same as the utility of the same name on Linux or BSD (or least OpenBSD) with respect to the `TMPDIR` environment variable. To...
I've noticed this before, but it was brought up again as I was answering "https://unix.stackexchange.com/questions/555047 ": The mktemp utility on macOS does not behave the same as the utility of the same name on Linux or BSD (or least OpenBSD) with respect to the TMPDIR environment variable. To create a temporary file in the _current_ directory, I can usually say
-sh
tmdfile=$(TMPDIR=. mktemp)
or
-sh
tmpfile=$(TMPDIR=$PWD mktemp)
(and similarly for a temporary directory with mktemp -d). On macOS, I will have to force the utility to use the current directory by giving it an actual template, as in
-sh
tmpfile=(mktemp ./tmp.XXXXXXXX)
because using the more convenient tmpfile=$(TMPDIR=. mktemp) would ignore the TMPDIR variable and create the file under /var/folders/qg/s5jp5ffx2p1fxv0hy2l_p3hm0000gn/T or in a similarly named directory. The manual for mktemp on macOS mentions that >If the -t prefix option is given, mktemp will generate a template string based on the prefix and the _CS_DARWIN_USER_TEMP_DIR configuration variable if available. Fallback locations if _CS_DARWIN_USER_TEMP_DIR is not available are TMPDIR and /tmp. On my system, _CS_DARWIN_USER_TEMP_DIR appears to be unset:
-none
$ getconf _CS_DARWIN_USER_TEMP_DIR
getconf: no such configuration parameter `_CS_DARWIN_USER_TEMP_DIR'
but e.g.
-sh
tmpfile=$(TMPDIR=. mktemp -t hello)
still creates a file under /var/folders/.../ (also when using $PWD in place of .). I'm noticing that
-none
$ getconf DARWIN_USER_TEMP_DIR
/var/folders/qg/s5jp5ffx2p1fxv0hy2l_p3hm0000gn/T/
but this doesn't help me much as I wouldn't know how to change this value. The macOS mktemp utility is said to come from FreeBSD, which in turn got it from OpenBSD (which must have been quite a while ago). Question: Is this a bug (or omission) in the macOS implementation of mktemp? How do I change the DARWIN_USER_TEMP_DIR value (or _CS_DARWIN_USER_TEMP_DIR mentioned by the manual) from within a script (I would ideally want to unset it so that $TMPDIR takes precedence)?
Kusalananda (354278 rep)
Dec 1, 2019, 03:03 PM • Last activity: Dec 2, 2019, 04:25 PM
2 votes
2 answers
2431 views
Improving mktemp
I am wondering how to best improve on `mktemp` for use with encrypted containers or file systems. The issue that I am dealing with is that I would like my shell scripts to store temporary files inside the file system that contains the working directory, if possible. The normal behaviour of `mktemp`...
I am wondering how to best improve on mktemp for use with encrypted containers or file systems. The issue that I am dealing with is that I would like my shell scripts to store temporary files inside the file system that contains the working directory, if possible. The normal behaviour of mktemp seems to be to use a root path specified in an environment variables or /tmp. This will, however, routinely leak temporary data to unencrypted locations if I am working with files inside encrypted containers. The idea is to first check the presence of a tmp directory in the mount point of the current file system and to use /tmponly as a last resort. How can I reliably (and efficiently) realize that. **Edit** A possible way to identify the mount directory of a given path is the following dir=realpath [path]; res=1; while [ $res -ne 0 ]; do dir="${dir%/*}"; mountpoint -q "$dir/"; res=$?; done; echo "$dir"; I am not sure, however, if that is the most efficient one.
highsciguy (2614 rep)
Jan 5, 2018, 09:48 PM • Last activity: Apr 11, 2019, 04:36 AM
0 votes
1 answers
638 views
Deleting temporary direction via trap
I have a script which looks like this: ``` #!/bin/bash set -e tmpdir=$(mktemp -d) pushd $tmpdir trap 'popd && rm -rf $tmpdir' EXIT # Business logic which may succeed or fail... /bin/false ``` When it exits, it `popd`s just fine, but it doesn't remove the temporary directory. It seems that `$tmpdir`...
I have a script which looks like this:
#!/bin/bash
set -e 

tmpdir=$(mktemp -d)
pushd $tmpdir
trap 'popd && rm -rf $tmpdir' EXIT

# Business logic which may succeed or fail...
/bin/false
When it exits, it popds just fine, but it doesn't remove the temporary directory. It seems that $tmpdir isn't resolved in the trap, but how can we overcome that? I verified that $tmpdir isn't resolved by rerunning the script without -rf in the rm. That exited with: rm: missing operand instead of the expected rm: cannot remove '/tmp/tmp.Y1SdoY5dSu/': Is a directory
Stewart (15631 rep)
Mar 28, 2019, 10:28 AM • Last activity: Mar 28, 2019, 11:06 AM
Showing page 1 of 20 total questions