What is the touch command in Linux & how to use it?

The touch is the command in Linux and Unix-like operating systems. The primary use of the touch command is to createchange and modify time stamps of a file in Linux and Unix-like operating systems. Generally, there are two different commands to create a file in the Linux system.

  • If you want to create a file with content, then you can use the cat command.
  • If you want to create an empty file, then you can touch command

When we create any file using the touch command is empty. This command is handy when you don’t have data to store at the time of file creation. So, You can create single or multiple files at a time using this command,

Creating a single file with the touch command

So, you can create single or multiple files at a time using this command,

touch file_name

For Example: I’m creating a single file with the name Amit,

touch Amit
touch command
single file created with the touch command

Creating multiple files with the touch command

So, I’m going to create multiple files at a time with the file name, Sahil, Manish, Rahul

touch Sahil Manish Rahul
touch command in linux
Multiple files created with the touch command

Changing the access time of the file

If you want to change the access time, then you can use the -a option along with touch command. So you can find the below command useful for that.

touch -a file_name

For Example,

touch -a Amit

Here, Amit is the name of the file, and with the help of -a, we can change the access time. As a result, you can see in the below picture.

touch command in linux
Changing the access time of a file with touch command and argument -a

To check the file, If it is created or not. You can use -c with touch command

touch -c file_name

For example,

touch -c sonu

Note: If there is no such file with the name of sonu, It will not create any file. So, you can see the result in the below picture.

toch command in linux
Checking a file existence with the touch command and argument -c

To update access and modification time

If you want to update the access and modification time, then you can use argument -c and -d with touch command,

touch -c -d file_name

For example,

touch -c -d '06 may' amit

So, you can see in the below picture, the access and modification time has been changed,

touch command in linux
Updating access and modification time with touch command

To change the modification time only, you can use -m with touch command.

touch -m file_name

For example,

touch -m Rahul

here, Rahul is the file name. As a result, you can see in the picture,

touc command
Updating modification time only with touch command

To create a file with a specific time

touch -t YYMMDDHHMM file_name
touch -t 202004180600 Sudarshan

In the above example, we are creating a file for April 18, 2020, at 06:00 with the name of Sudarsan. You can see in the below picture,

touch command
Creating a file with a specific date and time

Conclusion

In this article, you learned about touch command in Linux, and it is a little hard to use for further use, but it is easy if you are creating a file only. So, I hope, you understand. But if you have any questions, you can ask in the comment section.

Also, you can read about the below topics.

Leave a Reply

Your email address will not be published.