Thursday, January 18, 2018

Linux “Touch” Command

In Linux every single file is associated with timestamps, and every file stores the information of last access time, last modification time and last change time. So, whenever we create new file, access or modify an existing file, the timestamps of that file automatically updated.


In this blog we will cover some useful practical examples of Linux touch command. The touch command is a standard program for Unix/Linux operating systems, that is used to create, change and modify timestamps of a file. Before heading up for touch command examples, please check out the following options.

1. How to Create an Empty File

The following touch command creates an empty (zero byte) new file called manish.

# touch manish


2. How to Create Multiple Files

By using touch command, you can also create more than one single file. For example the following command will create 3 files named, manishvijith and rip.

# touch manish vijith rip


3. How to Change File Access and Modification Time

To change or update the last access and modification times of a file called vijith, use the -a option as follows. The following command sets the current time and date on a file. If the vijith file does not exist, it will create the new empty file with the name.

# touch -a vijith

The most popular Linux commands such as find command and ls command uses timestamps for listing and finding files.

4. How to Avoid Creating New File

Using -c option with touch command avoids creating new files. For example the following command will not create a file called vijith if it does not exists.

# touch -c vijith


5. How to Change File Modification Time

If you like to change the only modification time of a file called vijith, then use the -m option with touch command. Please note it will only updates the last modification times (not the access times) of the file.

# touch -m vijith


6. Explicitly Set the Access and Modification times

You can explicitly set the time using -c and -t option with touch command. The format would be as follows.

# touch -c -t YYDDHHMM vijith

For example the following command sets the access and modification date and time to a file vijith as 17:30(17:30 p.m.) December 10 of the current year (2018).

# touch -c -t 12101730 vijith

Next verify the access and modification time of file vijith, with ls -l command.

# ls -l
total 2
-rw-r--r--.  1 root    root   0 Dec 10 17:30 vijith


7. How to Use the time stamp of another File

The following touch command with -r option, will update the time-stamp of file rip with the time-stamp of vijith file. So, both the file holds the same time stamp.

# touch -r vijith rip


No comments:

Post a Comment

Configure NFS Client

This example is based on the environment below. +----------------------+           |           +----------------------+ | [...