• Home
  • Store
  • Blog
  • Contact
  • Home
  • Store
  • Blog
  • Contact
  • #linux
  • |
  • #commandline
  • |
  • #softwareengineering
  • |
  • #embeddedsystems
  • |
  • #compilers
  • ...
  • View All >>

Intro To 'mktemp' Command In Linux

2023-05-13 - By Robert Elder

Using 'mktemp' To Create Temporary Files

     I use the 'mktemp' command to create temporary files:

mktemp

     When I run the 'mktemp' command like this:

mktemp

     it will create an empty file and then print the new file's location:

/tmp/tmp.Xga0YnlUyC
ls -l /tmp/tmp.Xga0YnlUyC
-rw------- 1 robert robert 0 May 11 15:48 /tmp/tmp.Xga0YnlUyC

Using 'mktemp' To Create Temporary Files In Other Directories

     By default, the empty file will be created in the '/tmp' directory, but you can use the '-p' flag to specify another directory:

mktemp -p /home/robert/important
/home/robert/important/tmp.7hmhLN9sGO

     Now, checking the directory '/home/robert/important', you can see that the temp file was created in this location:

ls -l /home/robert/important
-rw------- 1 robert robert 0 May 11 16:10 tmp.7hmhLN9sGO

Example Use Case Of 'mktemp' - Creating Receipt Documents

     The 'mktemp' command is particularly useful for writing automated scripts.  For example, here, I've made a script that generates receipt documents:

#!/bin/bash

TMP_PS_FILE=$(mktemp -p ./)
echo "Using TMP_PS_FILE=$TMP_PS_FILE"

echo "What is the product price?"
read price
echo "What is the product quantity?"
read quantity

echo '%!PS' >> $TMP_PS_FILE
echo '/Courier 30 selectfont' >> $TMP_PS_FILE
echo '100 400 moveto' >> $TMP_PS_FILE
echo '(Robert Elder Software Inc.) show' >> $TMP_PS_FILE
echo '100 500 moveto' >> $TMP_PS_FILE
echo "(QTY: $quantity @\$${price}.00) show" >> $TMP_PS_FILE
echo '100 600 moveto' >> $TMP_PS_FILE
echo '(Thank You.  Come again.) show' >> $TMP_PS_FILE
echo 'showpage' >> $TMP_PS_FILE

ps2pdf "$TMP_PS_FILE" "$TMP_PS_FILE.pdf"
rm "$TMP_PS_FILE"

echo "Pdf receipt is found at $TMP_PS_FILE.pdf"

     This script uses the 'mktemp' command to generate a temporary file that will store a PostScript document describing our receipt.  The script then transforms the PostScript document into the PDF document that we want. The temporary PostScript file is then deleted.

     Let's try running this script now:

./receipt-generator.sh

     and then enter the following values when prompted:

69
420

     The temporary file has now been deleted, and the resulting receipt document can be seen here:

     And that's why the 'mktemp' command is my favourite Linux command.

Intro To 'stty' Command In Linux
Published 2023-10-04
$1.00 CAD
Terminal Block Mining Simulation Game
Intro To 'nproc' Command In Linux
Published 2023-07-15
Intro To 'comm' Command In Linux
Published 2023-09-06
How To Force The 'true' Command To Return 'false'
Published 2023-07-09
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01
Intro To 'chroot' Command In Linux
Published 2023-06-23
Join My Mailing List

Privacy Policy
Why Bother Subscribing?
  • Free Software/Engineering Content. I publish all of my educational content publicly for free so everybody can make use of it.  Why bother signing up for a paid 'course', when you can just sign up for this email list?
  • Read about cool new products that I'm building. How do I make money? Glad you asked!  You'll get some emails with examples of things that I sell.  You might even get some business ideas of your own :)
  • People actually like this email list. I know that sounds crazy, because who actually subscribes to email lists these days, right?  Well, some do, and if you end up not liking it, I give you permission to unsubscribe and mark it as spam.
© 2025 Robert Elder Software Inc.
Privacy Policy      Store Policies      Terms of Use