{"id":3492,"date":"2017-08-14T10:28:24","date_gmt":"2017-08-14T10:28:24","guid":{"rendered":"http:\/\/www.fastwebhost.in\/blog\/?p=3492"},"modified":"2019-01-22T09:47:49","modified_gmt":"2019-01-22T09:47:49","slug":"11-examples-of-touch-command-in-linux","status":"publish","type":"post","link":"https:\/\/www.fastwebhost.in\/blog\/11-examples-of-touch-command-in-linux\/","title":{"rendered":"11 Linux Touch commands with Examples"},"content":{"rendered":"<p>The touch command can be used to modify the access\/modification timestamps of files. More often it would be used to create an empty file quickly.<\/p>\n<p>A nice feature of touch is that, in contrast to some commands such as cp (which is used to copy files and directories) and mv (which is used to move or rename files and directories), it does not automatically overwrite (i.e., erase the contents of) existing files with the same name. Rather, it merely changes the last access times for such files to the current time.<\/p>\n<p>In this post we are going to explain you few simple and quick examples of using the touch command to modify timestamps or create files.\u200b<\/p>\n<h2>1. \u200bHow to create a blank file<\/h2>\n<p>\u200bTo create a simple blank file using touch command, use the below syntax. If the filename you choose is already there, than it will just change the access time.<\/p>\n<p>touch fastwebhost.txt\u200b<\/p>\n<h2>2. How to create multiple files<\/h2>\n<p>When you are suppose to create multiple files using touch command, just type the files name with extension separated by a space. You can check the below syntax.<\/p>\n<p>touch fast.\u200btxt web.txt host.txt<\/p>\n<h2>3. How to create bulk files with one command<\/h2>\n<p>If you were suppose to create bulk files, for example for 20 or 30 files and more than that with and without extensions. The below example will surely help you. You can check if the files were created or not with the command <span>ls or ll<\/span><\/p>\n<p>1. \u200bCreate files with names A to Z<\/p>\n<p>touch {A..Z}\u200b<\/p>\n<p>2. Create files from 1 to 100<\/p>\n<p>touch {1..100}\u200b<\/p>\n<p>3. Create bulk files with extensions<\/p>\n<p>touch {2..200}.txt\u200b<\/p>\n<p data-unit=\"px\" style=\"line-height: 30px;\">4. To create 10000 files with an extension<\/p>\n<p>touch {1..10}{1..1000}.txt\u200b<\/p>\n<h2>4. How to avoid creating new files<\/h2>\n<p>Do you just want to update the access time of an existing file? Yes, you can surely do that without creating a new file. Use the option <span>&#8220;<\/span><span>-c&#8221;<\/span>.<span>&nbsp;<\/span>If the file exists, touch will update the access time, else will do nothing\u200b.<\/p>\n<p>touch -c fas\u200btwebhost.txt<\/p>\n<h2>5. How to change access time of a file<\/h2>\n<p>To change only access time of a file use the <span>&#8220;-a&#8221;<\/span> option with the file name. You can check the below example.<\/p>\n<p>touch -a fastwebhost.txt\u200b<\/p>\n<p>Once you done with the above command, to check if it is changed or not? try the below command.<\/p>\n<p>stat fastwebhost.txt<\/p>\n<p>\u200bAnd the output looks like below.<\/p>\n<p>File: \u2018fastwebhost.txt\u2019Size: 0 Blocks: 0 IO Block: 4096 regular empty fileDevice: 804h\/2052d Inode: 29645431 Links: 1Access: (0664\/-rw-rw-r&#8211;) Uid: ( 1001\/ aditya) Gid: ( 1001\/ aditya)Access: 2017-08-14 13:30:24.029067639 +0530Modify: 2017-08-14 13:30:24.029067639 +0530Change: 2017-08-14 13:30:24.029067639 +0530<\/p>\n<h2>6. How to change modified time<\/h2>\n<p>Use the &#8216;-m&#8217; option to change the modified time of the file. You can check the time change in the following with stat command.<\/p>\n<p>touch -m fastwebhost.txt\u200bFile: \u2018fastwebhost.txt\u2019Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 804h\/2052d Inode: 29645431 Links: 1Access: (0664\/-rw-rw-r&#8211;) Uid: ( 1001\/ aditya) Gid: ( 1001\/ aditya)Access: 2017-08-14 13:30:24.029067639 +0530Modify: 2017-08-14 13:38:18.989096045 +0530Change: 2017-08-14 13:38:18.989096045 +0530<\/p>\n<h2>7. How to change modify time for multiple files<\/h2>\n<p>To change the modify time of multiple files using wildcard.\u200b<\/p>\n<p>touch -m *.txt\u200b<\/p>\n<h2>8. How to use both access and modifying time options<\/h2>\n<p>Let&#8217;s use the <span>&#8220;<\/span><span>a&#8221;<\/span> and <span>&#8220;<\/span><span>m&#8221;<\/span> option together to modify both access and modification time\u200bs. You can also check the changed timestamp using the <span>&#8220;<\/span><span>stat<\/span><span>&#8220;<\/span> command.<\/p>\n<p>touch -am fastwebhost.txt\u200b<\/p>\n<h2>\u200b9. How to set particular time for access\/modify<\/h2>\n<p>To set the access\/modify time to a specific datetime use the <span>t<\/span> option and specify the datetime in this format &nbsp;((CC)YY)MMDDhhmm.(ss)<\/p>\n<h3>List of options with Touch Command<\/h3>\n<p><span><\/span><\/p>\n<ul>\n<li style=\"margin-bottom: 10px !important;\">\u200b-a &#8211; Change access time of a file<\/li>\n<li style=\"margin-bottom: 10px !important;\">-c &#8211; Do not create&nbsp;a specified file if it does not exist.<\/li>\n<li style=\"margin-bottom: 10px !important;\">-m &#8211;&nbsp;Change the modification time of file.<\/li>\n<li style=\"margin-bottom: 10px !important;\">-r &#8211;&nbsp;ref_fileUse the corresponding times of the file named by ref_file instead of the current time.<\/li>\n<li style=\"margin-bottom: 10px !important;\">-t &#8211;&nbsp;timeUse the specified time instead of the current time. time will be a decimal number of the form<\/li>\n<li style=\"margin-bottom: 10px !important;\">MM \u2013 The month of the year [01-12]<\/li>\n<li style=\"margin-bottom: 10px !important;\">DD \u2013 The day of the month [01-31]<\/li>\n<li style=\"margin-bottom: 10px !important;\">hh \u2013 The hour of the day [00-23]<\/li>\n<li style=\"margin-bottom: 10px !important;\">mm \u2013 The minute of the hour [00-59]<\/li>\n<li style=\"margin-bottom: 10px !important;\">CC \u2013 The first two digits of the year<\/li>\n<li style=\"margin-bottom: 10px !important;\">YY \u2013 The second two digits of the year<\/li>\n<li style=\"margin-bottom: 0px !important;\">SS \u2013 The second of the minute [00-61]<\/li>\n<\/ul>\n<p>\u200btouch -c -t 2017080711.15 fastwebhost.txt<\/p>\n<p style=\"color: rgb(44, 143, 244);\">Note:&nbsp;If you omit the c option, a new file will be created with the given datetime if it does not exist.<\/p>\n<h2>10. How to \u200buse the timestamp of another file as reference<\/h2>\n<p>The below command will set the access\/modify time of fast.txt to that of fastwebhost.txt<\/p>\n<p>\u200btouch -r fastwebhost.txt fast.txt<\/p>\n<h2>11. How to specify date and time as string<\/h2>\n<p>Otherthan the option&nbsp;&#8220;<strong>t<\/strong>&#8221;&nbsp;, there is another option &#8220;<strong>-d<\/strong>&#8221; which accepts datetime in general human readable formats.<\/p>\n<p>The below&nbsp;example provides the date only. The time will automatically set to 00:00\u200b<\/p>\n<p>touch -c -d &#8217;14 Aug&#8217; fastwebhost.txt<\/p>\n<h2>Conclusion<\/h2>\n<p>In this article we covered basic touch commands which should be use more frequently. You\u200b can search more command options from its man pages. We are providing other Linux Commands articles in the below list.<\/p>\n<h3>Basic Linux Commands<\/h3>\n<p><span><\/span><\/p>\n<ul style=\"margin-bottom: 10px !important;\" data-unit=\"px\">\n<li style=\"line-height: 17px; margin-bottom: 0px !important;\"><a href=\"http:\/\/www.fastwebhost.in\/blog\/how-to-change-reset-mysql-root-password-linux-via-command-line\/\" target=\"_blank\" rel=\"\" data-tcb-events=\"\"><\/a><a href=\"http:\/\/www.fastwebhost.in\/blog\/grep-command-in-unix-linux-with-11-simple-examples\/\" target=\"_blank\">G\ufeffrep Commands<\/a><\/li>\n<\/ul>\n<ul style=\"margin-bottom: 0px !important;\" data-unit=\"px\">\n<li style=\"line-height: 17px; margin-bottom: 0px !important;\"><a href=\"http:\/\/www.fastwebhost.in\/blog\/how-to-create-a-mysql-database-via-command-line\/\" target=\"_blank\"><\/a><a href=\"http:\/\/www.fastwebhost.in\/blog\/putty-30-useful-putty-commands-for-beginners\/\" target=\"_blank\">Putty Commands<\/a><\/li>\n<\/ul>\n<ul>\n<li style=\"margin-bottom: 0px !important;\"><a href=\"http:\/\/www.fastwebhost.in\/blog\/how-to-select-mysql-database-linux-via-command-line\/\" target=\"_blank\"><\/a><a href=\"http:\/\/www.fastwebhost.in\/blog\/30-linux-find-command-with-examples\/\" target=\"_blank\">Find Commands<\/a><\/li>\n<\/ul>\n<ul>\n<li style=\"margin-bottom: 0px !important;\"><a href=\"http:\/\/www.fastwebhost.in\/blog\/how-to-delete-a-mysql-database-on-linux-via-command-line\" target=\"_blank\"><\/a><a href=\"http:\/\/www.fastwebhost.in\/blog\/what-is-curl-and-how-to-use-curl-commands-in-linux\/\" target=\"_blank\">cURL Commands<\/a><\/li>\n<\/ul>\n<ul>\n<li style=\"margin-bottom: 0px !important;\"><a href=\"http:\/\/www.fastwebhost.in\/blog\/how-to-backup-mysql-databases-via-command-line\" target=\"_blank\" rel=\"\" data-tcb-events=\"\"><\/a><a href=\"http:\/\/www.fastwebhost.in\/blog\/10-df-commands-to-check-space-in-linux\/\" target=\"_blank\">df Commands<\/a><\/li>\n<li style=\"margin-bottom: 0px !important; margin-top: 20px !important;\"><a href=\"http:\/\/www.fastwebhost.in\/blog\/how-to-use-sed-command-in-unix-with-examples\/\" target=\"_blank\">SED Commands<\/a><\/li>\n<li style=\"margin-bottom: 0px !important; margin-top: 20px !important;\"><a href=\"http:\/\/www.fastwebhost.in\/blog\/10-examples-of-linux-wget-command\/\" target=\"_blank\">Wget Commands<\/a><\/li>\n<li style=\"margin-bottom: 0px !important; margin-top: 20px !important;\"><a href=\"http:\/\/www.fastwebhost.in\/blog\/13-rsync-command-examples-on-linux\/\" target=\"_blank\">Rsync Commands<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The touch command can be used to modify the access\/modification timestamps of files. More often it would be used to create an empty file quickly. A nice feature of touch is that, in contrast to some commands such as cp (which is used to copy files and directories) and mv (which is used to move [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":3504,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[22],"tags":[76,147],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/posts\/3492"}],"collection":[{"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/comments?post=3492"}],"version-history":[{"count":3,"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/posts\/3492\/revisions"}],"predecessor-version":[{"id":4696,"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/posts\/3492\/revisions\/4696"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/media\/3504"}],"wp:attachment":[{"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/media?parent=3492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/categories?post=3492"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/tags?post=3492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}