[email protected]Office hours: Mon – Fri 9:00AM – 5:00PMLinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedJune 28, 2023

How to use touch command.

How To Use Touch Command.

In this article we will show you hou to use touch command.

 

Files Timestamps.

Before going into how to use the touch command, let’s start by reviewing the file timestamps.

A file in Linux has three timestamps:

  • atime (access time) – The last time the file was accessed/opened by some command or application such as cat , vim or grep .

  • mtime (modify time) – The last time the file’s content was modified.

  • ctime (change time) – The last time the file’s attribute or content was changed. The attribute includes file permissions, file ownership or file location.

To display the file status including the timestamps, use the stat command.

stat file_name

Creating a new file requires write permissions on the parent directory. Otherwise, you will receive a permission denied error.

 

How to Use the touch Command

In it’s simplest form when used without any options, if the file name specified as an argument doesn’t exist touch will create a new file.

If the file already exists touch will change the file last access and modification times to the current time.

For example, if the file file1 doesn’t exist the following command will create it otherwise, it will change its timestamps:

touch file1

To create or modify multiple files at once, specify the file names as arguments:

touch file1 file2 file3

If you don’t want the touch command to create new files, use the -c (–no-create) option.

For example, if the file file1 exist the following command will change the file timestamps otherwise, it will do nothing:

touch -c file1

 

Changing only access or modification times

By default, if no option is used touch will update the file last access and modification times to the current time. By using the -a and -m options, you can change only one of these timestamps.

 

Change only the access time

Use the -a option to change only the file’s access time:

touch -a file1

 

Change only the modify time

Use the -m option to change the file’s modify time:

touch -m file1

When changing the modify time, the change time will be also updated.

 

Setting specific timestamps

The touch command also allows us to update or create a file with a specific time other than the current time.

Use the -d (–date=) option to specify a date string and use it instead of the current time.

For example, to change both the last access and modification times of file1 to 1 June 2018 11:02 you would use the following command:

touch -d '1 June 2018 11:02' file1

The date string needs to be enclosed in single quotes.

You can also provide a partial date-time string to the touch command. Providing only the date, automatically changes the year to the current one:

touch -d '12 June' file1

Use the -t option to specify a timestamp and use it instead of the current time. The timestamp argument needs to be in the following format:

use [[CC]YY]MMDDhhmm[.ss]

For example, the following command will set the last access and modification times of file1 to 1 June 11:02 of the current year.

touch -t 06011102 file1

 

Using the timestamp of another file

The -r (–reference=) option allow us to specify a reference file and use its timestamps instead of the current time.

For example, the following command will tell touch to use the times of file1 for file2:

touch -r file1 file2

 

Changing symbolic link timestamp

By default, if you use a touch command on a symbolic link it will change the timestamps of its referenced file.

Use the -h (–no-dereference) to modify the timestamp of the symlink.

For example, to change the timestamps of the symbolic link symlink1 you would use:

touch -h symlink1

 

Conclusion

By now you should have a good understanding of how to use the Linux touch command.

Keep reading
How to Implement Network Segmentation and Resource Isolation in Linux.
February 6, 2024

How to Implement Network Segmentation and Resource Isolation in Linux.

How to Implement Network Segmentation and Resource Isolation in Linux. Securing and managing networks in a Linux environment involves advanced practices like network segmentation and resource isolation. This guide explores these concepts, providing detailed instructions and examples for…

Read article
How to open or close ports in Linux.
February 6, 2024

How to open or close ports in Linux.

How to open or close ports in Linux. Effectively managing ports on your Linux system is crucial for both security and service accessibility. This comprehensive guide will provide a detailed walkthrough on opening and closing ports using the robust tools iptables and ufw. Understanding these…

Read article
How to use ypdomainname command.
January 23, 2024

How to use ypdomainname command.

How to use ypdomainname command. In the realm of Linux networking, the ypdomainname command plays a crucial role in the context of the Network Information Service (NIS). NIS, formerly known as Yellow Pages, is a distributed database service that facilitates the sharing of network configuration and…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, colocation, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Office hours: Mon – Fri 9:00AM – 5:00PM