Spread the love
Temporary Folder by JackXan on DeviantArt

Introduction

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. Now we’re gonna teach you to use the mktemp command in Linux.

The syntax of the mktemp command

The syntax:

$ mktemp [option] … [template]

For example:

$ mktemp

Output:

You have just created a temporary file in the tmp directory. And the filename is also generated automatically.

Also:

Usage: mktemp [OPTION]... [TEMPLATE]
Create a temporary file or directory, safely, and print its name.
TEMPLATE must contain at least 3 consecutive `X's in last component.
If TEMPLATE is not specified, use tmp.XXXXXXXXXX, and --tmpdir is implied.
Files are created u+rw, and directories u+rwx, minus umask restrictions.

  -d, --directory     create a directory, not a file
  -u, --dry-run       do not create anything; merely print a name (unsafe)
  -q, --quiet         suppress diagnostics about file/dir-creation failure
      --suffix=SUFF   append SUFF to TEMPLATE.  SUFF must not contain slash.
                        This option is implied if TEMPLATE does not end in X.
      --tmpdir[=DIR]  interpret TEMPLATE relative to DIR.  If DIR is not
                        specified, use $TMPDIR if set, else /tmp.  With
                        this option, TEMPLATE must not be an absolute name.
                        Unlike with -t, TEMPLATE may contain slashes, but
                        mktemp creates only the final component

  -p DIR              use DIR as a prefix; implies -t [deprecated]
  -t                  interpret TEMPLATE as a single file name component,
                        relative to a directory: $TMPDIR, if set; else the
                        directory specified via -p; else /tmp [deprecated]

      --help     display this help and exit
      --version  output version information and exit

Report mktemp bugs to bug-coreutils@gnu.org
GNU coreutils home page: </software/coreutils/>
General help using GNU software: </gethelp/>
Report mktemp translation bugs to <http://translationproject.org/team/>
For complete documentation, run: info coreutils 'mktemp invocation'
Code language: HTML, XML (xml)

Creating a temporary directory

To do this. Let’s add the option -d into the command:

$ mktemp -d

Output:

Name the temporary file

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

$ mktemp newfileXXX

Output:

Or you can name the temporary directory:

$ mktemp -d newdicXXX

Output:

Adding the suffix

To do this. Let’s add the option –suffix into the command. For example:

$ mktemp newfileXXX --suffix ".txt"
Code language: JavaScript (javascript)

Output:

Conclusion

We just taught you to use the mktemp command in Linux.

Another interesting article may be: A Easy Way How To Install GIT ( Complete Guide )

Leave a Reply