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
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.
To see the new file generated use the ‘ls’ command as indicated in the 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.
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:
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.
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.
Now you can validate this change using the stat command.
The access time is equal to modification time.
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.
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.
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.
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.
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.
The modification time of a link has been updated.
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.