BASH Shell: HowTo Create Empty Temporary Files Quickly

by on March 23, 2005 · 9 comments· LAST UPDATED February 3, 2010

in

Use any one of the following command to create temporary empty file names. The first command is special as it use the redirection operator >, the redirection refers to the standard output. So you are creating a new file or destroying existing file:

>/tmp/filename

OR

echo -n "" > /tmp/filename

The touch command can be also used to create temporary empty file names:

touch /tmp/newfilename

mktemp Command

To make temporary unique filename use the mktemp command. In this example, create temporary filename in using a user's $TMPDIR environment variable:

mktemp

Sample outputs:

/tmp/tmp.yTfJX35144

Use /tmp/tmp.yTfJX35144 to store your output. You can store filename to a variable:

OUT="$(mktemp)"
ls > $OUT

The following bash scripting illustrates a simple use of mktemp where the script should quit if it cannot get a safe temporary file

#!/bin/bash
OUT=$(mktemp /tmp/output.XXXXXXXXXX) || { echo "Failed to create temp file"; exit 1; }
echo "Today is $(date)"  >> $OUT

The mktemp utility takes the given filename template and overwrites a portion of it to create a unique filename. The template may be any filename with some number of 'Xs' appended to it, for example /tmp/tfile.XXXXXXXXXX.

TMPDIR Environment Variable

By default mktemp will use user's $TMPDIR. If not defined it will use /tmp. You can use the specified directory as a prefix when generating the temporary filename. The directory will be overridden by the user's TMPDIR environment variable if it is set. In this example the temporary file will be created in /chroot/apache/var/tmp unless the user's TMPDIR environment variable specifies otherwise:

mktemp -p /chroot/apache/var/tmp php.lock.XXXXXXXXXX

References:

  • man page - mktemp and bash
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 9 comments… read them below or add one }

1 Nathan July 27, 2007 at 8:13 pm

The first command works fine. It creates a neat and empty file.

Second command does not work, “touch” is a command the file is the only parameter. It should not use a redirect as shown above.

touch /tmp/newfilename

Since this is a command it also works consistently no matter what shell interpreter someone might be using.

The third one is missing a -n on the echo to prevent the newline from being sent to the file making it less than empty.

echo -n “” > /tmp/filename

Reply

2 me August 1, 2007 at 9:28 pm

use “mktemp /tmp/tempfile.XXXXXXXXX” to make a secure tempory file and avoid exploits

Reply

3 artiomix August 2, 2007 at 2:29 pm

mktemp! wow! Some time ago I wrote my own utility which do the same as mktemp.. shit

Reply

4 pooran mal December 3, 2007 at 3:03 pm

how we create a empty shell??

Reply

5 amol September 11, 2008 at 1:16 am

mktemp is wicked neat… just what I needed when my google search landed on this page. thanks!

Reply

6 Philippe February 3, 2010 at 2:10 pm

Hi Vivek,

Nathan is right.
Why don’t you change for correct entries :
touch > /tmp/newfilename
should be
touch /tmp/newfilename

and
echo "" > /tmp/filename
should be
echo -n "" > /tmp/filename

TIA !

Reply

7 nixCraft February 3, 2010 at 3:15 pm

@Nathan/ Philippe,

Thanks for the heads up. The post has been updated.

Reply

8 David Ramirez February 21, 2014 at 7:04 pm

Cool tips in this post!…
I also have used:

cat /file_to_be_emptied

but if I want to feed it (or any other of the examples above) as argument to find … -exec the “>” is interpreted as redirection, such as:

find . -name ‘file_to_be_emptied’ -exec echo “” > {} \;

Escaping the ‘>’ doesn’t help. (find throws error).

I want to submit that via cron and is not working. A zero-size file named ‘{}’ is created.

Thanks for any hints…

Reply

9 David Ramirez February 21, 2014 at 7:16 pm

I answer myself for a workaround :

find . -name ‘file_to_be_emptied’ -exec cp `mktemp` {} \;

At least under Linux (Fedora) it works for me.

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , ,

Previous post:

Next post: