touch Command in Ubuntu 22.04

In Linux, the touch command creates empty or multiple empty text files. Apart from that, it can also be used to perform various operations on the timestamps of the files. Its working and usage are the same throughout all the Linux distributions. This post offers the use of the touch command in Ubuntu 22.04.

  • What is a touch Command?
  • How to Use the touch Command in Ubuntu 22.04?
    • Example 1: Create a New File
    • Example 2: Create Multiple Files
    • Example 3: Change Modify Time of a File
    • Example 4: Check the Modified Date & Time of the File
    • Example 5: Change the Time & Date of the File
    • Example 6: Change the Access Time of the File
    • Example 7: Change the Modify Time of File
    • Example 8: Match the Date & Time of the Two Files

What is a touch Command?

Basic usage of the touch command in Ubuntu will be performed in this tutorial, from creating files to changing the timestamps of files. The syntax of the touch“ command is as follows:

$ touch [Options] file

The “file” represents the filename, and the list of options supported by this command can be checked through the following command:

$ touch --help

These options will be used to demonstrate the usage of the touch command in Ubuntu 22.04.

How to Use touch Command in Ubuntu 22.04?

This section enlists several examples to demonstrate the usage of the touch command in Ubuntu 22.04.

Example 1: Create a New File

To create a new file, write the “touch” keyword with the filename. The practical demonstration of this command can be seen below:

$ touch test.txt

To verify the file creation, use the “ls” command:

A new file named “test” has been created, and “.txt” shows it’s a text file.

Example 2: Create Multiple Files

We can create multiple files in Ubuntu 22.04 using the single touch command in two ways:

Using Different Names

Different file names are used to create several files using the “touch” command. Let’s create multiple files using a single touch command:

$ touch test1.txt test2.txt test3.txt  

Multiple files are created using separate names for every file in the single touch command.

Create Files With Sequence Names

Sequence-wise files can be created using the touch command. To create files in series like file1 to file5, use the following command:

$ touch testFile{1..5}

Five files with sequence names are created using the “touch” command.

Example 3: Change Modify Time of a File

To change the file’s modified time without creating the file, use “-c” or “–no–create” as shown below:

$ touch -c test1.txt 
$ stat test1.txt

The “stat” command can be used to check the status of the specific file as we did in the above image. This command will also be used in the upcoming examples as well.

Example 4: Check the Modified Date & Time of the File

First, check the stats of the “test1.txt” using the following command:

$ stat test1.txt

The touch command will modify its date and time to the current timestamp via the following command:

$ touch test1.txt 
$ stat test1.txt

The modified date and time are the same as the execution time of the “touch” command.

Example 5: Change the Time & Date of the File

System time is the default creation time of the file. To manually change the date and time of the file to your desired time, we can use “-t”:

$ touch -t 202209301635.22 test1.txt 
$ stat test1.txt

Note: Time format is [[CC]YY]MMDDhhmm[.ss].

  • CC= First 2 digits of the year (Optional) (20),
  • YY = Last 2 digits of the year (22),
  • MM = Month (09),
  • DD = Date (30),
  • hh = Hours (16),
  • mm = Minutes (35),
  • .ss = seconds (Optional) (22).

The output shows the access time, and the file’s modification time is changed to the manually entered time.

Example 6: Change the Access Time of the File

The last time the user checks the file’s content is the access time which can be modified using the “a” flag. We can only change the access time of a file with this command:

$ touch -a test1.txt 
$ stat test1.txt

Access time changes with the above command.

Example 7: Change the Modify Time of File

The file modification time can be changed using the “m” option with the touch command as follows:

$ touch -m test1.txt 
$ stat test1.txt

Modify time is updated to the current system time.

Example 8: Match the Date & Time of the Two Files

The date and time of the two files can be matched using the “r” option (uses file’s time) with the “touch” command as shown below:

$ touch -r test1.txt test2.txt 
$ stat test1.txt test2.txt

The timestamp of “test1.txt” is changed to the timestamp of “test2.txt”, so both the timestamps are equal.

Conclusion

Touch command in Ubuntu 22..04 serves multiple-purpose functionality, such as creating a file “$ touch <filename>” or generating multiple files simultaneously. Moreover, changing the modification time, the access time of the file, and making two files times equal is also done with the “touch” command. In this article, we have covered the basic workings of the touch command.