Linux Commands

How to Use Mktemp Command on Linux

We tend to create many files and folders in the home folder of our Linux systems. These files and folders can save the data endlessly without the removal of such files and folders on reboot. Linux came up with the new feature to create temporary files in the “tmp” folder. These files or folders would be automatically deleted after the reboot and all the data within these files or folders will be lost. Let’s take a look at the “mktemp” command in the Linux shell to create the temporary files in Ubuntu 20.04 system.

You need to make sure that your system is already up to date and upgraded to Ubuntu 20.04 system. If not, try using the “apt” package command in the Ubuntu terminal shell along with the keyword “update”. Before everything, you need to open up the command-line application to do all this. The command-line application can be launched with the usage of the activity menu on the top left corner of your Ubuntu 20.04 screen. Tap on that icon and a search bar will be opened. Write the name of a command-line application as “terminal” and hit the Enter button. It will open your terminal in a few seconds.

Let’s take a look at how the “mktemp” single word command works in the shell. So, within the query area, we have written the “mktemp” command and pressed “Enter” for the execution purpose. The output will be like “/tmp/tmp.LB1DOVqUPF”. It is a random and temporary file created in the “tmp” folder of your system. The name of a file is also generated automatically.

Let’s check the “tmp” folder of our system now. Open it through the file explorer. You can see from the image underneath that the file tmp.LB1DOVqUPF is generated.

You have seen that the file has no extension. If you want to add some extension at the end of a temporary file, you must add the “—suffix” keyword in the mktemp command along with the extension. The extension must be added within the inverted commas. You can see, we have been using the “.txt” extension to create a text type file within the “tmp” folder. After executing this command, the random file “tmp.q0GlvSUC75.txt” with the “.txt” extension is created as per the output.

After opening the “tmp” folder using the file explorer, we have seen that the temporary text file “tmp.q0GlvSUC75.txt” is created in the folder having the “.txt” extension.

This was all about the creation of a temporary file in the “tmp” folder. Along with the text files or random simple files, we can also create the temporary directory in the “tmp” folder. This directory name will also be generated randomly i.e. assigned by itself. This “mktemp” command can be used with the flag “-d” to create a random directory within the “tmp” folder. After the execution of this query, the directory name “tmp.55ULnaranu” is created in the “tmp” folder as the picture demonstrated.

Let’s open the file explorer and navigate towards the “tmp” folder of your Ubuntu 20.04 system as shown below. The directory named “tmp.55ULnaranu” can be seen in the opened “tmp” folder of our system shown in the image. This is the temporary folder and may automatically remove from the system once you reboot your Ubuntu 20.04 system.

Same as the simple random files, the temporary files and directories also have some privileges and restrictions assigned automatically by the system once these are created. Let say, we have created a new temporary file “tmp.uhPHOWaabz” with the “mktemp” query in the shell. Now, we can check out the assigned privileges to this newly created temporary file with the list command. So, we have to use the keyword “ls” along with the “-al” flag along with the location of the file in the “tmp” folder.

The result shows that the random file has only “read” and “write” privileges for the current user i.e., “saeedraza” and has no assigned privileges for the group and other users. The output is also showing the user and group it belongs to along with its creation date and time. This was about the simple temporary files.

Now, we will check out the same thing for the temporary directories. Therefore, you need to create a temporary directory in the “tmp” folder using the “mktemp” command with the “-d” flag. The directory is now generated in the “tmp” directory. The list command is utilized to check the privileges assigned to the temporary directory just created within the “tmp” folder. The “-ld” flag is used within the list command alongside the directory path as below.

The directory has read, write, and execution rights to the current user i.e., saeedraza. While the other users and groups have no rights to read, write and execute this directory. The output presented in the images shows the directory’s name, date, time on which it is created. Also, it shows the group and user name it belongs to.

The mktemp is not only used to create the temporary files and directories in the accounts “tmp” folder but the “home” folder as well. For this, you need to add the file name along with more than 3 “XXX” at the end. It will eventually create your file “newcHs” in the home folder. The “XXX” characters will automatically be replaced by automatically generated characters of the system. The list command is used to list all the files and directories of the current home of our Ubuntu 20.04 system. The “newcHs” file is shown in the “home” directory.

To create a directory in the home root folder, we need to add the “-d” flag within the “mktemp” folder with the directory name having “XXX” at its end. The directory “newoC9” is generated. The list “ls” query is showing that the directory “newoC9” is in the home folder.


We have implemented the working of the “mktemp” command in Ubuntu 20.04. We have used it to create temporary files and folders in the “tmp” folder along with some extensions. We have also taken a look at how to create the temporary file and directory in the home folder of our system.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.