We already know how to create files of a certain size under Linux. Creating files of a certain size can be useful in many cases. After installing a new file server, you can check, for example, the download limit, maximum download size, download speed and so on. In this case, you can create files of a certain size and test them on the file server. There is also a special command line tool called mktemp that can be used to create temporary files and folders. The mktemp command securely creates a temporary file or folder and prints the name. All files and folders are stored in a temporary system directory, i.e. in /tmp. So there is no need to clean them manually. When the system reboots, the temporary files disappear.
mktemp control tutorial with examples
Mktemp is part of the GNU coreutils package. So don’t bother hanging it. We will now look at some practical examples.
To create a new temporary file, just run it:
You can see the result as shown below:
As you can see from the output, a new temporary file with the random name tmp.U0C3cgGGFpk is created in /tmp. This file is just empty.
You can also create a temporary file with the specified suffix. The following command creates a temporary file with a .txt extension:
mktemp — Suffix .txt
How about a temporary directory? Yes, you can! To create a temporary directory, use the -d option.
$ mktemp -d
This creates a random empty folder in the /tmp folder.
All files are created with u+rw permissions and folders with u+rwx permissions without the mask. In other words, the resulting file has read and write permissions for the current user, but not for the group or other users. And the resulting directory has read, write and output rights for the current user, but no access rights for groups or other users.
You can check the access rights to the files with the ls command :
$ ls -al /tmp/tmp.U0C3cgGFpk
-rw—————————1 sk 0 May 14 13:20 /tmp/tmp.U0C3cgGFpk
Check the access rights to the folder with the ls command:
$ ls -ld /tmp/tmp.PE7tDnm4uN
drwx——- 2 sk 4096 14. May 13:25 /tmp/tmp.PE7tDnm4uN
I suggest you read it:
Use the mktempcommand to create temporary files or folders with custom names.
As I said before, all files and folders are created with random filenames. We can also create a temporary file or a folder with a custom name. Simply add at least 3 consecutive Xs at the end of the filename, as shown below
$ mktemp ostechnixXXX
To create a directory, just start it up:
mktemp -d ostechnixXXX
Note that when you choose a custom name, the files/directories will be created in the current working directory, not in /tmp. In this case, they must be removed manually.
Also, as you may have noticed, the X in the filename is replaced by random characters. However, you can add the suffix of your choice.
For example, I want to add a blog at the end of a filename. So my team will take care of it:
$ mktemp ostechnixXXX –suffix=blog
We now have a blog suffix at the end of the filename.
If you don’t want to create a file or folder, just try it out as shown below.
Get help, run!
$ mktemp –help
Why do we really need mktemp?
You may wonder why we need mktemp when we can easily create empty files with the command touch filename. The mktemp command is mainly used to create temporary files/directories with random names. So we don’t have to bother finding the names. Since mktemp makes names random, there will be no name collision. In addition, mktemp securely creates 600 (rw) files and 700 (rwx) folders so that other users cannot access them. For more information we refer you to the man pages.
Millions of dollars.
Thank you so much for coming!
Help us to help you:
Have a nice day!mktemp example,mktemp example c,mktemp cleanup,mktemp windows,mktemp vs mkstemp,mktemp command not found,ubuntu mktemp,mktemp python