mktemp Command in Linux - Explained with examples ?

Temporary files are used to temporarily store data that the operating system needs temporarily during operation and will disappear when no longer needed. they will disappear on their own after reboot.

This command in Linux allows the user to make a temporary file or directory in the tmp folder. 

Here at LinuxAPT, we shall look into how to use the mktemp command in Linux.


What is the syntax of the mktemp command ?

It's syntax is given below:

$ mktemp [option] … [template]

For example, if you run the below command:

$ mktemp

The output will show that a temporary file has been created in the tmp directory with a filename generated automatically.


How to create a temporary directory ?

Using the mktemp command with the option -d as shown below:

$ mktemp -d

You will be able to create a temporary directory.


How to name the created temporary file ?

If you want to name the temporary file, add more than 3 X's at the end. For example:

$ mktemp newfileXXX

Or you can name the temporary directory:

$ mktemp -d newdicXXX


How to add the suffix ?

You can use the option –suffix with the mktemp command to add a suffix as shown below:
$ mktemp newfileXXX --suffix ".txt"


[Need help in fixing Linux related issues ? We can help you. ]

This article covers how to use the mktemp command in Linux. In fact, mktemp can help to Create temporary files or directoriesin your Linux system.

Related Posts