Using Temporary Files - Shell Scripting

The Linux system contains a special directory location reserved for temporary files. Linux uses the /tmp directory for files that don’t need to be kept indefinitely. Most Linux distributions configure the system to automatically remove any files in the /tmp directory at bootup.

Any user account on the system has privileges to read and write files in the /tmp directory.
This feature provides an easy way for you to create temporary files that you don’t necessarily have to worry about cleaning up.

There’s even a specific command to use for creating a temporary file. The mktemp command allows you to easily create a unique temporary file in the /tmp folder. The shell creates the file but doesn’t use your default umask value. Instead, it only assigns read and write permissions to the file’s owner and makes you the owner of the file. Once you create the file, you have full access to read and write to and from it from your script, but no one else will be able to access it (other than the root user of course).

Creating a local temporary file

By default, mktemp creates a file in the local directory. To create a temporary file in a local directory with the mktemp command, all you need to do is specify a filename template. The template consists of any text filename, plus six X’s appended to the end of the filename:

$ mktemp testing.XXXXXX
$ ls -al testing*
-rw------- 1 rich rich 0 Oct 29 21:30 testing.UfIi13
$

The mktemp command replaces the six X’s with a six-character code to ensure the filename is unique in the directory. You can create multiple temporary files and be assured that each one is unique:

$ mktemp testing.XXXXXX
testing.1DRLuV
$ mktemp testing.XXXXXX
testing.lVBtkW
$ mktemp testing.XXXXXX
testing.PgqNKG
$ ls -l testing*
-rw------- 1 rich rich 0 Oct 29 21:57 testing.1DRLuV
-rw------- 1 rich rich 0 Oct 29 21:57 testing.PgqNKG
-rw------- 1 rich rich 0 Oct 29 21:30 testing.UfIi13
-rw------- 1 rich rich 0 Oct 29 21:57 testing.lVBtkW
$

As you can see, the output of the mktemp command is the name of the file that it creates. When you use the mktemp command in a script, you’ll want to save that filename in a variable, so you can refer to it later on in the script:

$ cat test19
#!/bin/bash
# creating and using a temp file
tempfile=`mktemp test19.XXXXXX`
exec 3$tempfile
echo "This script writes to temp file $tempfile"
echo "This is the first line3
echo "This is the second line.3
echo "This is the last line.3
exec 3-
echo "Done creating temp file. The contents are:"
cat $tempfile
rm -f $tempfile 2/dev/null
$ ./test19
This script writes to temp file test19.vCHoya
Done creating temp file. The contents are:
This is the first line
This is the second line.
This is the last line.
$ ls -al test19*
-rwxr--r-- 1 rich rich 356 Oct 29 22:03 test19*
$

The script uses the mktemp command to create a temporary file and assigns the file name to the $tempfile variable. It then uses the temporary file as the output redirection file for file descriptor 3. After displaying the temporary file name on STDOUT, it writes a few lines to the temporary file, then it closes the file descriptor. Finally, it displays the contents of the temporary file and then uses the rm command to remove it.

Creating a temporary file in /tmp
The -t option forces mktemp to create the file in the temporary directory of the system. When you use this feature, the mktemp command returns the full pathname used to create the temporary file, not just the filename:

$ mktemp -t test.XXXXXX
/tmp/test.xG3374
$ ls -al /tmp/test*
-rw------- 1 rich rich 0 2007-10-29 18:41 /tmp/test.xG3374
$

Since the mktemp command returns the full pathname, you can then reference the temporary file from any directory on the Linux system, no matter where it places the temporary directory:

$ cat test20
#!/bin/bash
# creating a temp file in /tmp
tempfile=`mktemp -t tmp.XXXXXX`
echo "This is a test file.$tempfile
echo "This is the second line of the test.$tempfile
echo "The temp file is located at: $tempfile"
cat $tempfile
rm -f $tempfile
$ ./test20
The temp file is located at: /tmp/tmp.Ma3390
This is a test file.
This is the second line of the test.
$

When mktemp creates the temporary file, it returns the full pathname to the environment variable. You can then use that value in any command to reference the temporary file.

Creating a temporary directory

The -d option tells the mktemp command to create a temporary directory instead of a file. You can then use that directory for whatever purposes you need, such as creating additional temporary files:

$ cat test21
#!/bin/bash
# using a temporary directory
tempdir=`mktemp -d dir.XXXXXX`
cd $tempdir
tempfile1=`mktemp temp.XXXXXX`
tempfile2=`mktemp temp.XXXXXX`
exec 7$tempfile1
exec 8$tempfile2
echo "Sending data to directory $tempdir"
echo "This is a test line of data for $tempfile1" 7
echo "This is a test line of data for $tempfile2" 8
$ ./test21
Sending data to directory dir.ouT8S8
$ ls -al
total 72
drwxr-xr-x 3 rich rich 4096 Oct 29 22:20 ./
drwxr-xr-x 9 rich rich 4096 Oct 29 09:44 ../
drwx------ 2 rich rich 4096 Oct 29 22:20 dir.ouT8S8/
-rwxr--r-- 1 rich rich 338 Oct 29 22:20 test21*
$ cd dir.ouT8S8
[dir.ouT8S8]$ ls -al
total 16
drwx------ 2 rich rich 4096 Oct 29 22:20 ./
drwxr-xr-x 3 rich rich 4096 Oct 29 22:20 ../
-rw------- 1 rich rich 44 Oct 29 22:20 temp.N5F3O6
-rw------- 1 rich rich 44 Oct 29 22:20 temp.SQslb7
[dir.ouT8S8]$ cat temp.N5F3O6
This is a test line of data for temp.N5F3O6
[dir.ouT8S8]$ cat temp.SQslb7
This is a test line of data for temp.SQslb7
[dir.ouT8S8]$

The script creates a directory in the current directory, then it uses the cd command to change to that directory before creating two temporary files. The two temporary files are then assigned to file descriptors and used to store output from the script.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Shell Scripting Topics