Categories
Planning

How to use touch to create empty files and modify timestamps

How to use touch to create empty files and modify timestamps

What is the Touch Command?

As we progress through our GNU Core Utilities series, we move on to the touch command. The touch command in Linux is used to manipulate the timestamp associated with files. With it you can change the modification time, access time, or even copy another files timestamps. The touch command is also often used just to create an empty file. Below we will show some examples of how to use the touch command.

Create an Empty File with Touch

To create an empty file, you simple run the touch command and specify a filename after it.

This creates a empty file named my file. Let’s take a look at the file and notice the timestamp which marks the time the file was created.

Timestamp formats

There are several formats you can use while manipulating timestamps.

Stamp Format

The STAMP uses a rigid format which can be used the with -t option.

  • CC = First two digits of the year
  • YY = Last two digits of the year
  • MM = Month
  • DD = Specifies the Day (If less that 10 use leading zero ie. 05 for May)
  • hh = Hour (If less than 10 use leading zero)
  • mm = Minutes (If less than 10 use leading zero)
  • ss = Seconds (If less than 10 use leading zero)

As an example this is what it would look like to change a file to October 8th 2018, at 07:30 and 30 seconds.

String Format

The STRING format a free format human readable date string. Below are some of the options you can use with the string format.

Changing a File Timestamp

You can use touch to modify two different timestamps of a file. If you do not specify which you want to modify, the will both be changed.

Modifying the Access Time

The access time or atime of a file is changed whenever a file it opened for reading. If we want to use touch to modify the atime only, we must specify the -a option.

Using the example above, we created a blank file on February 1 at 12:01. We can use the ls command to check the last access time of the file.

This shows us that the file has not been read since the file was created. Now let’s use the cat command to read the file, and see what happens.

As you can see the atime attribute was updated because we access the file with cat.

Now we can use the touch command to modify the atime to be whatever we want. The format for setting a timestamp is Let’s change it to January 22 2019 at 07:30 like so.

Let’s use “ls -l —time=atime” to check the change.

We successfully changed the atime of the file. We could have also used the String format like so:

Modifying the Modified Time

The mtime or last modified time can easily be modified using the same syntax. If we want to use touch to modify the mtime only, we must specify the -m option. Here are some examples.

Using the stamp format:

Using the string format:

Copying Another File’s Timestamp

You can copy the timestamp from another file using the -r (reference) option.

As you can see, this copied the timestamp from oldfile to myfile:

touch command is one of these little but extremely useful tools in Unix and Linux which you may have used for quite sometime before realizing their full potential. In short, it updates file timestamps – access and modification ones (atime and mtime respectively).

Why modify file timestamps?

There are quite a few legitimate reasons why you may want to update timestamps on a certain file. Ranging from source control approaches to storage usage analysis, there are processes out there which rely on the timestamps associated with each file and directory of yours.

After all, it’s always useful to know when the file was last modified or when somebody tried to access its contents.

Changing timestamps of a time to the current system time

The default behavior of touch command is to change all three timestamps associated with a file to the current system time.

You simply specify the filename as a command line parameter, no oother options are needed. If there isn’t a file with the specified name, touch command will create it for you if permissions allow it:

As you can see from the example, the file which isn’t originally found, gets created by the touch command and gets its timestamps set to the current system time and date.

Changing file timestamps to a specific date and time

If you have a specific time and date you would like to be used for all the timestamps of a file or directory, touch command will gladly accempt a timestamp template with -t command line option.

Template for the timestamp is quite thorough: [[CC]YY]MMDDhhmm[.ss], but it’s entirely up to you whether to specify the year (either two-digit or a full form) or not.

This example resets the date to October 16th:

As you can see from the output, both access time and modification time got updated. The reason change time (ctime) is set to a different date is because this field reflects the last update to the inode behind a file, and always reflects the current time. In other words, it’s set to Nov 18th 2008 because of the date of writing this example.

If you fancy adding a year to the timestamp specification, you can specify something from both past and future.

Here’s how easy it is to set atime and mtime to the Oct 16th, 2010 date:

Modifying atime of a file in Unix

Similar to the commands above, you can use -a option to make touch only update the access time field of a file:

Modifying mtime of a file in Unix

If you’re interested in updating the modification date only, use -m option:

Using a reference file to set atime and mtime

Finally, the really useful option for synchronizing access and modification time fields between multiple files is to use reference file. A reference file is the file which already has the timestamps you’d like to copy:

By specifying this file using a -r option, you can use the touch command to set the same atime and mtime values to any file of yours:

The touch command is a standard command used in UNIX/Linux operating system which is used to create, change and modify timestamps of a file. Basically, there are two different commands to create a file in the Linux system which is as follows:

  • cat command: It is used to create the file with content.
  • touch command: It is used to create a file without any content. The file created using touch command is empty. This command can be used when the user doesn’t have data to store at the time of file creation.

Using touch Command

Initially, we are in home directory and this can be checked using the pwd command. Checking the existing files using command ls and then long listing command(ll) is used to gather more details about existing files. As you can see in the below figure there is no existing files.

How to use touch to create empty files and modify timestamps

Touch command Syntax to create a new file: You can create a single file at a time using touch command.

Syntax:

The file which is created can be viewed by ls command and to get more details about the file you can use long listing command ll or ls -l command . Here file with name ‘File1‘ is created using touch command.

How to use touch to create empty files and modify timestamps

Touch command to create multiple files: Touch command can be used to create the multiple numbers of files at the same time. These files would be empty while creation.

Syntax:

Multiple files with name Doc1, Doc2, Doc3 are created at the same time using touch command here.

How to use touch to create empty files and modify timestamps

touch Command Options

Like all other command Touch command have various options. These options are very useful for various purpose.

touch -a: This command is used to change access time only. To change or update the last access or modification times of a file touch -a command is used.

Syntax:

Here touch -a command changes access time of the file named Doc1.

How to use touch to create empty files and modify timestamps

touch -c : This command is used to check whether a file is created or not. If not created then don’t create it. This command avoids creating files.

Syntax:

How to use touch to create empty files and modify timestamps

touch -c-d : This is used to update access and modification time.

Syntax:

How to use touch to create empty files and modify timestamps

touch -m : This is used to change the modification time only. It only updates last modification time.

Syntax:

How to use touch to create empty files and modify timestamps

touch -r : This command is used to use the timestamp of another file. Here Doc2 file is updated with the time stamp of File 1.

Syntax:

How to use touch to create empty files and modify timestamps

touch -t : This is used to create a file using a specified time.

The command touch is very useful in daily use, it is useful to test file transferences by creating empty files for testing, having the capability to create dummy files to do anything we want with them can help at any time for example to test any Linux command which interacts directly with a file, for commands like wipe, srm, or shred you don’t want to try real files.

When we use the command touch with the name of an existing file, touch wont create a new file but will update the file’s timestamps. On the other hand changing the timestamps would be useful if we want to avoid someone to know we accessed a file forging the real date and time it was really accessed.

Linux files have 3 timestamps: atime, mtime and ctime.

atime: atime contains information on when the file content was read with tools or commands to display the file’s content like less, nano, vi, vim, cat, grep, head, etc. The atime timestamp changes and is updated every time the file is viewed.

mtime: mtime shows the last modification of a file’s content, including it’s name, but not it’s ownership or permissions, only the file itself.

ctime: like mtime ctime also shows when a file was modified but it also gets updated when ownership,group or permission access on a file were changed. We can edit the atime and mtime but we can’t edit the ctime, the time get’s updated when a file’s permissions are edited or when the files are really read or modified

Editing the atime timestamp:

Before seeing a file’s timestamps let’s create it by typing:

Where:

touch = creates an empty file called LinuxHintEmptyFIle

ls = list files in the directory

Now to see the file’s timestamps in the terminal type

How to use touch to create empty files and modify timestamps

The command stat shows the access permissions, last access (atime), last modification (mtime) and last change (ctime).

Since the file was recently created date and time are the same, let’s change the file’s permissions to see the stat’s output difference:

How to use touch to create empty files and modify timestamps

Where:

Chmod -777: removes all permissions on the file LinuxHintEmptyFile.

stat: shows the file’s timestamps

In the image above we can see the ctime (Change) was updated.

Now, let’s edit the file atime to 1/2/99, type:

How to use touch to create empty files and modify timestamps

If you also want to change the date access type:

How to use touch to create empty files and modify timestamps

Editing the mtime timestamp:

The syntax to edit mtime is the same, to change the ctime to 1989.02.01 type:

How to use touch to create empty files and modify timestamps

Now you can see the modify date was updated, to update also it’s time similarly with atime type:

How to use touch to create empty files and modify timestamps

Now let’s create a second file and we’ll use the command touch to copy the timestamps of a file on the second:

We have a file accessed,modified and changed the 2019-05-15 00:02:14, to copy the file timestamps from the file LinuxHintEmptyFile type:

As you see now it both files share the same timestamps.

If we want to change all timestamps to current time we only need to type “touch”:

