Home » Linux Touch Command

Linux Touch Command

by Online Tutorials Library

Linux touch command

touch command is a way to create empty files (there are some other mehtods also). You can update the modification and access time of each file with the help of touch command.

Syntax:

Example:

TOUCH Command

Look above, we have created two files namely ‘myfile1’ and ‘myfile2’ through touch command. To create multiple files just type all the file names with a single touch command followed by enter key. For example, if you would like to create ‘myfile1’ and ‘myfile2’ simultaneously, then your command will be:


touch Options

Option Function
touch -a To change file access and modification time.
touch -m It is used to only modify time of a file.
touch -r To update time of one file with reference to the other file.
touch -t To create a file by specifying the time.
touch -c It does’t create n empty file.

Linux touch -a command

touch command with option ‘a’ is used to change the access time of a file. By default, it will take the current time of your system.

Syntax:

Example:

To see the access and change time of your file, you need to use stat command.

TOUCH -a Command

In above snapshot we have used ‘stat’ command (which we’ll learn in later tutorial)just to check the status of our directory (usr). So don’t get confused with that. Now you can match the access time of directory (usr) before and after passing the command ‘touch -a usr. It has taken the default access time of our system.

Linux touch -m command

The touch ‘-m’ option will help you to change only the modification time of a file.

Syntax:

Example:

TOUCH Command

Notice carefully in the above snapshot, only modification time has been changed.

Linux touch -r option

This command will update time with reference to the other mentioned command. There are two ways to use this command. Both works the same.

In below example, we want to change time-stamp of ‘Demo.txt’ with reference to ‘demo.txt’. Firstyou can write it as,

or

First we’ll see the status of both the files before using touch comand.

TOUCH Command

Now after using touch -r demo.txt Demo.txt command, time of Demo.txt has been changed wit reference to time of demo.txt

TOUCH Command

Linux touch -t command

with this command, you can change the access time of a file by determining a specified time to it.

It will modify the time by specified time instead of default time.

Format of time will be:

Below screenshot shows status of file 2.png before the touch command,

TOUCH -t Command

This screenshot shows that time of file 2.png has been modified by our specified time.

TOUCH Command

Linux touch -c command

Using ‘c’ option with touch command will not create an empty file, if that file doesn’t exist.

Syntax:

Example:

TOUCH Command

In above figure, we wanted to create file ‘movie’ but with ‘c’ option no file has been created.

Next TopicLinux rm

You may also like