The Touch Command in Linux
Any Linux system comes with a handful of tools pre-installed. The touch command is one such tool. The linux touch command is to modify the timestamps in the file metadata such as access and modification times. However, it’s also widely used for creating a new empty file in the desired location. With the proper file permission, the touch command can change the timestamps of any file. If the file didn’t exist prior, then it creates the file instead. In this guide, we’ll dive deeper into the usage of the touch command in Linux.
Prerequisites
To perform the steps demonstrated in this guide, you’ll need the following components.
The Touch Command Syntax
The core command structure of the touch command is as follows:
0 seconds of 6 minutes, 56 secondsVolume 0%
$ touch <options> <file_or_dir_name>
This tool can work without any options provided. It also supports multiple options for advanced queries. Some options have abbreviated forms. If an option requires additional info, then it’s mandatory to provide.
The Touch Command Options
Here are some of the common options available for the touch command.
File Timestamps
Before jumping further, you need to be aware of the different types of timestamps every single file has.
Click Here to Watch In Hindi On YouTube
To view the value of atime, use the following command:
$ ls -lu
To view the value of ctime, use the following command:
$ ls -lc
To view the value of mtime, use the following command:
$ ls -l
Using the Touch Command
We’ve covered all the basics of the touch command. It’s time to put them into action.
Creating a File
This is, by far, one of the most popular usages of the touch command. To create a new file, run the following command:
$ touch <file_name>
For example, create a new file test.txt:
$ touch test.txt
As mentioned earlier, if the file doesn’t exist, touch creates it automatically. Verify the change:
$ ls -l
Creating Multiple Files
With the touch command, you can create multiple files at the same time. To do so, use the following command structure:
$ touch <filename_1> <filename_2>
For example, create two files test_1.txt and test_2.txt:
$ touch test_1.txt test_2.txt
Verify the changes:
$ ls -l
We can also create large batches of files (assuming the file names are sequential). To do so, enter the first and last element of the element in curly braces. For example, to create files test_1.txt to test_10.txt, use the following command:
$ touch test_{1..10}.txt
Verify the action:
$ ls -l
It also works for alphabets. For example, to create files test_a.txt to test_b.txt, use the following command:
$ touch test_{a..j}.txt
Verify the action:
$ ls -l
Setting Specific Timestamp
To set a specific timestamp to a file, the command structure is as follows:
$ touch -t <timestamp> <file_name>
The <timestamp> has a specific structure.
$ [[CC]YY]MMDDhhmm[.ss]
Here,
The items in the square brackets are optional. If the value of YY is 0-68, then CC is automatically assumed 20. If the value of YY is 69-99, then CC is assumed 19.
Let’s put it into action. Change the timestamp of test.txt to January 1, 1999 (12:00).
$ touch -t 199901011200 test.txt
Verify the changes:
$ ls -l --full-time test.txt
Setting Specific Timestamp Using Date String
A more comfortable way of changing the timestamp is by using the date strings. The syntax for using date strings is as follows:
$ touch -d <date_string> <file_name>
One benefit of using the date string is its flexibility. It supports various human-readable textual forms, for example,
Let’s change the timestamp of test.txt to “1 January 2011”.
$ touch -d "1 January 2011" test.txt
Verify the change:
$ ls -l --full-time test.txt
Changing File Access Time
There are two ways to change the atime of a file.
Changing file access time to current
The following touch command will set the access timestamp of the target file to the current time:
$ touch -a <file_name>
For example, change the atime of test.txt to the current time:
$ touch -a test.txt
Check the change:
$ ls -lu --full-time test.txt
Changing file access time explicitly
We can also specify the file access time explicitly. To do so, we’ll combine the “-a” and “-t” flags together. The command structure will look like this:
$ touch -at <timestamp> <file_name>
For example, the following command will set the file access time of test.txt to January 1, 1999:
$ touch -at 9901010000 test.txt
Verify the change:
$ ls -lu --full-time test.txt
Changing Modification Time
Using the touch command, you can change the modification time (mtime) of a file. There are two ways of doing so.
Changing mtime to current
The following touch command will set the mtime of a file to the current time.
$ touch -m <file_name>
For example, change the mtime of test.txt to the current time:
$ touch -m test.txt
Verify the change:
$ ls -l --full-time test.txt
Changing mtime explicitly
We can combine “-m” and “-t” flags together to set a custom mtime. The syntax is as follows:
$ touch -mt <timestamp> <file_name>
For example, changing mtime to “1 January 1999” would look like this:
$ touch -mt 9901010000 test.txt
Verify the changes:
$ ls -l --full-time test.txt
Changing mtime and atime
With the touch command, we can use multiple options. Using this feature, we can set the mtime and atime of a file in a single command. The syntax would look like this:
$ touch -am <file_name>
Note that in this example, the time is changed to the current time. If you want a specific time, then you have to use the “-t” flag for a specific timestamp.
Avoid Creating New Files
If used with the flag “-c”, the touch command won’t create the file if it doesn’t exist.
$ touch -c <file_name>
Changing Timestamp Using a Reference File
We can tell touch to use the timestamps of a reference file. The command structure is as follows:
$ touch -r <reference_file> <file_name>
The target file will inherit the timestamps from the reference file.
Final Thoughts
The touch utility is a key terminal program when working with files in Linux. This tutorial demonstrates some general usage of the touch command. For more comprehensive details about the available options, check out the man page:
$ man touch
From <https://linuxhint.com/touch-command-linux/>