Building Bash Muscle (15)

touch is a very basic command that lets you create empty files. That’s not the only purpose that you use touch for. It can modify attributes of existing files. Let’s take a look at the following bash script.

$ touch testfile.txt
$ echo 'hello world' >> testfile.txt
$ ll
-rw-rw-r--  1 amaterasu48 amaterasu48     12 Jan  2 23:02 testfile.txt

When you execute the following command, you get to change the modification time.

$ touch -m testfile.txt
$ ll

Now the output show the modified time to be later than the original time.

-rw-rw-r--  1 amaterasu48 amaterasu48     12 Jan  2 23:07 testfile.txt

To modify just the access time, execute the following command.

$ touch -a testfile.txt

To check the access time, execute ls -lu. Also, when you use touch to existing files without any option, it modifies both the modified time and access time.


touch is used not only to create empty files but it can modify modified and accessed time attributes.

Leave a Reply

Your email address will not be published.