Creating a Temporary File in Linux


Introduction

In Linux, it is often necessary to create temporary files for various purposes, such as storing intermediate data during processing or storing configuration information for a script. Temporary files are usually created in the /tmp directory, which is a standard location for storing temporary files on most Linux systems.

Creating a Temporary File in Linux

There are several ways to create a temporary file in Linux. One of the most common methods is to use the mktemp command, which creates a unique temporary file and prints the file name to the console.

Syntax

To create a temporary file using mktemp, use the following syntax.

$ mktemp [options] [template]

The template argument is a string that specifies the name and location of the temporary file, and can include XXXXXX as a placeholder for a unique suffix that mktemp will generate. The options argument is optional and can be used to specify various options, such as the directory in which to create the file or the permissions to set on the file.

Create a Random Temporary File in tmp Directory

The simplest way to create a temporary file can be done by running mktemp command without any arguments. If you run the mktemp command without any arguments, it will create a unique temporary file in the default temporary file directory (usually /tmp) with a default template of tmp.XXXXXX.

$ mktemp

This will create a file with a name like /tmp/tmp.qhgG9f.

You can also use the -q option to suppress the output of the mktemp command and store the file name in a variable instead. 

$ temp_file=$(mktemp -q)
$ echo "Temporary file: $temp_file"

This will create a temporary file with a name like /tmp/tmp.qhgG9f and store the file name in the temp_file variable. The echo command will then print the file name to the console.

Specifying a Directory and Permissions

To specify a different directory in which to create the temporary file, use the -d option. 

$ mktemp -d /my/custom/dir/temp.XXXXXX

This will create a temporary file in the /my/custom/dir directory with a name like temp.qhgG9f.

$ mktemp -d /my/custom/dir/temp.XXXXXX

You can also use the -p option to specify a prefix for the file name, like this −

$ mktemp -p /my/custom/dir mytemp.XXXXXX

This will create a file with a name like /my/custom/dir/mytemp.qhgG9f.

Specify Template of temporary file

To create a temporary file in the /tmp directory with a unique suffix and default permissions, you can use the following command −

$ mktemp /tmp/temp.XXXXXX

This will create a file with a name like /tmp/temp.qYhg9f, where qYhg9f is the unique suffix generated by mktemp.

You can also use the -t option to specify a template that includes the XXXXXX placeholder, like this −

$ mktemp -t temp.XXXXXX

This will create a file with a name like temp.qYhg9f in the default temporary file directory (usually /tmp).

Create multiple Temporary files

To create multiple temporary files at once, use the -u option followed by a template with multiple instances of the XXXXXX placeholder.

$ mktemp -u /tmp/temp1.XXXXXX /tmp/temp2.XXXXXX

This will create two temporary files with names like /tmp/temp1.qhgG9f and /tmp/temp2.qhgG9f.

Checking the Status of the Temporary File

After creating a temporary file, you may want to check its status to ensure that it was created successfully. You can use the stat command to display information about a file, including its size, permissions, and creation date.

To use stat, specify the name of the file as an argument, like this −

$ stat /tmp/temp.qhOs8Q

This will display information about the file, including its size, permissions, and creation date.

Removing the Temporary File

When you are finished with a temporary file, it is a good idea to remove it to free up disk space and prevent clutter. You can use the rm command to remove a file, like this:

$ rm /tmp/temp.qhOs8Q

This will delete the file permanently, so be sure that you no longer need the file before removing it.

mktemp output and file status

Here is an example of the mktemp command in action −

Example

$ mktemp /tmp/temp.XXXXXX
/tmp/temp.qhgG9f
$ stat /tmp/temp.qhgG9f
   File: /tmp/temp.qhgG9f
   Size: 0     Blocks: 0    IO Block: 4096    regular empty file
Device: 802h/2050d Inode: 1234   Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/ user) Gid: ( 1000/ user)
Access: 2022-12-16 11:07:35.000000000 -0500
Modify: 2022-12-16 11:07:35.000000000 -0500
Change: 2022-12-16 11:07:35.000000000 -0500
   Birth: -
$ rm /tmp/temp.qhgG9f

In this example, the mktemp command creates a temporary file in the /tmp directory with a unique suffix

Use mktemp in Bash Script

Once the temporary file has been created, you can use it like any other file on the system. When you are finished with the file, you can remove it using the rm command.

Example

Here is an example of creating a temporary file and then displaying its contents −

# Create the temporary file and store its path in a variable
temp_file=$(mktemp)
# Write some content to the file
echo "This is a temporary file" > $temp_file
# Display the contents of the file
cat $temp_file
# Remove the temporary file
rm $temp_file

This code will create a temporary file, write the string "This is a temporary file" to it, display the contents of the file, and then delete the file. The output of this code will be −

Output

This is a temporary file

Conclusion

In Linux, temporary files can be created easily using the mktemp command. By specifying a template and optional options, you can create a unique temporary file in the desired location and with the desired permissions. You can then use the stat command to check the status of the file, and the rm command to remove it when it is no longer needed.

Updated on: 12-Jan-2023

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements