Javatpoint Logo
Javatpoint Logo

Touch Command in Linux/Unix with Examples

touch command is a way to create empty files (there are some other mehtods also). You can update the modification and access time of each file with the help of touch command.

It is available in ReactOS, Unix, TSC's FLEX, Unix-like operating systems, the Microware OS-9 shell, the AROS shell, and Digital Research/Novell DR DOS. Also, the command is available for Microsoft Windows and FreeDOS.

  • It is the same as opening or creating a file and storing it without any notification to the file contents in its default usage.
  • The touch command ignores closing, saving, and opening the file. Rather it updates the dates related to the directory or file.
  • An updated modification or access date can be important for a range of other programs like backup utilities or make command-line interface programming utilities.
  • These programs are typically only focused on files which have been modified or created after the program was last executed.
  • Also, the touch command can be helpful to quickly create files for scripts or programs that need a file with a particular name to be available for successful program operation but don't need the file to include any specific content.

SUS (Single Unix Specification) defines that the touch command should modify the modification times, access times, or both for the files. The file is recognized by a pathname imported as one argument. Also, it represents that if the file recognized doesn't exist, the file is made, and the modification and access times are fixed as specified. The touch command applies the current time if new timestamps are not specified.

Brief History of touch

First, a touch utility occurred in Version 7 AT&T UNIX. The command is today available for several different OSes, including various Unix, DOS, Unix-like systems, the classic Mac OS, and Microsoft Windows. The release of touch grouped in GNU coreutils was specified by Paul Rubin, Randy Smith, David MacKenzie, Jim Kingdon, and Arnold Robbins.

For Microsoft Windows, the command is an isolated package as a part of the native Win32 ports UnxUtils collection of GNU Unix-like utilities. The FreeDOS edition is licensed upon the GPL and was integrated by Kris Heidenstrom. KolibriOS and DR DOS 6.0 add the touch command's implementation. Also, the touch command has been shipped to the IBM i OS.

Working of touch in Ubuntu

touch is a common Linux command to make an empty file or modify the file timestamps (file access time, last date, or modification).

Timestamps

Linux files contain three timestamps: ctime, mtime, and atime. These timestamps are explained below:

  • atime: It includes details on when the content of the file is read with commands or tools to show the content of the file like head, grep, cat, vim, vi, nano, less, etc. The atime timestamp modifies and is updated each time it is checked.
  • mtime: It displays the last modification of the content of a file, such as its name, but only the file itself, not its permission or owner.
  • ctime: Also, ctime represents when any file is changed, but it is also updated if the permission, group, or ownership access on any file is modified. We can edit the mtime and atime, but we can not modify the ctime. The time will be updated if the permissions of a file are modified or if the files are modified or read.

Syntax:

Example:


TOUCH Command

Look above, we have created two files namely 'myfile1' and 'myfile2' through touch command. To create multiple files just type all the file names with a single touch command followed by enter key. For example, if you would like to create 'myfile1' and 'myfile2' simultaneously, then your command will be:

touch Options

OptionFunction
touch -aTo change file access and modification time.
touch -mIt is used to only modify time of a file.
touch -rTo update time of one file with reference to the other file.
touch -tTo create a file by specifying the time.
touch -cIt does't create n empty file.
-d=, --date=This option is used to modify the timestamp with a date string.
-h, --no-dereferenceIt modifies the timestamp of a symbolic link.
-f No effect.The option forces modifications in older BSD.
--version, -vIt shows the program version.
--helpIt shows the help menu.

Linux touch -a command

touch command with option 'a' is used to change the access time of a file. By default, it will take the current time of your system.

Syntax:

Example:

To see the access and change time of your file, you need to use stat command.

TOUCH -a Command

In above snapshot we have used 'stat' command (which we'll learn in later tutorial)just to check the status of our directory (usr). So don't get confused with that. Now you can match the access time of directory (usr) before and after passing the command 'touch -a usr. It has taken the default access time of our system.

Linux touch -m command

The touch '-m' option will help you to change only the modification time of a file.

Syntax:

Example:

TOUCH Command

Notice carefully in the above snapshot, only modification time has been changed.

Linux touch -r option

This command will update time with reference to the other mentioned command. There are two ways to use this command. Both works the same.

In below example, we want to change time-stamp of 'Demo.txt' with reference to 'demo.txt'. Firstyou can write it as,

or

First we'll see the status of both the files before using touch comand.

TOUCH Command

Now after using touch -r demo.txt Demo.txt command, time of Demo.txt has been changed wit reference to time of demo.txt

TOUCH Command

Linux touch -t command

with this command, you can change the access time of a file by determining a specified time to it.

It will modify the time by specified time instead of default time.

Format of time will be:

Below screenshot shows status of file 2.png before the touch command,

TOUCH -t Command

This screenshot shows that time of file 2.png has been modified by our specified time.

TOUCH Command

Linux touch -c command

Using 'c' option with touch command will not create an empty file, if that file doesn't exist.

Syntax:

Example:


TOUCH Command

In above figure, we wanted to create file 'movie' but with 'c' option no file has been created.

Set particular timestamp

We can use the touch command to fix a particular timestamp for a file. For instance:

The timestamp format pursues a particular pattern:

Where,

  • CC: These are the initial two digits for the year.
  • YY: These are the last two digits for the year.
  • MM: It shows the month.
  • DD: It shows the day.
  • hh: It shows the hour.
  • mm: It shows the minutes.
  • ss: It shows the seconds.

Setting file timestamp with the data string

We can use the touch command with the -d flag for setting a timestamp with a date string. Its syntax is as follows:

The date string is an adjustable time format and allows several human-readable text formats. A few examples are as follows:

  • Calendar dates, e.g., 19 August 2020.
  • Time of day, e.g., 9:28pm or 7:03am.
  • Days of the week, e.g., Tuesday, Wednesday, etc.
  • Relative time, e.g., 8 years ago, Next Wednesday, yesterday, etc.

Next TopicLinux rm





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA