Many Linux users, especially newbies, confuse the touch command for being the one that creates files. While it can do this, the command can do much more.
For example, if you use VPS hosting on your Linux machine, you can use the command to alter the timestamps of folders and files. In this brief post, we’ll explore the various options the command supplies and all the ways you can use it.
The Syntax of the Touch Command
If you intend to follow along with this tutorial, remember to connect to your VPS using an SSH client of your choice before using the command.
Here’s what the touch command’s syntax is:
|touch [options] [Name of the file]|
It’s quite straightforward – it’s the options that give you all the flexibility. Let’s have a look at all the available options.
The Touch Command’s Options
|Option||What It Does|
|-a||Alters the access time|
|-c||Prohibits the creation of a new file|
|-d=<string>||Alters the timestamp according to the date string|
|-h||Alters the timestamp for symbolic links|
|–help||Shows the help menu|
|-m||Alters the modification time|
|-r=<file>||Alters the timestamp based on the reference file|
|-t <stamp>||Alters the timestamp according to the “<stamp>” value, which has the date-time format|
|-v or –version||Shows the touch command version|
Having a general idea of what flags work with the touch command won’t do the trick. You must also learn what timestamps are. In every Linux-based OS, folders and files are assigned a timestamp by default. This timestamp keeps track of when the folder or file was last modified.
You might be familiar with what a timestamp is, but chances are, you don’t know about the three kinds of timestamps:
- Access time (atime): This timestamp indicates when the file was last read.
- Changed time (ctime): It indicates when the file’s metadata was last changed.
- Modification time (mtime): It indicates when the file’s content was last modified.
In addition to creating files and modifying file and folder timestamps, the touch command can also alter filer and folder access. The only exception is that the command cannot alter ctime, since there is no way to change ctime directly.
However, the atime and mtime of a file are both parts of the file’s metadata, and changing both of them also changes the ctime. When both those timestamps are modified, ctime is automatically set to the current time.
Real-life Examples of Using the Touch Command
#1 Creating a File
Run the touch command with just a filename and no options, and it will create a new file for you with the name and the format you specify. Here’s what that looks like:
If you specify the name of a file that exists, the file will be left untouched, but the access and mtime will be updated.
#2 Creating Several Files
The touch command can create several files in one go – all you need to do is write all the file names. Of course, you’ll need to put a space between the names.
|touch firstFile secondFile|
#3 Altering Access Time
The “-a” flag will change the atime of the file whose name you mention. Here’s how that would work:
|touch -a fileName|
To check the new atime of a file, you can run:
|ls -lu filename|
#4 Altering Modification Time
The “-m” flag will change the mtime of the file whose name you mention.
#5 Altering Both Access and Modification Time
Doing this is as simple as using both the a and m flags in one command, like so:
|touch -am fileName|
#6 Altering Access Time without Creating a New File
To modify the atime and mtime and make them the current time without creating a new file, you can use the c flag:
|touch -c newFile|
#7 Setting Specific atime and mtime Values
To set specific atime and mtime values, use the t flag, and then pass the date and time in YYYYMMDDhhmm.ss format. Of course, you’ll also need to mention the filename:
|touch -t 20232001547.30 fileName|
#8 Altering Timestamp Using a Symbolic Link
Using the touch command on a symbolically linked file name changes the timestamp of the original file that the linked file pointed to.
But if you want to alter the atime and mtime to the current time for a linked file, you can use the h option like so:
|touch -h SymbLinkedFile|
#9 Setting Timestamp Using a Reference File
The touch command makes it possible for you to set one file’s atime and mtime to another, essentially copying that information.
You can do this using the r option, and you will also need to write both the files’ names. Here’s how:
|touch -r referenceFile fileName|
#10 Setting the Date and Time with a String
You can set the timestamp to a string you pass using the d option. If you mention just a date, the time will be set to 00:00. Here’s an example of that:
|touch -d ‘9 Apr’ filename|
You can also pass just the time, and the date will be set to today’s date. Here’s what the command would look like:
|touch -d ’16:20′ filename|
Changing timestamps and access is something every Linux user will need to do at some point. The touch command makes accomplishing these things a breeze.
We’ve covered all the most helpful options of the command in this post. Plus, with the examples of use cases, you will have no problem using the command without issues.