{"id":6682,"date":"2018-09-11T17:14:11","date_gmt":"2018-09-11T13:14:11","guid":{"rendered":"https:\/\/www.howtoforge.com\/linux-mktemp-command\/"},"modified":"2018-09-11T17:14:11","modified_gmt":"2018-09-11T13:14:11","slug":"linux-mktemp-command-tutorial-for-beginners-5-examples","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/linux-mktemp-command-tutorial-for-beginners-5-examples\/","title":{"rendered":"Linux mktemp Command Tutorial for Beginners (5 Examples)"},"content":{"rendered":"<p>Creating temporary files or directories is a common task that many Linux command line users do. But do you know there&#8217;s a dedicated command line tool &#8211; dubbed mktemp &#8211; that lets you do this?<\/p>\n<p>In this tutorial, we will discuss the mktemp command basics using some easy to understand examples. But before we do that, it&#8217;s worth mentioning that all examples here have been tested on an Ubuntu 18.04 LTS machine.<\/p>\n<h2 id=\"linux-mktemp-command\">Linux mktemp command<\/h2>\n<p>The mktemp command in Linux lets users create a temporary file or directory. Following is its syntax:<\/p>\n<p class=\"command\">mktemp [OPTION]&#8230; [TEMPLATE]<\/p>\n<p>And here&#8217;s how the tool&#8217;s man page explains it:<\/p>\n<pre> Create a temporary file or directory, safely, and print its name.\u00a0 TEM?<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 PLATE must contain at least 3 consecutive 'X's in last\u00a0 component.\u00a0\u00a0 If<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 TEMPLATE is not specified, use tmp.XXXXXXXXXX, and --tmpdir is implied.<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Files are created u+rw, and directories\u00a0 u+rwx,\u00a0 minus\u00a0 umask\u00a0 restric?<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 tions.<\/pre>\n<p>Following are some Q&amp;A-styled examples that should give you a good idea on how the mktemp command works.<\/p>\n<h2 id=\"q-how-to-create-anbsptemporary-file-using-mktemp\">Q1. How to create a\u00a0temporary file using mktemp?<\/h2>\n<p>Simple. Just execute &#8216;mktemp&#8217; sans any option. You&#8217;ll see a\u00a0temporary file will get created in the \/tmp\/ folder.<\/p>\n<p class=\"command\">mktemp<\/p>\n<p>For example, in my case, the following output was produced:<\/p>\n<pre>\/tmp\/tmp.Hli0TAaYqp<\/pre>\n<h2 id=\"q-how-to-create-a-temporary-directory-using-mktemp\">Q2. How to create a temporary directory using mktemp?<\/h2>\n<p>This can be done using the -d command line option.<\/p>\n<p class=\"command\">mktemp -d<\/p>\n<p>For example, in my case, the following output was produced:<\/p>\n<pre>\/tmp\/tmp.JDDdS9Lloy<\/pre>\n<p>So you see, the name doesn&#8217;t reflect the fact that it&#8217;s a directory, but it actually is.<\/p>\n<h2 id=\"q-how-to-give-a-custom-name-to-your-temporary-filedirectory\">Q3. How to give a custom name to your temporary file\/directory?<\/h2>\n<p>As you might have observed by now, mktemp gives a random name to the temporary file or directory that it creates. However, if you want, you can provide a custom name.<\/p>\n<p>The tool offers a template that you can use. All you have to do is to provide a name, followed by three or more X in continuation. These X&#8217;s are replaced by mktemp with random characters so that the final name of the file or directory turns out to be unique.<\/p>\n<p>For example, I executed the following command:<\/p>\n<p class=\"command\">mktemp tempfileXXX<\/p>\n<p>And the following file was produced:<\/p>\n<pre>tempfileEgh<\/pre>\n<p>Note that files produced this way are located in the current working directory, instead of the \/tmp\/ directory (which is the default behavior).<\/p>\n<h2 id=\"q-how-to-add-a-suffix-to-the-end-of-the-filedirectory-name\">Q4. How to add a suffix to the end of the file\/directory name?<\/h2>\n<p>In the previous section, we discussed how to have a custom name when using mktemp. But that way, you can have only the initial part of the name as desired. What if you want a suffix of your choice as well?<\/p>\n<p>You&#8217;ll be glad to know this is also possible. You just have to use the &#8211;suffix option. Following is an example:<\/p>\n<p class=\"command\">mktemp tempfileXXX &#8211;suffix=HTF<\/p>\n<p>And here&#8217;s the name of the file produced by the aforementioned command:<\/p>\n<pre>tempfilecVkHTF<\/pre>\n<p>So you can see the suffix you supplied has been added to the filename.<\/p>\n<h2 id=\"q-how-to-just-perform-a-dry-run-with-mktemp\">Q5. How to just perform a dry run with mktemp?<\/h2>\n<p>If you just want mktemp to display a name and not create a file\/directory, then this can be done using the -u command line option.<\/p>\n<p class=\"command\">mktemp -u<\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>Depending on the kind of work you do on the Linux command line, the mktemp command can prove to be a handy, time-saving tool for you. Here, in this tutorial, we have discussed several command line option the tool offers. To learn more, head to its <a href=\"https:\/\/linux.die.net\/man\/1\/mktemp\" target=\"_blank\" rel=\"noopener\">man page<\/a>.<\/p>\n<div class=\"authorbox\" readability=\"15\">\n<img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/09\/linux-mktemp-command-tutorial-for-beginners-5-examples.jpg\" alt=\"Himanshu Arora\" title=\"\"> <\/p>\n<p><strong>About Himanshu Arora<\/strong><\/p>\n<p>Himanshu Arora has been working on Linux since 2007. He carries professional experience in system level programming, networking protocols, and command line. In addition to HowtoForge, Himanshu&#8217;s work has also been featured in some of world&#8217;s other leading publications including Computerworld, IBM DeveloperWorks, and Linux Journal.<\/p>\n<\/div>\n<div>\n<p><b>Share this page:<\/b><\/p>\n<p>\n<a href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.howtoforge.com%2Flinux-mktemp-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/09\/linux-mktemp-command-tutorial-for-beginners-5-examples.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-mktemp-command%2F&amp;text=Linux+mktemp+Command+Tutorial+for+Beginners+%285+Examples%29&amp;via=howtoforgecom&amp;related=howtoforgecom\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/09\/linux-mktemp-command-tutorial-for-beginners-5-examples-1.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/howtoforgecom\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/09\/linux-mktemp-command-tutorial-for-beginners-5-examples-2.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/plus.google.com\/share?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-mktemp-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/09\/linux-mktemp-command-tutorial-for-beginners-5-examples-3.png\" height=\"20\" alt=\"\" title=\"\"><\/a>\n<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Creating temporary files or directories is a common task that many Linux command line users do. But do you know there&#8217;s a dedicated command line tool &#8211; dubbed mktemp &#8211; that lets you do this? In this tutorial, we will discuss the mktemp command basics using some easy to understand examples. But before we do [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-6682","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/6682","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=6682"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/6682\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=6682"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=6682"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=6682"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}