BASH Shell: HowTo Create Empty Temporary Files Quickly

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:



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:


Sample outputs:


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

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

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


  • man page – mktemp and bash

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 12 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
12 comments… add one
  • Nathan Jul 27, 2007 @ 20:13

    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

  • me Aug 1, 2007 @ 21:28

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

  • artiomix Aug 2, 2007 @ 14:29

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

  • pooran mal Dec 3, 2007 @ 15:03

    how we create a empty shell??

  • amol Sep 11, 2008 @ 1:16

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

  • Philippe Feb 3, 2010 @ 14:10

    Hi Vivek,

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

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

    TIA !

  • 🐧 nixCraft Feb 3, 2010 @ 15:15

    @Nathan/ Philippe,

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

  • David Ramirez Feb 21, 2014 @ 19:04

    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…

  • David Ramirez Feb 21, 2014 @ 19:16

    I answer myself for a workaround :

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

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

  • Linuxer Nov 7, 2014 @ 15:19

    You can avoid the call of mktemp for each found file; just ‘dd’ from and to the same file; usually reading and writing to the same file results in an empty file…

    find . -maxdepth 1 -name bla -exec dd if={} of={} 2>/dev/null ;

  • frodo1 Sep 9, 2015 @ 8:55

    A practical one: do the backup of f1 otherwise make a new one

    date="$(date '+%d%m%y%H%M')";
    if [ -n "${f1}" ]
           cp -v "$f1" "$path"/"$f1"."$date".bck
            echo -n "## file made by script on date $date by $user" > "$path"/"$f1"
  • Anon Jun 21, 2016 @ 13:16

    Shortest version (using noop):


Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum