The mktemp Command Tutorial With Examples For Beginners
We already know how to create files of a certain size in Linux. Creating files with the given size can be helpful in many occasions. For example, after deploying a new file server, you might want to check upload limit, maximum upload size, and download speed of the files etc. In such cases, you can create files of specific size and test them on your file server. There is also a dedicated command line tool named “mktemp” is available to create temporary files and directories. The mktemp command creates a temporary file or directory safely and prints its name. All files and directories will be saved in the system’s temporary directory, i.e /tmp. So you need not to manually clean up them. Once you rebooted your system, the temporary files will be gone.
The mktemp Command Tutorial With Examples
Mktemp is part of GNU coreutils package. So don’t bother with installation. We will see some practical examples now.
To create a new temporary file, simply run:
You will see an output like below:
As you see in the output, a new temporary file with random name “tmp.yjcVSaKkuv” is created in /tmp directory. This file is just an empty file.
How about a temporary directory? Yes, it is also possible! To create a temporary directory, use -d option.
$ mktemp -d
This will create a random empty directory in /tmp folder.
All files will be created with u+rw permission, and directories with u+rwx, minus umask restrictions.
You can verify the file or directory permissions using ls command.
$ ls -al /tmp/tmp.yjcVSaKkuv -rw------- 1 sk sk 0 Aug 22 16:51 /tmp/tmp.yjcVSaKkuv
$ ls -al /tmp/tmp.vrhruN1WVX/ total 8 drwx------ 2 sk sk 4096 Aug 22 16:58 . drwxrwxrwt 12 root root 4096 Aug 22 17:36 ..
As I already said, all files and directories are created with a random file names. We can also create a temporary file or directory with a custom name. To do so, simply add at least 3 consecutive ‘X’s at the end of the file name like below.
$ mktemp ostechnixXXX ostechnixq70
Similarly, to create directory, just run:
$ mktemp -d ostechnixXXX ostechnixcBO
Please note that if you choose a custom name, the files/directories will be created in the current working directory, not /tmp location. In this case, you need to manually clean up them.
Also, as you may noticed, the X’s in the file name are replaced with random characters. You can however add any suffix of your choice.
For instance, I want to add “blog” at the end of the filename. Hence, my command would be:
$ mktemp ostechnixXXX --suffix=blog ostechnixZuZblog
Now we do have the suffix “blog” at the end of the filename.
If you don’t want to create any file or directory, you can simply perform a dry run like below.
$ mktemp -u /tmp/tmp.oK4N4U6rDG
For help, run:
$ mktemp --help
Why do we actually need mktemp?
You might wonder why do we need “mktemp” while we can easily create empty files using “touch filename” command. The mktemp command is mainly used for creating temporary files/directories with random name. So, we don’t need to bother figuring out the names. Since mktemp randomizes the names, there won’t be any name collision. Also, mktemp creates file/directory safely with permission 600, so the other users can’t access it. For more details, check man pages.
$ man mktemp