Linux mktemp Command
The Linux mktemp command is used to create a temporary file.
mktemp creates a temporary file for use in shell scripts.
Syntax
mktemp [-qu][filename argument]
Parameters:
-q Suppresses error messages if an error occurs during execution.
-u The temporary file will be deleted before mktemp exits.
[filename argument] The filename argument must be in the format "customname.XXXXXX".
Example
When using the mktemp command to generate a temporary file, the filename argument should be provided in the format "filename.XXXX". mktemp will create a temporary file based on the filename argument. Enter the following command at the command prompt:
mktemp tmp.xxxx # Generate a temporary file
After using this command, you can use dir
or ls
to view the current directory, resulting in the following output:
cmd@cmd-desktop:~$ mktemp tmp.xxxx # Generate a temporary file
cmd@cmd-desktop:~$ dir # View the current directory
file test testfile testfile1 tmp.3847 # tmp.3847 has been generated
As shown, the generated temporary file is tmp.3847, where "XXXX" in the filename argument is replaced by 4 randomly generated characters.