Touch Command in Linux

FREE Online Courses: Knowledge Awaits – Click for Free Access!

In this article, you will learn all there is to the touch command in Linux. We will go through what the touch command is, why it is used, a brief history, different timestamps of a file, the syntax and options of the touch command, and some practical examples of the touch command in the terminal. So sit down, grab your snacks, and read right till the end!

What is linux touch command?

The touch command is a command-line-based utility in Linux-based operating systems that has 2 roles:

1. Modifying a timestamp

2. Creating a file.

The creating files part of the touch command is easy, it can create a file, if and only if the file does not exist. You can even create multiple files using the touch command.

But the touch command can also change timestamps, but first, what is a timestamp? The timestamp is a way of tracking what happened at what time, it is much like history on a timeline.

When we create a file or access it, everything gets logged by a timestamp, using the touch command, we can actually change these timestamps! Speaking in technical language, a timestamp is a piece of information associated with a file that identifies an important time in the file’s history.

History of the touch command in Linux

The first appearance of the touch command was in the 7th version of AT&T UNIX. Today, the touch command is available in a range of operating systems, both Linux-based and others like Windows, DOS, macOS, Unix-based, and many more.

The touch command that comes in the package of Coreutils, was written, designed, and developed by Paul Rubin, Arnold Robbins, Jim Kingdon, David MacKenzie, and Randy Smith.

Timestamps of Linux files

Before we go any further, let us look at the different timestamps a file in the Linux filesystem has. In Linux-based operating systems, a file has 3 timestamps:

1. atime

This timestamp stands for ‘access time’. This timestamp contains the last time the file was either accessed or opened by commands like “cat”, “vim”, “grep”, “vi”, etc.

2. mtime

This timestamp stands for modify time’. This timestamp contains the last time the contents of the file were modified.

3. ctime

This timestamp stands for change time’. This timestamp contains the last time the file’s content or attribute was changed. Attributes here mean permissions, location, or ownership.

If you want to display the status of the file including the timestamps of it use the “stat” command as shown below.

finding the status of a file by using the stat command

Syntax of Linux touch command

As we have seen, the touch command has 2 roles (creating files and modifying timestamps), let us take a look at the syntax of the touch command on each case:

The syntax for creating files using the touch command:

touch <options> <filename>

The syntax for creating files consists of 2 fields, one is for options (which we will look into in the next section) and the other is for entering the name of the file you want to create.

The syntax for modifying timestamps using the touch command:

touch <options> <timestamp> <filename>

The syntax for modifying timestamps consists of 3 fields, the first one is for the options, the second for the timestamp you want to modify to, and the third is the filename you want to modify the timestamps of.

Options used with Linux touch command

In both the syntaxes of the touch command, there is a field for options where we can enter a range of available options with which we can specify how touch should function and also how the output should be formatted. Let us look at the options available with the touch command.

1. -a

This option sets only the access time.

2. -c

This option does not create files. You can also write this option as “–no-create”.

3. -d

This option parses the date string and uses it instead of the current time.

4. -f

This option does nothing, nonetheless, it is accepted to provide compatibility with BSD versions of the touch command.

5. -h

This option makes the touch command modify the timestamp of symlink rather than its referenced file if the file you specified is a symbolic link. You can also write this option as “–no-dereference”

6. -m

This option sets only the modification time.

7. –r

This option sets the time of the file you specified to the times of the file reference file you specified instead of the current time.

8. -t

This option uses the numeric timestamp instead of the current time. You can use the format:

[[CC]YY]MMDDhhmm[.ss]

9. –help

This option displays the help menu as follows

help-menu of the touch command

10. –version

This option displays information about the version of the touch command

version of the touch command

Now that we have laid down the fundamentals, let us look at how to put all of this into action. Let’s start with the simple thing first: creating files using the touch command.

Creating a single file using the touch command

To create a single simply use the following syntax:

touch <filname>

creating a single file using the touch command

Creating multiple files using Linux touch command

To create multiple files at once using the touch command, let us look at them one by one. The first method is to simply name the files you want to create one after the other as shown: touch <file1> <file2> <file3>.

creating multiple files using the touch command

Another method we can use to create multiple files is to use {start .. finish}, use the syntax:

