Questions tagged [mktemp]

20 questions
21
votes
2 answers

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…
Psychonaut
  • 864
  • 8
  • 17
11
votes
1 answer

mktemp on macOS not honouring $TMPDIR

I've noticed this before, but it was brought up again as I was answering "How to move directory into a directory with the same name?": The mktemp utility on macOS does not behave the same as the utility of the same name on Linux or BSD (or least…
Kusalananda
  • 320,670
  • 36
  • 633
  • 936
6
votes
3 answers

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…
Vi.
  • 5,528
  • 7
  • 34
  • 68
4
votes
1 answer

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…
S.S. Anne
  • 453
  • 3
  • 15
3
votes
0 answers

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: for i in {1..10000} do file=$(mktemp) cat > $file <<- "SIGN" "Hello World" SIGN done echo "finished" And the second one is called ramFile.bash: for i in…
raylight
  • 361
  • 1
  • 5
  • 13
3
votes
2 answers

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…
oliver
  • 428
  • 2
  • 9
3
votes
3 answers

How to create multiple files/directories with randomized names?

I'm trying to create some files that has different names and with different extensions. Let say I want to create for example 3 files with following name and extension: File001.000 File002.001 File003.002 Or with alphabetical…
αғsнιη
  • 40,939
  • 15
  • 71
  • 114
2
votes
1 answer

Is it OK to overwrite a file created by mktemp?

I'd like to copy a file to a temporary location. I'd like to make sure I'm not overwriting anything important, and that I know the location of the file while the script is running. #!/bin/bash myfile="$(mktemp)" cp "source" "$myfile" # work with…
user
  • 339
  • 1
  • 4
  • 13
2
votes
2 answers

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…
highsciguy
  • 2,534
  • 4
  • 21
  • 29
1
vote
1 answer

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…
sudoer
  • 45
  • 4
1
vote
1 answer

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…
Porcupine
  • 1,680
  • 2
  • 19
  • 45
1
vote
2 answers

Is there a programmatical difference between directories created with mktemp -d or mkdir?

I try to understand Stephen Kitt's answer to this question where he created a temporary directory with the following code: #!/bin/bash scripttmp=$(mktemp -d) # Create a temporary directory (these will usually be created under /tmp or…
user149572
1
vote
1 answer

`mktemp -d` followed by `pushd` works fine on command line, but not when in a script

If I paste these lines into a command prompt on Debian... DIR=$(mktemp -d -t bbbrtc.XXXXXX) || exit 1 echo "tmpdir = $DIR" cd "$DIR" They make a new temp directory, print the directory name, and then pushd into that…
bigjosh
  • 589
  • 2
  • 6
  • 14
0
votes
2 answers

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: creation tracking of temporaries created trapping exit (regular completion and interruption by user)…
wass rubleff
  • 163
  • 7
0
votes
1 answer

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. if ! tempfile -n /tmp/updating > /dev/null; then echo 'Another synchronization is currently running'…
1
2