11

I want a Bash one-liner that atomically creates a file if it doesn't exist. That means essentially "if file doesn't exist, create it" but ensuring that no one else manages to create the file in the small space between doing the if and creating the file.

Ram Rachum
  • 5,231
  • 7
  • 34
  • 46

4 Answers4

8

Stealing an answer from various comments and links to [SO]. It seems there is a POSIX compliant method that doesn't involve mkdir as I mentioned in my original answer below

set -o noclobber # or set -C
{ > file ; } &> /dev/null

This redirect to file returns 0 or fails and returns non-zero if the file already exists.


Original answer

You'll have to use mkdir - that's atomic, either the directory gets created and you can continue or it doesn't created so you take appropriate action.

Of course, mkdir doesn't create a file but once you know you have exclusive access to the directory then you can make the file you want in it.

As to a one liner - I'll leave that up to you. Personally I'd write it over a few lines, as that'll be more maintainable.

quadruplebucky
  • 5,139
  • 20
  • 23
user9517
  • 115,471
  • 20
  • 215
  • 297
1

Try this one. The ln provides the test-and-set functionality.

touch lock.$$.tmp
if ln lock.$$.tmp lock.dat 2>/dev/null
then
    echo "File is mine"
else
    echo "Test and set failed"
fi
rm -f lock.$$.tmp
roaima
  • 1,591
  • 14
  • 28
0

Is it file or directory? If file, you can use simple command touch - if file exists, it just modify last access time. If file doesn't exists, is created.

Ondra Sniper Flidr
  • 2,653
  • 12
  • 18
0

If you don't care about the filename, you can delegate this task to a utility, for example mktemp

Create a temporary file or directory, safely, and print its name.
TEMPLATE must contain at least 3 consecutive `X's in last component.
If TEMPLATE is not spec‐ified, use tmp.XXXXXXXXXX, and --tmpdir is 
implied.  Files are created u+rw, and directories u+rwx, minus umask
restrictions.

Invocation:

filename=$(mktemp)
asdmin
  • 2,050
  • 17
  • 28