{"id":7390,"date":"2021-06-22T16:37:46","date_gmt":"2021-06-22T16:37:46","guid":{"rendered":"https:\/\/linuxways.net\/?p=7390"},"modified":"2021-06-22T16:37:46","modified_gmt":"2021-06-22T16:37:46","slug":"linux-touch-command-examples","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/centos\/linux-touch-command-examples\/","title":{"rendered":"Linux Touch Command Examples"},"content":{"rendered":"<p>The touch command is a command-line utility used to create new and empty files. Aside from that, the command can be used in updating certain file properties such as access times and timestamps. In this article, we will go through practical examples of the Linux touch command<\/p>\n<p><strong> Basic syntax <\/strong><\/p>\n<p>The touch command takes the following syntax.<\/p>\n<pre><strong>$ touch [OPTION] [FILE]<\/strong><\/pre>\n<p>With that in mind, now let\u2019s check out a few touch command example usages.<\/p>\n<h2><strong>Creating an empty file<\/strong><\/h2>\n<p>In its simplest form without any command options, the touch command is used to create an empty file.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre><strong>$ touch [filename]<\/strong><\/pre>\n<p>In the example below, we have created a file named file1.txt. Using the<strong> ls <\/strong>command we can see that the file has been created.<\/p>\n<pre><strong>$ touch file1.txt<\/strong><\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"533\" height=\"117\" class=\"wp-image-7391\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-562.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-562.png 533w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-562-300x66.png 300w\" sizes=\"auto, (max-width: 533px) 100vw, 533px\" \/><\/strong><\/p>\n<h2><strong>Creating Multiple files<\/strong><\/h2>\n<p>Additionally, you can create multiple files at a go by specifying the file names in one command as follows<\/p>\n<pre><strong>$ touch [filename1] [filename2] [filename3] [filename4]<\/strong><\/pre>\n<p>For example, the common below creates four new files.<\/p>\n<pre><strong>$ touch file1.txt file2.txt file3.txt file4.txt<\/strong><\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"672\" height=\"99\" class=\"wp-image-7392\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-563.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-563.png 672w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-563-300x44.png 300w\" sizes=\"auto, (max-width: 672px) 100vw, 672px\" \/><\/strong><\/p>\n<p>We have created files named file1, file2, file3, and file4.<\/p>\n<h2><strong>Changing file access time<\/strong><\/h2>\n<p>If you would like to update the last access time of a file, use the touch -a command.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre><strong>$ touch -a [filename]<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"918\" height=\"382\" class=\"wp-image-7393\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-564.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-564.png 918w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-564-300x125.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-564-768x320.png 768w\" sizes=\"auto, (max-width: 918px) 100vw, 918px\" \/><\/p>\n<p>Here, we can check the access time of file2 before and after running the touch -a command. You will notice the access time has been modified.<\/p>\n<p>We are using the stat command to check the status of our directory.<\/p>\n<p><strong>Avoid creating new files<\/strong><\/p>\n<p>The touch command with the c option can be used to check whether a certain file exists. If that file does not exist, touch will not create it. You will have avoided creating a new file.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre><strong>$ touch -c [filename]<\/strong><\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"854\" height=\"155\" class=\"wp-image-7394\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-565.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-565.png 854w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-565-300x54.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-565-768x139.png 768w\" sizes=\"auto, (max-width: 854px) 100vw, 854px\" \/><\/strong><\/p>\n<p>Referring to the image above, the file named sample was not created.<\/p>\n<h2><strong>Changing access and modification time<\/strong><\/h2>\n<p>To update both the access and modification time use the touch command below.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre><strong>$ touch -c -t YYMMDDHHMM fileName<\/strong><\/pre>\n<p>For example:<\/p>\n<pre><strong>$ touch -c -t 202106121830 file4.txt<\/strong><\/pre>\n<p>Just to be cocksure, confirm the changes using the <strong>stat<\/strong> command as follows.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"984\" height=\"403\" class=\"wp-image-7395\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-566.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-566.png 984w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-566-300x123.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-566-768x315.png 768w\" sizes=\"auto, (max-width: 984px) 100vw, 984px\" \/><\/strong><\/p>\n<p>From the output, the access and modification time for file4.txt have both been changed to <strong>2021-06-12 18.30:30<\/strong>.<\/p>\n<h2><strong>Changing the modification time of a file<\/strong><\/h2>\n<p>If you want to change only the modification time of a file, use the touch -m command.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre><strong>$ touch -m [filename]<\/strong><\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"919\" height=\"431\" class=\"wp-image-7396\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-567.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-567.png 919w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-567-300x141.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-567-768x360.png 768w\" sizes=\"auto, (max-width: 919px) 100vw, 919px\" \/><\/strong><\/p>\n<p>From the image above, we can check the modification time of file3 before and after running the touch command. The time has been updated.<\/p>\n<h2><strong>Use the timestamps of another file<\/strong><\/h2>\n<p>The touch command with the <strong>-r <\/strong>option is used to apply the timestamp of one file to that of another file.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre><strong>$ touch -r second_file_name first_file_name<\/strong><\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"916\" height=\"384\" class=\"wp-image-7397\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-568.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-568.png 916w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-568-300x126.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-568-768x322.png 768w\" sizes=\"auto, (max-width: 916px) 100vw, 916px\" \/><\/strong><\/p>\n<p>You can confirm the modification times using the <strong>stat<\/strong> command as follows.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"919\" height=\"347\" class=\"wp-image-7398\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-569.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-569.png 919w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-569-300x113.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-569-768x290.png 768w\" sizes=\"auto, (max-width: 919px) 100vw, 919px\" \/><\/strong><\/p>\n<p>Here, the timestamps of file2 match that of file1.<\/p>\n<h2><strong>Creating a file using a specified time<\/strong><\/h2>\n<p>The touch -t command is used to specify time when creating a file. The syntax for the command is:<\/p>\n<pre><strong>$ touch -t YYMMDDHHMM fileName<\/strong><\/pre>\n<p>For example,<\/p>\n<pre><strong>$ touch -t 202106162228.30 file.txt<\/strong><\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"921\" height=\"219\" class=\"wp-image-7399\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-570.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-570.png 921w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-570-300x71.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/06\/word-image-570-768x183.png 768w\" sizes=\"auto, (max-width: 921px) 100vw, 921px\" \/><\/strong><\/p>\n<p>File.txt has been created with the specified time of <strong>2021-06-16 22.28:30<\/strong>.<\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>We have covered basic examples of the touch command. For more information use \u2018man touch\u2019 to view the manual page.<\/p>","protected":false},"excerpt":{"rendered":"<p>The touch command is a command-line utility used to create new and empty files. Aside from that, the command can be used in updating certain file properties such&hellip;<\/p>","protected":false},"author":1,"featured_media":7559,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,4,5,83,165,2],"tags":[378],"class_list":["post-7390","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-centos","category-debian","category-mint","category-opensuse","category-red-hat","category-ubuntu","tag-touch-command"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/7390","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=7390"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/7390\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/7559"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=7390"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=7390"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=7390"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}