How to make a unique temporary file in Bash on Linux?

It is common to make a unique temporary file. How to make a unique temporary file in Bash on Linux safely and quickly?

asked Oct 3, 2016 by anonymous

1 Answer

You can use the mktemp program.

In the simplest way,

tmpfile=$(mktemp)

The file will be like /tmp/tmp.j0wD39Mr3b if you do not prefer any meaningful names.

You can set the file to your preferred string like myapp.<6 char random string> by

tmpfile=$(mktemp -t myapp.XXXXXX)

Your temporary file will be like /tmp/myapp.V9XnA6.

You may also choose the directory where the temporary file is in by setting the -p option.

Check more about mktemp in mktemp man page.

answered Oct 8, 2016 by Eric Z Ma (44,280 points)

Please log in or register to answer this question.

Copyright © SysTutorials. User contributions licensed under cc-wiki with attribution required.
Hosted on Dreamhost

...