touch <filename {<start>..<finish>}>

For example, the command “touch linux{1..5}” will create the files ‘linux1’, ‘linux2’, ‘linux3’, ‘linux4’, and ‘linux5’.

creating multiple files using the touch command by specifiying a range

That is pretty much it for creating files with the touch command, let us now see how to modify and set timestamp using the touch command.

Before we look at how to modify timestamps, let us see how to display them, we can make use of the “stat command”, but we can also use the command “ls -l”.

printing detailed information about a file by using the ls l command

Setting a specific timestamp with the touch command

To set a specific timestamp pair the touch command with the option “-t” followed by the specific timestamp in the following format [[CC]YY]MMDDhhmm[.ss], where

  • CC stands for the first two digits of a year
  • YY stands for the last two digits of a year
  • MM stands for the month
  • DD stands for the day
  • hh stands for the hour
  • mm stands for the minutes
  • ss stands for the seconds

setting a specific timestamp with the touch command

Setting a file stamp using a date string

Instead of using the format shown above, we can use standard date convection and also English statements to set the timestamps. We can do so by using the option “-d” followed by the string you want to set the timestamp to as shown:

touch -d <string> <filename>

Before we see an example, let us see what all values and formats we can enter in the “string”.

1. Calendar dates – 19 June 2003

2. Time of the day – 10:37pm

3. Days of the week – Monday

4. Relative time – tomorrow, next week, 5 years ago, yesterday, etc.

For example, the command “touch -d tomorrow <filename>” will change the date of the timestamp to tomorrow’s date.

setting a file stamp using a date string

Changing access date to today’s date

If you want to change the access date ( timestamp that contains the last time the file was either accessed or opened by commands like “cat”, “vim”, “grep”, “vi”, etc) we can use the option “-a” as shown below:

touch -a <filename>

changing access date to todays date

In the above output, the access time has been changed from 7th May 2022 to 6th May 2022.

Explicitly changing access time

Using only the option “-a” changes the atime to today’s date, but what if you want to specifically change it to some other date, well, we pair it with the option “-t” followed by the date in the format [[CC]YY]MMDDhhmm[.ss].

explicitly changing access time

Changing access date to today’s date

If you want to change the access date (timestamp contains the last time the contents of the file were modified) we can use the option “-m” as shown: touch -a <filename>.

changing access date to todays date

In the above output, the modify time has been changed from 1st January 1999 to 6th May 2022.

Explicitly changing modification time

Using only the option “-m” changes the atime to today’s date, but what if you want to specifically change it to some other date, well, we pair it with the option “-t” followed by the date in the format [[CC]YY]MMDDhhmm[.ss].

explicitly changing modification time

Changing both access and modification time at once

If you want to change both the access time and modification time t0 today’s date in just one command, you can do so by combining the options “-a” and “-m” as shown:

touch -am <filename>

changing both access and modification time at once

Avoid creating a new file

We have seen that the touch command creates files if they don’t exist, sometimes we may have to override this functionality. To do so, use the option “-c” as shown:

touch -c <filename>

avoid creating a new file

Set timestamp using a symbolic link

To change the timestamp of a symbolic link, use the option “-h” using the following syntax:

touch -h <filename>

Set the timestamp using a reference file

If you want to set the timestamp of a file as the timestamp of another file, use the option “-r” followed by the reference file and the file you want to change the timestamp, here is the syntax:

touch -r <reference file><file>

setting the timestamp using a reference file

Touch vs cat command in Linux

Touch or cat? Which one is better? Frankly speaking, the question is incomplete, it depends on which grounds you are speaking, because, touch is clearly the winner in terms of modifying timestamps and cat is clearly the winner in displaying the content of the file.

However, they both have a slim common ground – creating files, if we talk about which command is better for creating files, hands down the appreciation goes to the cat command. Why? Because the cat command can not only create files, but can also edit, combine, display, and so much more.

Summary

As you have seen, the touch command is a really simple tool that helps you create files and modify timestamps of files and directories. You have now learned what the touch command is, a brief history of it, the different timestamps of Linux files, and the syntax and options of the touch command. You have also learned many practical examples of the touch command in the terminal.

Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *