Use the touch command on Linux and seamlessly bring modifications to the timestamp of any file. Apart from that, you can even create new files, assign desired timestamps, and alter the concerned access time for a concerned file pretty easily.
Pre Requisites
Before you learn how to use the touch
command in Linux, you must have all the utilities ready. Here is the list of requirements for invoking and employing the touch
command:
- A fully functional Linux system
- Full access to the Terminal or Command Line utility
- Knowledge of basic Terminal commands
touch Command: Fundamental Syntax
The basic syntax for the touch command in Linux looks something like this:
$ touch [options] [name of the file/directory]
One great thing about the touch command is that it is very flexible in nature. While you can use it without any option, you can also include multiple ones for resolving advanced queries. The options associated with the touch command can either be in short or long-form. It is crucial you know about each of them in the most detailed manner possible.
How to Use the Touch Command in Linux
Having already talked about the fundamental syntax and availability of options, it is time to learn about using them.
–a
: The -a
flag is used to alter the access time
-c: It helps avoid the creation of any new file within a specific directory. Alternatively, you can use the --no-create
flag as well.
-d=<string>
: This flag is meant to help alter the concerned timestamp by specifying a particular date. The —date=<string>
works the same way.
-f
: Employing the -f
option with the touch command, you can make sure that no changes occur in the directory.
-h
: With the help of the -h
flag, you can alter the symbolic link’s timestamp. An alternative for this can be the —no-difference
flag.
-m
: The -m
flag changes the concerned file modification time.
–r=<file>
: If you’re after altering the timestamp to any desired (usually referenced) file’s timestamp, you can either use the -r=<file>
option or the --reference=<file>
-t <stamp>
: By invoking the touch command alongside the -t <stamp>
option, you can modify that particular timestamp.
--help
: It displays the entire help menu
--version/-v
: This options lets you check the version of touch installed.
Using touch Command on Linux to Create a File
One of the basic uses of the touch command on Linux is creating a file. It is indeed one of the straight-forward approaches as you don’t even need to specify any option. All you need to do is launch the Terminal using the “Ctrl+Alt+T” key combination and pass the touch command alongside the desired name.
Input:
$ touch [name of the file]
Verification
To verify if the file creation was successful, you can simply list the directory contents by invoking the ls command. If you’re after a file that already exists, what touch will do is alter the timestamp. However, it is worth noting that the contents and the permissions remain the same.
Using touch Command on Linux to Create Multiple Files
Using the touch command on Linux and creating multiple files remains the same. The only difference is that you’ll need to specify the names of each file you’re after here. For instance, if you want to create three files named testfile1
, testfile2
, and testfile3
, the concerned command should look something like this:
$ touch testfile1 testfile2 testfile3
But what if the need is to create 100 files? Do you think inputting the names 100 times will be an efficient method? Of course not. Luckily, there exists a promising way out.
The touch
command brings a brilliant utility where a large batch of files can be created by running a single line of code. The format for the command is something like the following:
$ touch [name of the file] {[start]..[finish]
For e.g., To create 100 files named testfiledistroid, use the following command:
$ touch testfiledistroid {1..100}
Using touch Command on Linux to Set Specific Timestamps: The Time String Method
Another great utility of the touch
command is in specifying the concerned timestamp. The command for that obeys the following format:
$ touch -t [desired timestamp] [name of the concerned file]
Before specifying a timestamp, you must know the eligible format. Here is what it should look like:
[[CC]YY]MMDDhhmm[.ss]
Suppose you want the timestamp for the file testdistroid to be 1st January 2021 (midnight). In that case, the format should be:
$ touch -t 20210101000 testdistroid
To check the timestamp details, you can use the ls
command and the --full-time
flag.
Using touch Command on Linux to Set Specific Timestamps: The Date String Method
To set a timestamp for your file using, you’ll need to use the touch
command alongside the -d
option in the following manner:
$ touch -d [string] [name of the desired file]
The date string method is a flexible alternative. Interestingly it also accepts human-readable commands like today and tomorrow. For instance, if you’re looking to change the timestamp for testdistroid file and set it to today, invoke the following command:
$ touch -d today testdistroid
Changing the Access Time to Current
To change any file’s access time to the current time, use the touch
command together with the -a
flag. Here is how:
$ touch -a [name of the file]
Other Quick Uses of touch Command on Linux
Changing modification time to current: $ touch -m [name of the file]
Changing modification time to explicitly: $ touch -mt [desired timestamp] [name of the file]
Avoid creating new file: $ touch -c [name of the file]
Setting timestamp using a reference file: $ touch -r [desired reference file] [name of the file]
Setting timestamp using a symbolic link: $ touch -h [name of the file]
And that’s everything you need to know about the touch command on Linux. Here in this write-up, I’ve walked you through all the situations where you can use the touch command and conclude a number of activities.
If this guide helped you, please share it.