How to use the Linux touch command + Examples

The Linux touch command is mainly used to create empty files and change timestamps of files or folders. File timestamp information consists of three attributes: access time, modification time, and change time.

With this tutorial you will be able to learn about touch, its options, uses and, at the same time, you will be able to see useful examples.

What is a timestamp in Linux?

In Linux, every file and folder has an associated timestamp that provides information about when a file’s content or attributes were modified. There are three types of timestamps:

  • Access time (atime): the last time a file was read.
  • Modification Time (mtime): The last time the content of a file was modified. Like access time, it is also part of the file state metadata.
  • Changes Made Time (ctime): The last time a file’s metadata (for example, permissions) was modified

Because atime and mtime are part of a file’s state metadata, changing a file’s atime or mtime results in ctime, which is automatically set to the current date. There is no way to set or change the ctime manually.

The Ubuntu – Linux touch command is mainly used to manipulate the access and modification time of files by using various options as described below. Remember, before using the touch command, you will need to access your using !

Linux touch command syntax

The touch command syntax is:

touch

The following section lists the uses of the Linux touch command by listing the various options available.

Create a file using Touch

The touch command without any options creates a new file. If the file exists, the command will update the access and modification time to the current time without changing its content:

See also  WooCommerce Tutorial: How to Install eCommerce in WordPress

touch file_name.txt

Create multiple files using Touch

It is also possible to create multiple files using a single touch command. To do this, specify the file names with spaces between them. It will look like this on the command line:

touch filename1.txt filename2.txt filename3.txt

You can automatically generate file names as they are created, as in the following example:

touch file_name{1..3}.txt

The touch command above will create three files called file_name1.txt, file_name2.txt Y filename3.txt.

Change access time using touch command

To change the access time of a file to the current time, use the option –a followed by the file name with the touch command, as in the following example:

touch -a file_name.txt

Change Modify Time with Touch

The option -m together with the touch command changes the modification time of a file to the current time:

touch -m filename1.txt

Change access and modification time with Touch

To change both the access time and the modification time with a single command, use the options –a Y –m boards:

touch -am filename1.txt

Change access time without creating a new file

If you want to change the access and modification time of an existing file to the current time without actually creating a new file. Use the option –c followed by the file name with the touch command.

touch -c file_name.txt

Set a specific access and modification time with Touch

It is also possible to set the access and modification time of a file to a particular date using the option -t followed by the date to consider for the change. It would look like this:

See also  Setting up a Linux VPN Server with OpenVPN - Step by Step Guide

touch -t 201903081047.30 file_name.txt

The date and time format must be in CCYYMMDDhhmm.ss where:

  • CC: the first two digits of the year
  • YY: the second two digits of the year
  • MM: The month of the year
  • DD: The day of the month
  • hh: the time of day
  • Mm: The minute of the hour
  • SS: The second of the minute

Change the timestamp of a symbolically linked file

When you use a symbolically linked filename with Ubuntu’s touch command, the timestamp information for the original file, that is, the file pointed to by the link file, is modified. To change the access and modification time to the current time for a symbolically linked file, use the –h:

touch -h symbolic_link_file

Set timestamp using another file as reference

The Linux touch command can also set the access and modification time of a file by reading the timestamp information of another file. For example, the following touch command with the option -r will scan the timestamp information from the file reference.txt and will set these timestamp values ​​in the file file_name.txt. Let’s look at the example:

touch -r reference.txt file_name.txt

Specify the date and time as a string using the touch command

You can also specify the date and time as a string of characters using the option -d. The following Linux touch command example sets the date to March 8 and the time is automatically set to 00:00:

touch -d ‘Mar 8’ file_name.txt

Instead of specifying the date as a string, you can specify the time as a string. In that case, the date will automatically become the current date:

See also  How to create a contact form in WordPress

touch -d ’20:10 ‘file_name.txt

conclusion

This tutorial covers the uses of the Linux touch command including the most common options. If you have any problems associated with the use of the Linux touch command, you can ask us in the comments or read the manual using the terminal.

Deyi is a digital marketing enthusiast, with a background in web design, content creation, copywriting, and SEO. She is part of ‘s SEO & Localization team. In her free time, she likes to develop projects, read a book or watch a good movie.

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x