Linux Commands

Touch Command in Ubuntu 22.04

The touch command in Ubuntu 22.04 serves various purposes such as generating files or sets of files, altering the modification or access times of files, etc. Moreover, this command can be used to classify the files based on their timestamps without causing any changes to the files. Besides this, you can set the date and time of modification of a file according to your desire using the touch command.

In this blog, we elaborate on the usage of a touch command in Ubuntu 22.04 by demonstrating some basic and advanced examples of this command. Without any further ado, let’s get started.

How Does Touch Command Work in Ubuntu 22.04?

The “touch” is a basic Linux command to create the empty files or change the file timestamps (last date or time of file access or modification).

Timestamps

Linux files have 3 timestamps: atime, mtime, and ctime.

atime:  It contains information on when the file content is read with tools or commands to display the file’s content like less, nano, vi, vim, cat, grep, head, etc. The atime timestamp changes and is updated every time the file is viewed.

mtime: It shows the last modification of a file’s content, including its name, but not its owner or permissions, only the file itself.

ctime: Like “mtime”, “ctime” also shows when a file is modified but it also gets updated when the ownership, group, or permission access on a file are changed. We can edit the atime and mtime but we can’t edit the ctime. The time gets updated when a file’s permissions are edited or when the files are really read or modified

Here are some basic examples of the touch command in Ubuntu 22.04 that explain the uses of this command.

Example 1: How to Create a New File

One of the basic usages of the touch command is generating new files. The following written command is the simplest and fastest way to create a file with the touch command. This method is used most often by system administrators.

$ touch file.txt

See the new file generated using the “ls” command as indicated in the output.

$ ls

A new empty file is generated.

Example 2: How to Create Multiple Files

Another usage or one may consider this an advantage of the touch command is that you can generate numerous files with just one command line.

$ touch filename1 filename2 filename3

Here, we generate three files.

Example 3: How to Evaluate the Modification Date and Time

For the purpose of evaluating the modification date and time of a file, you can use the stat command:

$ stat sample.txt

Now, let’s apply the touch command. Running the touch command on that particular file updates and displays the modification date and time of the file, making it the same as the date and time of the system.

$ touch sample.txt

The modification date and time of the file are equal to that of the system.

Example 4: How to Alter the Time and Date of File

Although, by default, the touch command makes the modification time of the file equal to that of the system as mentioned already. However, use the -t option to perform it manually.

$ touch -t 202212040957 sample.txt

Now, you can validate this change using the stat command.

$ stat sample.txt

The access time is equal to the modification time.

Example 5: How to Change the Access Time of File

Using the -a option along with the touch command allows you to alter the access time of a file:

$ touch -a sample.txt

The access time that is altered in the previous example is now updated.

Example 6: How to Alter the Modification Time of the File

To do this, use the -m option to alter the modification time of a file.

$ touch -m sample.txt

The modification time which we changed previously is now updated.

Example 7: How to Equalize the Modification Time of Two Files

When you use the -r option along with the touch command, it basically references a particular file and equals the modification time of a file equal to that of the file that you referenced.

$ touch -r file.txt sample.txt

The modification time of “file.txt” is equal to “sample.txt”.

Example 8: How to Prevent Creating New Files While Updating the Modification Time

Using the -c or a –no-create option with the touch command prevents the creation of new files that do not already exist when updating the modification time of a file.

$ touch -c filename1 filename2 filename4

No new file is generated.

Example 9: How to Alter the Modification Time of Link

For the purpose of updating the modification time of a link instead of a file, use the -h or a –no-dereference option:

$ touch -h sample.txt

The modification time of a link is updated.

Conclusion

The touch command in Ubuntu 22.04 serves various purposes such as generating an empty file or multiple files all at once. Moreover, this can be used to evaluate the modification time or access time of files. You can also update these timestamps of a file according to your desire using the touch command. These are some basic usages of this command, whereas the advanced usages consist of making the timestamp of one file equal to another, updating the modification time of a link, etc.

About the author

Syed Minhal Abbas

I hold a master's degree in computer science and work as an academic researcher. I am eager to read about new technologies and share them with the rest of the world.