$ stat LHFile2
File: LHFile2
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 802h / 2050d Inode: 13766635 Links: 1
Access: ( 0644 / -rw-r–r– ) Uid: ( 1003 / linuxhint ) Gid: ( 1003 / linuxhint )
Access: 1999 -02-01 21 : 35 : 00.000000000 +0200
Modify: 1989 -02-01 20 : 22 : 00.000000000 +0200
Change: 2019 -05- 15 00:03: 11.592503463 +0300
Birth: –

$ touch LHFile2 strong > em >
$ stat LHFile2
File: LHFile2
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 802h / 2050d Inode: 13766635 Links: 1
Access: ( 0644 / -rw-r–r– ) Uid: ( 1003 / linuxhint ) Gid: ( 1003 / linuxhint )
Access: 2019 -05- 15 00:08: 51.652519679 +0300
Modify: 2019 -05- 15 00:08: 51.652519679 +0300
Change: 2019 -05- 15 00:08: 51.652519679 +0300
Birth: –

How to use touch to create empty files and modify timestamps

As you see the second stat shows the updated atime, mtime and ctime after the command touch.

I hope this tutorial was useful as an introduction to the command touch, for more information on this command type “man touch”, should you have any inquiry contact us opening a ticket support at LinuxHint Support. Keep following LinuxHint for more tips and updates on Linux.

About the author

David Adams

David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.

The touch command is the easiest way to create new, empty files. It is also used to change the timestamps (i.e., dates and times of the most recent access and modification) on existing files and directories.

touch’s syntax is

When used without any options, touch creates new files for any file names that are provided as arguments (i.e., input data) if files with such names do not already exist. Touch can create any number of files simultaneously.

Thus, for example, the following command would create three new, empty files named file1, file2 and file3:

A nice feature of touch is that, in contrast to some commands such as cp (which is used to copy files and directories) and mv (which is used to move or rename files and directories), it does not automatically overwrite (i.e., erase the contents of) existing files with the same name. Rather, it merely changes the last access times for such files to the current time.

Several of touch’s options are specifically designed to allow the user to change the timestamps for files. For example, the -a option changes only the access time, while the -m option changes only the modification time. The use of both of these options together changes both the access and modification times to the current time, for example:

The -r (i.e., reference) option followed directly by a space and then by a file name tells touch to use that file’s time stamps instead of current time. For example, the following would tell it to use the times of file4 for file5:

The -B option modifies the timestamps by going back the specified number of seconds, and the -F option modifies the time by going forward the specified number of seconds. For example, the following command would make file7 30 seconds older than file6.

touch -r file6 -B 30 file7

The -d and -t options allow the user to add a specific last access time. The former is followed by a string (i.e., sequence of characters) in the date, month, year, minute:second format, and the latter uses a [[CC]YY]MMDDhhmm[.ss] format. For example, to change the last access time of file8 to 10:22 a.m. May 1, 2005, 1 May 2005 10:22 would be enclosed in single quotes and used as follows, i.e.

Partial date-time strings can be used. For example, only the date need be provided, as shown for file9 below (in which case the time is automatically set to 0:00):

Just providing the time, as shown below, automatically changes the date to the current date:

The most commonly used way to view the last modification date for files is to use the ls command with its -l option. For example, in the case of a file named file10 this would be

The complete timestamps for any file or directory can be viewed by using the stat command. For example, the following would show the timestamps for a file named file11:

The –help option displays a basic list of options, and the –version option returns the version of the currently installed touch program.

As you may know, the “access time” and “modify time” timestamps of a file will be changed to the current time after the file is edited or modified. Sometimes, you might want to preserve the old timestamps for any reason even after editing or modifying the files. This brief guide explains how to edit a file without changing its timestamps in Linux.

If you don’t know much about Linux file timestamps, refer the following guide.

  • Linux File Timestamps Explained With Examples

Edit A File Without Changing Its Timestamps In Linux

A file’s timestamps can be updated using touch command. The timestamps also gets updated when we manually add contents in a file or remove data from it. If you want change the contents of files without changing its timestamps, there is no direct way to do it. But it is possible!

We can use one of the touch command’s option -r (reference) to preserve file timestamps after editing or modifying it. The -r option is used to set the timestamps of one file to the timestamp values of another.

I have a text file named ostechnix.txt . Let us have look at the timestamps of this file with stat command:

As stated already, if we change the contents or metadata of this file, the timestamps will also change.

Now create a new, empty file, for example ostechnix.timestamp , with the same timestamps as ostechnix.txt file using touch command:

Check the timestamps of the new file:

How to use touch to create empty files and modify timestamps

Edit a file without changing its timestamp in Linux

See? Both files’ atime and mtime timestamps are same!

Now make the changes in the main file i.e. ostechnix.txt . As you guessed, the main file’s timestamps will change.

Finally, copy the timestamps of new file i.e. ostechnix.timestamp to the main file:

