The touch Command Does Much More Than Just Create an Empty File

Here we have come up with another article where you will learn how to use the touch command, which has been primarily used for creating empty files for a long time.

And after reading this article, you will be amazed by the capabilities of touch command, which you have been underestimating for a long period of time.

So let me start this article where you will learn how to create an empty file or multiple files using the touch command. In addition, you will also learn to change access and modification times by giving some references.

10 Common Usage of top Command

I have compiled some of the common usages of touch commands that you can use in the real world, so make sure to learn them for future use.

1. Create an empty file using the touch command

First of all, let me start this guide with one of the common functionality of the touch command, which will be very handy for your future use to create an empty file from the terminal itself, without opening any command line editor like nanovim, etc.

And to create an empty file, first open your terminal window and copy-paste the code below, which will create a file named “filename”.

$ touch filename

Once you have invoked the command, it will not throw any messages, so to check whether the file has been created or not, use the ls command to list out the files.

$ ls -l

The output of the above command.

lmde@trendoceans:~$ ls -l
-rw-r--r-- 1 lmde lmde    0 Sep 28 06:13 filename

According to the above output, you have created a file in the current directory which has a size of 0 bytes. Obviously, the file size will be zero because the file is completely empty.

2. Create multiple files using the touch command

Not only that, but you can also create more than one file at a time by passing multiple file names, which can be done by running the below command.

$ touch filename1 filename2 filename3

Instead of that, you can use the brace expansion method, which can create n numbers of files in a matter of seconds. Seriously, is it possible to do it? Yes, it’s possible by executing the following command on your terminal.

$ touch filename{1..10}

If you want to learn more about how to create multiple files, then check out this article.

3. Do not create a file and change the timestamp

Typically, when you execute the touch command with a filename, it creates a new file if the file is not located in the following directory, or else if the file exists, then it will change the access time of the file.

And hence, if you want to touch should not create a file, then use the -c or --no-create option like in the below code snippet, which will skip the file creation process.

$ touch -c filename

4. Change access time using the touch command

The touch command is not only used to create empty or zero byte files; it is also useful for changing access times, modification, and changing the time of a file.

When you run touch filename, it will create the file, but if the file is present, then it will change the access, modification, and change time. However, if you are interested in changing only access time and leaving the rest of all things untouched, then you should use the -a option.

$ touch -a filename

This will change the atime or access time of the corresponding file.

5. Change modification time using the touch command

After learning how to change access time in the previous section, you will see how to change the modification time or mtime of the respective file by using the -m options. Let’s say you have a file whose name is “filename” on your system and want to change only the modified time. Then you should execute the below code.

$ touch -m filename

6. Change access time on the basis of the reference file using the touch command

Not only that, but you can also change the access or atime of any respective file by specifying the path of the reference file by using the --refrence=/path/of/file or -r option, which will use the access time of the specified file to set the atime of the corresponding file.

Let’s take an example for better understanding. I do have two different files. One of the filenames is “sample_1” and the other is “sample_2”. Before changing the access time, let me check the access time by using the stat command.

lmde@trendoceans:~$ stat sample_1
  File: sample_1
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fe00h/65024d    Inode: 26345654    Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/    lmde)   Gid: ( 1000/    lmde)
Access: 2022-10-03 05:53:16.407781136 -0400

------------------------------------------------------------------------------------

lmde@trendoceans:~$ stat sample_2
  File: sample_2
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fe00h/65024d    Inode: 26345788    Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/    lmde)   Gid: ( 1000/    lmde)
Access: 2022-09-28 06:23:57.503851824 -0400

According to the above output, both files have different atime, so let me replicate the timestamp of sample_1 to sample_2 by running the below command.

$ touch --reference=/home/trendoceans/sample_1 ~/sample_2
or
$ touch -r ~/sample_1 ~/sample_2

Result after executing the above command

lmde@trendoceans:~$ stat sample_1
  File: sample_1
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fe00h/65024d    Inode: 26345654    Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/    lmde)   Gid: ( 1000/    lmde)
Access: 2022-10-03 05:53:16.407781136 -0400

------------------------------------------------------------------------------------
lmde@trendoceans:~$ stat sample_2
  File: sample_2
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fe00h/65024d    Inode: 26345788    Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/    lmde)   Gid: ( 1000/    lmde)
Access: 2022-10-03 05:53:16.407781136 -0400

7. Change access time on basis of reference file for Symlinks using the touch command

When you want to replicate access time across symlinks, then you should use the -h or — no-dereference option like in the below code snippet, but the only problem with this option is it won’t support most of the Linux distributions.

If it’s working in your system, then don’t forget to put the distribution name in the below comment section.

$ touch -h symlink-file
or
$ touch --no-dereference symlink-file

8. Change the timestamp of a file using the touch command

If you have read one of our articles where we discussed how to delete files that have not been accessed or modified in a particular time frame, I have used the same techniques to come up with the output of the result.

So let’s see how to change or set the custom date and time of the file by following the [[CC]YY]MMDDhhmm[.ss] pattern.

For example, I want to change the file timestamp to some 202210041630, which looks weird. Hold on, I’ll decode the secret number for you, but don’t tell anyone it’s secret 😅

2022   10     04    16     00
  ↓     ↓      ↓     ↓     ↓
Year  Month  Date  Hour   Minutes

After getting aware of the timestamp, let’s use the -t option to set a custom timestamp as shown in the below code snippet.

$ touch -t 202210041600 filename  

The output of the above command.

lmde@trendoceans:~$ stat filename
  File: filename
  Size: 6               Blocks: 8          IO Block: 4096   regular file
Device: fe00h/65024d    Inode: 26345654    Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/    lmde)   Gid: ( 1000/    lmde)
Access: 2022-10-04 16:00:00.000000000 -0400
Modify: 2022-10-04 16:00:00.000000000 -0400
Change: 2022-10-04 10:44:55.225002615 -0400
 Birth: 2022-09-28 06:13:28.479889509 -0400

9. Change only the date of the file using the touch command

Above, you have learned how to change the timestamp of the file, which changes the date and time of the file, but sometimes you just want to change the date of the file. Then use the below command, which will change the date of the file, and subsequently, it will reset the time of the file.

$ touch -d '04 October' filename

Not only that, you can also set the date of the file using the below command.

$ touch --date="yesterday" filename
or
$ touch -d "friday" filename

10. Change only time of the file using the touch command

Yes, you can also use the -d or --date= option to set the time of the file without making any changes to the date section. Let’s say you want to set the time of the file which is saved as “filename” in your system to 18:45:00 or 18:45 like that, then you should consider the below command.

$ touch -d "18:45:22" filename

The output of the above command

lmde@trendoceans:~$ stat filename
  File: filename
  Size: 6               Blocks: 8          IO Block: 4096   regular file
Device: fe00h/65024d    Inode: 26345654    Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/    lmde)   Gid: ( 1000/    lmde)
Access: 2022-10-04 18:45:22.000000000 -0400
Modify: 2022-10-04 18:45:22.000000000 -0400
Change: 2022-10-04 10:44:55.225002615 -0400
 Birth: 2022-09-28 06:13:28.479889509 -0400

Cheat sheet of touch command

  • Create empty or zero bytes of file in Linux:
    • $ touch filename
  • Create multiple empty or zero bytes of files in Linux:
    • $ touch filename{1..10}
  • Avoid touch command to not create file:
    • $ touch -c filename
  • Change access or atime of the file:
    • $ touch -a filename
  • Change access or atime of the file in Linux using reference file:
    • $ touch -r /refrence-file-path/ /to-file-path/
  • Change modification or mtime of the file in Linux:
    • $ touch -m filename
  • Change the timestamp of the symbolic link in Linux:
    • $ touch -h symlink-file
  • Change timestamp of the file in Linux:
    • $ touch -t 202210041600 filename
  • Change date of the file in Linux:
    • $ touch -d '04 October' filename
  • Change time of the file in Linux:
    • $ touch -d "HH:MM:SS" filename

That’s all for now. If you want to add something, then do let me know in the comment section.

Leave a Reply