BASH shell: How to create empty temporary files quickly
Use the any one of the following command to create empty files. 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
$ touch > /tmp/newfilename
$ echo "" > /tmp/filename
You may also be interested in other helpful articles:
- Shell scripting (BASH) : How to create temporary random file name
- Shell tip: Clear the command history and screen when you log out
- Temporary memory files and shell scripts
- BASH Shell Frequently Asked Questions
- How to keep files safe from accidental overwriting with noclobber under BASH shell
Discussion on This Article:
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!



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
use “mktemp /tmp/tempfile.XXXXXXXXX” to make a secure tempory file and avoid exploits
mktemp! wow! Some time ago I wrote my own utility which do the same as mktemp.. shit
how we create a empty shell??