{"id":1279,"date":"2021-11-29T05:15:49","date_gmt":"2021-11-28T23:45:49","guid":{"rendered":"https:\/\/smarttech101.com\/?p=1279"},"modified":"2022-02-10T19:44:04","modified_gmt":"2022-02-10T14:14:04","slug":"touch-command-in-linux-unix-with-examples","status":"publish","type":"post","link":"https:\/\/smarttech101.com\/touch-command-in-linux-unix-with-examples\/","title":{"rendered":"Touch Command in Linux\/Unix with Examples"},"content":{"rendered":"\n
In Linux\/Unix, the touch command is used to change<\/strong> the “modify-time<\/strong>” and “access-time<\/strong>” of a file without modifying the contents of the file. If the file does not exist, it is created.<\/strong><\/p>\n\n\n\n But before I start, you should know a little bit about these terms. Modify Time<\/strong> is the last time the content <\/strong>in the file was changed. On the other hand, the access time <\/strong>is the last time the file was read<\/strong>. I will be using the stat command<\/strong> to find these.<\/p>\n\n\n\n Now, we will understand the application of the touch command using a few examples.<\/p>\n\n\n\n If you have followed my recent post of setting up URxvt<\/a>, you might have seen that I created an empty file Syntax:<\/strong><\/p>\n\n\n\n For the example<\/strong> shown below, there is no such file called To set the timestamp to the current time, we follow the following Syntax<\/strong>:<\/p>\n\n\n\n Example:<\/strong><\/p>\n\n\n\n Syntax:<\/strong><\/p>\n\n\n\n The command is the same as touch FILE1<\/strong> except that all three files are given the same timestamps.<\/p>\n\n\n\n \ud83d\ude03 Fun Fact: <\/strong>In any man page<\/strong>, “triple dots” (…) means “repeatable argument”. You can have these arguments as many times as you want. Syntax:<\/strong><\/p>\n\n\n\n Example:<\/strong><\/p>\n\n\n\n Syntax:<\/strong><\/p>\n\n\n\n Example:<\/strong><\/p>\n\n\n\n All of the above Two ways are possible:<\/p>\n\n\n\n Syntax:<\/strong><\/p>\n\n\n\n The above command assumes timestamp in the format of Example:<\/strong><\/p>\n\n\n\n Here, If you omit Syntax:<\/strong><\/p>\n\n\n\n Here, touch parses the STRING in human-readable form like that in the<\/strong> date -d<\/strong> command<\/a>.<\/p>\n\n\n\n Example<\/strong>:<\/p>\n\n\n\nTable of Contents<\/h2>\n\n\n\n
How to Create an Empty File<\/h2>\n\n\n\n
~\/.Xresources<\/code> before symlinking<\/a> it to
~\/.Xdefaults<\/code>. There are many such real-life examples\/commands where a file needs to exist before these commands can be executed. And in this scenario, we use touch commands to create the empty file.<\/p>\n\n\n\n
~$ touch NON-EXISTANT-FILE<\/code><\/pre>\n\n\n\n
empty.txt<\/code> but as soon as I execute the
touch empty.txt<\/code><\/strong> command, I create it. You can also notice its size as “0”.<\/p>\n\n\n\n
How to Change the Timestamps of an Existing File<\/h2>\n\n\n\n
~$ touch EXISTING-FILE<\/code><\/pre>\n\n\n\n
How to Change Times of Multiple Files Together<\/h2>\n\n\n\n
~$ touch FILE1 FILE2 FILE3<\/code><\/pre>\n\n\n\n
<\/p>\n\n\n\n
How to Change Only the Access Time of a File<\/h2>\n\n\n\n
~$ touch -a FILE<\/code><\/pre>\n\n\n\n
How to Change Only the Modification Time of a File<\/h2>\n\n\n\n
~$ touch -m FILE<\/code><\/pre>\n\n\n\n
touch -m<\/code> changing only the modification mime<\/em><\/figcaption><\/figure>\n\n\n\n
touch<\/code> commands were putting a
current<\/code> timestamp on an existing file. What if we want to put a timestamp other than the current one?<\/p>\n\n\n\n
How to Put a Given Timestamp instead of Current Time on a File<\/h2>\n\n\n\n
touch -t<\/code><\/li>
touch -d<\/code> or
touch --date<\/code><\/li><\/ol>\n\n\n\n
1. Using touch -t<\/h3>\n\n\n\n
~$ touch -t STAMP FILE<\/code><\/pre>\n\n\n\n
CCYYMMDDhhmm.ss<\/code><\/p>\n\n\n\n
~$ touch -t 202103040100.10 ~\/.Xresources<\/code><\/pre>\n\n\n\n
CCYY<\/code>=2021
MMDD<\/code>=0304=4 March
hhmm.ss<\/code>=0100.10=01 hour 00 min 10 sec<\/p>\n\n\n\n
CCYY<\/code>, it becomes the current year. You can omit CC as well but I prefer not to do that because it is more confusing and not sustainable<\/strong> (for more, see info touch <\/strong>command).<\/p>\n\n\n\n
touch -t<\/em><\/code> STAMP using Given Time instead of Current Time<\/em><\/figcaption><\/figure>\n\n\n\n
2. Using touch -d<\/h3>\n\n\n\n
~$ touch -d STRING FILE<\/code><\/pre>\n\n\n\n
-d STRING<\/th> Meaning<\/th><\/tr><\/thead> -d '14:02'<\/code><\/td>
14:02 @ Today<\/td><\/tr> -d 'yesterday'<\/code><\/td>
current time @ yesterday<\/td><\/tr> -d 'today'<\/code><\/td>
current day<\/td><\/tr> -d 'next sunday'<\/code><\/td>
upcoming Sunday<\/td><\/tr> -d 'tomorrow<\/code>\u2018<\/td>
current time but tomorrow<\/td><\/tr> -d 'June 15, 2004'<\/code><\/td>
June 15 2004 @ 12:00:00 AM<\/td><\/tr> -d '@1619076682<\/code>\u2018<\/td>
Thu Apr 22 01:01:22 PM IST 2021; explained below<\/td><\/tr> -d '00:03:00'<\/code><\/td>
00:03:00 @ Today<\/td><\/tr> -d '-1 hour'<\/code><\/td>
1 hour ago<\/td><\/tr> -d '+1 hour'<\/code><\/td>
1 hour later<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n How to Change Timestamps of Links<\/h2>\n\n\n\n