Fluent::Programmer

    Home Blog About
  • Home
  • Blog
  • About

How to create a temporary file on Linux using Bash? 👋

  • Fluent Programmer
  •   3 min read

Quick summary ↬  Depending on the OS the command varies. mktemp and tempfile are the commands used in the Bash script to create temporary file - it is easy, readily available and simple to use.

Temporary file creation in Bash

It is must to have feature in all the programming languages - an ability to create temporary files or intermediary file. These files are called temporary files but that does not mean that it will be deleted from the disk after a time period. In Linux, based on the distribution we have two commands to create temporary files - mktemp and tempfile.

In this article we explore ways to (1) how to create temporary file, (2) how to store the name of the temporary file created in a variable, (3) how to customize the name of temporary file by adding prefix and suffix, (4) tempfile as touch or vim, (5) how to create a directory with mktemp and tempfile.

How to create a temporary file?

1
2
$ tempfile
/tmp/R4FkiHL

Simple, right? If you din’t specify a directory name the tempfile command will create a temporary file in the /tmp/ directory.

Now let’s try mktemp.

1
2
$ mktemp
/tmp/tmp.HtOPhjkH

How to create a temporary file inside a directory of your wish?

To create a file inside a directory specify the directory name as shown below:

With mktemp use the -p option:

1
2
$ mktemp -p ~/dirname
/home/fluentprogrammer/dirname/kldyuonYUkh

With tempfile use the –directory or -d option:

1
2
3
4
5
$ tempfile --directory ~/dirname
/home/fluentprogrammer/dirname/kldyuonYUkh

$ tempfile -d ~/dirname
/home/fluentprogrammer/dirname/kldyuonYUkh

Make sure the directory is already present or else it will show an error as shown below

1
2
$ tempfile -p ~/dirname
mkstemps: No such file or directory

How to customize the temporary file names?

Pretty simple. –prefix and –suffix are used to customize the name of the file as shown below

1
2
3
4
5
$ mktemp -p ~/dirname/ --suffix .txt
/home/fluentprogrammer/dirname/HKhjegKHGGK.txt

$ tempfile --directory ~/dirname/ --prefix howto_ --suffix .txt
/home/fluentprogrammer/dirname/howto_hksgJKGGK.txt

But if we mention the name of the file explicitly then –prefix and –suffix does not work.

1
2
$ tempfile --name hello_i_am_explicitly_mentioned --prefix howto_ --suffix .txt
/tmp/hello_i_am_explicitly_mentioned

How to find the name of the temporary file?

mktemp and tempfile are good to create temporary files inside the /tmp/ as well as inside the directory of your wish but it is not returning the name of the file. If we want to use this comment in the bash script then it should be having the capability to return the file name. And yeah, It can return back the file name after creation. Check the below code,

1
2
3
4
5
6
7
8
$ TMPFILE=$(mktemp -p ~/dirname)
$ echo $TMPFILE
/home/fluentprogrammer/dirname/HKhjegKHGGK


$ TMPFILE=$(tempfile -d ~/dirname)
$ echo $TMPFILE
/home/fluentprogrammer/dirname/jkhjttYKHHQ

Conclusion

We used tmpfile and mktemp to create temporary files, customize it, store the file name in a variable so we could use it in subsequent commands in the bash script, etc.

References

[1] https://man7.org/linux/man-pages/man3/tmpfile.3.html

[2] https://man7.org/linux/man-pages/man3/mktemp.3.html

[3] https://opensource.com/article/22/6/make-temporary-file-bash

About The Author

Fluentprogrammer doesn't need coffee to program. They run on pure caffeine and lines of code.

Email Newsletter

Table of Contents

  • Temporary file creation in Bash
    • How to create a temporary file?
    • How to create a temporary file inside a directory of your wish?
    • How to customize the temporary file names?
    • How to find the name of the temporary file?
  • Conclusion
  • References
  • C++
  • Beautiful code series

Unhealthy love with dark corners of C++

Founded by an engineer to help engineers. 2021–2023.

  • About us
  • Privacy policy