Skip to content

Linux touch command (changes metadata)

Change file metadata on Linux with the touch command

Every file contains metadata information, such as its name, creation date, modification date, and last access date, among other information.

The touch command is used to change the time metadata of the files, modifying the date of last access or modification of the files.

This can be useful to mark a file to undergo incremental or differential backup starting from a certain date, or to be synchronized in a repository, without the need to change the content of the file, simply by changing its modification date.

The touch command is also useful for creating an empty file if the entered file does not exist.

Touch control options

The most common options are:

  • -a Change the file’s last-access information
  • -c Don’t create the file if it doesn’t exist
  • -d data Allows a date in text format to be entered instead of the current date and time - -m Change the modification time of the file
  • -t stamp Use the format [[CC] YY] mmddHmm [.ss] instead of the current date and time
  • —time=Word Change the time information entered, which may be time (access) or modification (mtime)

The -d=data option is mostly free human-readable date text, such as “Sun, February 29, 2004 16:21:42 -0800 “or” 2004-02-29 16:21:42 “or until “next Thursday”. A date sequence may contain items indicating calendar date, time of day, time zone, day of the week, relative time, relative date, and numbers.

Create empty file on Linux

In the example below touch is used to create an empty file called config.txt:

$ touch config.txt $ ls -l -rw-rw-r-- 1 ec2-user ec2-user 0 set 13 22:29 config.txt

Create multiple files at once on Linux

A list can also be used to create multiple files:

$ touch test {1.. 3} .txt <br></br>$ ls -l <br></br>-rw-rw-r-- 1 ec2-user ec2-user 26 set 12 16:14 test1.c <br></br>-rw-rw-r-- 1 ec2-user ec2-user 0 set 13 22:31 teste1.txt <br></br>-rw-rw-r-- 1 ec2-user ec2-user 0 set 13 22:31 teste2.txt <br></br>-rw-rw-r-- 1 ec2-user ec2-user Sep 13 22:31 teste3.txt

Change the modification date of a file on Linux

Touch can be used to change the modification date of a file, such as test1.c, which changed on 12/set:

$ touch -d “09/12" test1.c
$ ls -l <br></br>-rw-rw-r-- 1 ec2-user ec2-user ec2-user 26 Sep 12 22:35 teste1.c <br></br>-rw-rw-r-- 1 ec2-user ec2-user <br></br>ec2-user 0 set 13 22:31 ec2-user 0 set 13 22:31 teste2.txt <br></br>-rw-rw-r-- 1 ec2-user ec2-user 0 set 13 22:31 teste1.txt teste3.txt

You can also specify a specific date and time instead of the current date and time. In this example: 11/05/1978 at 18:40:30:

$ touch -t 197805111840.30 test1.c $ ls -l -rw-rw-r-- 1 ec2-user ec2-user ec2-user 226 May 11 1978 test1.c

The format of the specific time is defined by the standard CCYYMMDDHHMM.ss, in which:

  • CC — The first two digits of the year.
  • YY — The subsequent two digits of the year.
  • MM — The month of the year [01-12].
  • DD — The day of the month [01-31].
  • hh — The time of day [00-23].
  • mm — The minute of the hour [00-59].
  • ss — The second of the minute [00-59]

You can also specify the date and time as a set (string) using the -d option. The following command line shows an example that sets the date to February 23 and the time is automatically set to 00:00:

$ touch -d '23 Feb' test1.c $ ls -l test* -rw-rw-r-- 1 ec2-user ec2-user ec2-user 26 Feb 23 2019 test1.c

Watch this short video of how the touch control works:

Learn much more about Linux in our online course. You can register here. If you already have an account, or want to create one, just log in or create your user here.

Did you like it?

Share