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.U0C3cgGFpk" is created in /tmp directory. This file is just an empty file.
You can also create a temporary file with a specified suffix. The following command will create a temporary file with ".txt" extension:
$ mktemp --suffix ".txt" /tmp/tmp.sux7uKNgIA.txt
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. In other words, the resulting file will have read and write permissions for the current user, but no permissions for the group or others. And the resulting directory will have read, write and executable permissions for the current user, but no permissions for groups or others.
You can verify the file permissions using "ls" command:
$ ls -al /tmp/tmp.U0C3cgGFpk -rw------- 1 sk sk 0 May 14 13:20 /tmp/tmp.U0C3cgGFpk
Verify the directory permissions using "ls" command:
$ ls -ld /tmp/tmp.PE7tDnm4uN drwx------ 2 sk sk 4096 May 14 13:25 /tmp/tmp.PE7tDnm4uN
Create temporary files or directories with custom names using mktemp command
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 files safely with permission 600(rw) and directories with permission 700(rwx), so the other users can't access it. For more details, check man pages.
$ man mktemp