Categories: Ubuntu

Touch Command in Ubuntu 22.04

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 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 have elaborated 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.

Basic Examples of Touch Command in Ubuntu 22.04

Sponsored

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

How to generate a new file

One of the basic usages of the touch command is generating new files. The command written below 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

To see the new file generated use the ‘ls’ command as indicated in the output.

Output

A new empty file has been generated.

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

Output

Here we have generated three files.

How to evaluate 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

Output

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

$ touch sample.txt

Output

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

How to alter the time and date of file

Although by default the touch command will make 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 202214040987 sample.txt

Now you can validate this change using the stat command.

$ stat sample.txt

Output

The access time is equal to modification time.

Sponsored

How to change the access time of file

Using the -a option along with the touch command will allow you to alter the access time of a file.

$ touch -a sample.txt

Output

The access time that was altered in the previous example has now been updated.

Alter the modification time of file

In order to do this use the -m option to alter the modification time of a file.

$ touch -m sample.txt

Output

The modification time which we changed previously has now been updated.

Advanced Examples of Touch Command in Ubuntu 22.04

Here we have demonstrated some advanced usages of the touch command in Ubuntu 22.04.

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 you referenced.

$ touch -r file sample.txt

Output

The modification time of doc1 is equal to sample.txt

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 filename3

Output

No new file is generated.

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

Output

The modification time of a link has been updated.

Conclusion

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.

Ubuntu Server Admin

Recent Posts

From sales development to renewals: Mariam Tawakol’s career progression at Canonical

Career progression doesn’t follow a single path – and at Canonical, we embrace that. Our…

10 hours ago

In pursuit of quality: UX for documentation authors

Canonical’s Platform Engineering team has been hard at work crafting documentation in Rockcraft and Charmcraft…

10 hours ago

Ubuntu 24.10 (Oracular Oriole) reached End of Life on 10th July 2025

This is a follow-up to the End of Life warning sent earlier to confirm that…

1 day ago

Canonical announces Charmed Feast: A production-grade feature store for your open source MLOps stack

July 10, 2025: Today, Canonical announced the release of Charmed Feast, an enterprise solution for…

1 day ago

Ubuntu Adding NVIDIA 575 Driver Support for 24.04 & 22.04 LTS

For NVIDIA users, the latest 575 series driver will be available soon in Ubuntu repository.…

2 days ago

Amarok Released 3.3 with Qt6/KF6 Port & GStreamer Backend

Amarok, the free open-source KDE music player, released new 3.3 version on Tuesday, named “Far…

2 days ago