Now the main file has its old timestamps before it was edited/modified.

Please note that we can’t preserve the change time (ctime) timestamps. It will always update to the current time.

TL;DR

Create a new file with same timestamps as main file with touch command, do the changes in the main file and set the timestamps of the new file to main file with -r option.

Tutorial on using touch, a UNIX and Linux command for changing file timestamps. Examples of creating an empty file, updating access and modification time, updating just access time, updating just modification time and setting timestamps in the past.

Estimated reading time: 4 minutes

Table of contents

What is the touch command in UNIX?

The touch command is a command line utility to update timestamps on files. UNIX and UNIX like operating systems store timestamp information for each file or folder including access time, modify time and change time. It is possible to modify timestamps using the touch command either to update a timestamp to the current time or to modify it to a date in the past.

How to create an empty file

To create an empty file using the touch command pass the name of the file to be created to the touch command. This sets the access and modification time for the file to the current time when running the command.

How to create multiple empty files

To create multiple empty files pass more than one filename to be created to the touch command. This sets the access and modification time for each file created to the current time when running the command.

How to avoid creating a file if it does not exist

By default the touch command will create a file if it does not exist. To prevent this behaviour pass the -c option.

How to update access and modification time

To update the access and modification time of a file or files pass the name of the file or files to the touch command. This will update the access and modification dates to the time the command was run.

How to change only the access time

To change the access time of a file pass the -a option.

In this example the modified date remains the same.

How to change only the modification time

To change the access time of a file pass the -m option.

In this example the access time remains the same.

How to apply timestamp attributes from one file to another

To apply one files timestamps to another one pass the -r option followed by the source and target files. This takes the source file as a reference and applies the timestamps to the target file.

How to set a time that is not the current time

To set a time that is not the current time pass the -t option followed by a time in the format [[CC]YY]MMDDhhmm[.ss] . In the following example the file access and modification dates are set to 25th January 1977.

You can literally bend time with the touch command.

How to use touch to create empty files and modify timestamps

Further reading

  • touch man page
  • The touch command
  • Touch Wikipedia page

Have an update or suggestion for this article? You can edit it here and send me a pull request.

  • UNIX
  • Linux

Recent Posts

About the author

How to use touch to create empty files and modify timestamps

George Ornbo is a Software Engineer based in Buckinghamshire, England.

Synopsis

Description

Update the access and modification times of each FILE to the current time.

A FILE argument that does not exist is created empty, unless -c or -h is supplied.

A FILE argument string of – is handled specially and causes touch to change the times of the file associated with standard output.

Mandatory arguments to long options are mandatory for short options too. -a change only the access time -c, –no-create do not create any files -d, –date=STRING parse STRING and use it instead of current time -f (ignored) -h, –no-dereference affect each symbolic link instead of any referenced file (useful only on systems that can change the timestamps of a symlink) -m change only the modification time -r, –reference=FILE use this file’s times instead of current time -t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of current time –time=WORD change the specified time: WORD is access, atime, or use: equivalent to -a WORD is modify or mtime: equivalent to -m –help display this help and exit –version output version information and exit

Note that the -d and -t options accept different time-date formats.

Date String

Author

Reporting Bugs

Copyright

See Also

should give you access to the complete manual.

Change file timestamps

This is a POSIX utility.

Use the optional argument /t to specify a date other than the current time.
( four-digit years, two-digit months, days, hours, minutes, seconds)

An alternative Touch using the COPY command.

As first documented in the ancient (MSDOS) KB article Q69581 the COPY /b command can be used to touch the date/time of a file:

COPY /B Source.txt+,,Source.txt

This takes a source file, adds nothing (represented by the commas) and copies it to the same file as a destination. If you are working in a single folder, the second filename can be omitted as it will default to writing the same filename.

or as a batch file:

@Echo Off
COPY /B %1+,, %1

There are a few caveats with this technique:
It fails for read-only or hidden files.
It may reset file permissions back to the default for the folder.
In some versions of Windows, if the file is 0 bytes long it will be deleted!
If the file is a binary file with trailing null characters (0x00), they will be deleted, corrupting the file.
It is no longer a documented option in current versions of Windows.

A better alternative is PowerShell, you can call a one-liner with powershell.exe

#And smiles you’ll give and tears you’ll cry
And all you touch and all you see
Is all your life will ever be#

Pink Floyd (Breathe)

Related commands:

Q299648 – Date and Time Stamps for Files and Folders (FAT vs NTFS)
COPY – Copy one or more files to another location.
Equivalent PowerShell Script: touch – Change file timestamps.
Equivalent bash command (Linux): touch – Change file timestamps.