The Touch Command in Linux
Any Linux machine comes with a handful of gear pre-installed. The contact command is one such software. The linux contact command is to change the timestamps within the report metadata reminiscent of get entry to and amendment instances. However, it’s additionally broadly used for developing a brand new empty report within the desired location. With the correct report permission, the contact command can exchange the timestamps of any report. If the report didn’t exist prior, then it creates the report as an alternative. In this information, we’ll dive deeper into the use of the contact command in Linux.
Prerequisites
To carry out the stairs demonstrated on this information, you’ll want the next parts.
The Touch Command Syntax
The core command construction of the contact command is as follows:
$ contact choices> file_or_dir_name>
This software can paintings with none choices equipped. It additionally helps a couple of choices for complicated queries. Some choices have abbreviated paperwork. If an choice calls for more information, then it’s necessary to offer.
The Touch Command Options
Here are one of the commonplace choices to be had for the contact command.
- –model or -v: Displays this system model.
- –lend a hand: Prints the lend a hand menu.
- -a: Changes the get entry to time.
- -m: Changes the amendment time.
- -no-dereference or -h: Change the timestamp of a symbolic hyperlink. Learn extra about symbolic hyperlinks in Linux.
- -c or –no-create: Not developing the report if it doesn’t exist.
-
–date=
or –d= : Timestamp is modified in step with the date string. -
–reference=
or –r= : Timestamp is modified in step with the reference report. -
-t
: Changes the timestamp. Here, the stamp is within the date/time structure.
File Timestamps
Before leaping additional, you wish to have to pay attention to the various kinds of timestamps each and every unmarried report has.
- Access time (atime): This timestamp helps to keep observe of when the report used to be final learn by way of any command, for instance, grep and cat.
- Change time (ctime): This timestamp helps to keep observe of the final time any belongings of the report used to be modified. Actions like report renaming, permission amendment, or shifting the report, and many others. will exchange its worth.
- Modification time (mtime): This timestamp tracks when the content material of the report used to be final modified.
To view the price of atime, use the next command:
To view the price of ctime, use the next command:
To view the price of mtime, use the next command:
Using the Touch Command
We’ve lined the entire fundamentals of the contact command. It’s time to position them into motion.
Creating a File
This is, by way of a long way, one of the in style usages of the contact command. To create a brand new report, run the next command:
For instance, create a brand new report check.txt:
As discussed previous, if the report doesn’t exist, contact creates it routinely. Verify the exchange:
Creating Multiple Files
With the contact command, you’ll create a couple of recordsdata on the similar time. To achieve this, use the next command construction:
$ contact filename_1> filename_2>
For instance, create two recordsdata test_1.txt and test_2.txt:
$ contact test_1.txt test_2.txt
Verify the adjustments:
We too can create huge batches of recordsdata (assuming the report names are sequential). To achieve this, input the primary and final part of the part in curly braces. For instance, to create recordsdata test_1.txt to test_10.txt, use the next command:
Verify the motion:
It additionally works for alphabets. For instance, to create recordsdata test_a.txt to test_b.txt, use the next command:
Verify the motion:
Setting Specific Timestamp
To set a particular timestamp to a report, the command construction is as follows:
$ contact -t timestamp> file_name>
The
Here,
- CC: The first two digits of the yr.
- YY: The final two digits of the yr.
- MM: Month
- DD: Day of the month
- hh: Hour
- mm: Minute
- ss: Seconds
The pieces within the sq. brackets are non-compulsory. If the price of YY is 0-68, then CC is routinely assumed 20. If the price of YY is 69-99, then CC is thought 19.
Let’s put it into motion. Change the timestamp of check.txt to January 1, 1999 (12:00).
$ contact -t 199901011200 check.txt
Verify the adjustments:
$ ls -l –full-time check.txt
Setting Specific Timestamp Using Date String
A extra comfy means of adjusting the timestamp is by way of the use of the date strings. The syntax for the use of date strings is as follows:
$ contact -d date_string> file_name>
One advantage of the use of the date string is its flexibility. It helps quite a lot of human-readable textual paperwork, for instance,
- Relative instances: “5 years ago”, “yesterday”, “next tuesday” and many others.
- Calendar dates: “1 January 1969”, “2 February 2022” and many others.
- Weekdays: “Sunday”, “Monday” and many others.
- Time of day: “2:22am”, “12:22pm” and many others.
Let’s exchange the timestamp of check.txt to “1 January 2011”.
$ contact -d “1 January 2011” check.txt
Verify the exchange:
$ ls -l –full-time check.txt
Changing File Access Time
There are two tactics to switch the atime of a report.
Changing report get entry to time to present
The following contact command will set the get entry to timestamp of the objective report to the present time:
For instance, exchange the atime of check.txt to the present time:
Check the exchange:
$ ls -lu –full-time check.txt
Changing report get entry to time explicitly
We too can specify the report get entry to time explicitly. To achieve this, we’ll mix the “-a” and “-t” flags in combination. The command construction will seem like this:
$ contact -at timestamp> file_name>
For instance, the next command will set the report get entry to time of check.txt to January 1, 1999:
$ contact -at 9901010000 check.txt
Verify the exchange:
$ ls -lu –full-time check.txt
Changing Modification Time
Using the contact command, you’ll exchange the amendment time (mtime) of a report. There are two tactics of doing so.
Changing mtime to present
The following contact command will set the mtime of a report to the present time.
For instance, exchange the mtime of check.txt to the present time:
Verify the exchange:
$ ls -l –full-time check.txt
Changing mtime explicitly
We can mix “-m” and “-t” flags in combination to set a customized mtime. The syntax is as follows:
$ contact -mt timestamp> file_name>
For instance, converting mtime to “1 January 1999” would seem like this:
$ contact -mt 9901010000 check.txt
Verify the adjustments:
$ ls -l –full-time check.txt
Changing mtime and atime
With the contact command, we will be able to use a couple of choices. Using this selection, we will be able to set the mtime and atime of a report in one command. The syntax would seem like this:
Note that on this instance, the time is modified to the present time. If you wish to have a particular time, then it’s important to use the “-t” flag for a particular timestamp.
Avoid Creating New Files
If used with the flag “-c”, the contact command received’t create the report if it doesn’t exist.
Changing Timestamp Using a Reference File
We can inform contact to make use of the timestamps of a reference report. The command construction is as follows:
$ contact -r reference_file> file_name>
The goal report will inherit the timestamps from the reference report.
Final Thoughts
The contact software is a key terminal program when operating with recordsdata in Linux. This instructional demonstrates some normal utilization of the contact command. For extra complete information about the to be had choices, take a look at the person web page:
Happy computing!