Create a temporary file with Bash on Linux | Linux United Kingdom

Takeaway: The mktemp command on Fedora-based systems and tempfile on Debian-based systems are designed to ease this burden, making it easy to create, use, and delete unique files.

The number of words in this article: 2700, the reading time is about 3 minutes

The mktemp command on Fedora-based systems and tempfile on Debian-based systems are designed to ease this burden, making it easy to create, use, and delete unique files.

When programming with the Bash scripting language, it is sometimes necessary to create a temporary file. For example, you might need an intermediate file that can be committed to disk so that you can process it with another command. It’s easy to create a file like temp or anything that ends in .tmp . However, these names are likely generated by other processes, so you may accidentally overwrite existing temporary files. Other than that, you shouldn’t spend your brains coming up with names that look unique. The mktemp command on Fedora-based systems and tempfile on Debian-based systems are designed to ease this burden, making it easy to create, use, and delete unique files.

Create a temporary file

Both mktemp and tempfile create a temporary file as their default action, and print the file’s name and location as output:


  1. $ tempfile

  2. /tmp/fileR5dt6r

  3. $ mktemp

  4. /tmp/tmp.ojEfvMaJEp

Unless you specify a different path, the system will place temporary files in the /tmp directory.

For mktemp , the path can be specified with the -p option:


  1. $ mktemp -p ~/Demo

  2. /home/tux/Demo/tmp.i8NuhzbEJN

For tempfile , the --directory or -d options can be used:


  1. $ tempfile --directory ~/Demo/

  2. /home/sek/Demo/fileIhg9aX

find your temp files

The problem with using an auto-generated temp file is that you have no way of knowing what its name is. That’s why both commands return the generated filename as output. You can use Konsole, GNOME Terminal or rxvt
? opensource.com
Wait for an interactive shell to interact with the file using the filename displayed on the terminal.

However, if you are writing a script, there is no way to intervene by reading the filename and using it in the following command.

The authors of mktemp and tempfile thought of this problem and have an easy fix. The terminal sends output to a stream called “standard output”. You can capture standard output by setting a variable to the result of a command launched in a subshell:


  1. $ TMPFILE=$(mktemp -p ~/Demo)

  2. $ echo $TMPFILE

  3. /home/tux/Demo/tmp.PjP3g6lCq1

$TMPFILE is used when referencing a file, it is the same as interacting with the file itself directly.

Create a temporary directory with mktemp

You can also use the mktemp command to create directories instead of files:


  1. $ mktemp --directory -p ~/Demo/

  2. /home/tux/Demo/tmp.68ukbuluqI

  3. $ file /home/tux/Demo/tmp.68ukbuluqI

  4. /home/tux/Demo/tmp.68ukbuluqI: directory

custom temporary name

Sometimes you may even want to add an element of predictability to pseudo-randomly generated filenames. You can use these two commands to customize the name of the temporary file.

With mktemp , you can add a suffix to the filename:


  1. $ mktemp -p ~/Demo/ --suffix .mine

  2. /home/tux/Demo/tmp.dufLYfwJLO.mine

With tempfile , you can set prefix and suffix:


  1. $ tempfile --directory ~/Demo/ --prefix tt_ --suffix .mine

  2. /home/tux/Demo/tt_0dfu5q.mine

use tempfile as touch

You can also set a custom name with tempfile :


  1. $ tempfile --name not_random

  2. not_random

When you use the --name option, it is absolute and all other forms of customization are ignored. In fact, it even ignores the --directory option:


  1. $ tempfile --directory ~/Demo --prefix this_is_ --suffix .all --name not_random_at

  2. not_random_at

In a way, tempfile can replace touch and test because it refuses to create files that already exist:


  1. $ tempfile --name example.txt

  2. open: file exists

The tempfile command is not installed by default on all Linux distributions, so you must make sure it exists before using it as a hack for tests in scripts.

Install mktemp and tempfile

GNU Core Utils
? www.gnu.org
Including the mktemp command. Major distributions include Core Utils by default (it’s the same package that contains chmod , cut , du and other basic commands).

The Debian Utils package contains the tempfile command and is installed by default on most Debian-based distributions and Slackware Linux.

Summarize

Temporary files are convenient because there is no confusion about whether they are safe to delete. They are temporary, intended to be used as needed and discarded without hesitation. Use them when you need them, and clear them when you’re done.

The text and pictures in this article are from Linux China

loading.gif

This article is reprinted from https://www.techug.com/post/create-a-temporary-file-with-bash-on-linux-linux-china10758afcf68fe2a44ad9/
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment