Ubuntu

How to use the touch command on Ubuntu

How to use the touch command on Ubuntu
touch” is a basic Linux command to create empty files or change files timestamps (last date or time of file access or modification).

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.

Timestamps

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:

$ touch LinuxHintEmptyFile
$ ls
LinuxHintEmptyFile

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

$ stat LinuxHintEmptyFile
File: LinuxHintEmptyFile
Size: 0   Blocks: 0         IO Block: 4096   regular empty file
Device: 802h/2050d    Inode: 13766630  Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1003/linuxhint)   Gid: ( 1003/linuxhint)
Access: 2019-05-14 22:57:09.320314528 +0300
Modify: 2019-05-14 22:57:09.320314528 +0300
Change: 2019-05-14 22:57:09.320314528 +0300
Birth: -

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:

$ chmod -777 LinuxHintEmptyFile
$ stat LinuxHintEmptyFile
File: LinuxHintEmptyFile
Size: 0       Blocks: 0         IO Block: 4096   regular empty file
Device: 802h/2050d    Inode: 13766630  Links: 1
Access: (0000/----------)  Uid: ( 1003/linuxhint)   Gid: ( 1003/linuxhint)
Access: 2019-05-14 22:57:09.320314528 +0300
Modify: 2019-05-14 22:57:09.320314528 +0300
Change: 2019-05-14 23:23:55.968391139 +0300
Birth: -

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:

$ touch -a --date="1999-02-01" LinuxHintEmptyFile
$ stat LinuxHintEmptyFile
File: LinuxHintEmptyFile
Size: 0     Blocks: 0  IO Block: 4096   regular empty file
Device: 802h/2050d    Inode: 13766630  Links: 1
Access: (0000/----------)  Uid: ( 1003/linuxhint)   Gid: ( 1003/linuxhint)
Access: 1999-02-01 00:00:00.000000000 +0200
Modify: 2019-05-14 22:57:09.320314528 +0300
Change: 2019-05-14 23:34:32.964421513 +0300
Birth: -

If you also want to change the date access type:

$ touch -a --date="1999-02-01 21:35" LinuxHintEmptyFile
$ stat LinuxHintEmptyFile
File: LinuxHintEmptyFile
Size: 0     Blocks: 0         IO Block: 4096   regular empty file
Device: 802h/2050d    Inode: 13766630  Links: 1
Access: (0000/----------)  Uid: ( 1003/linuxhint)   Gid: ( 1003/linuxhint)
Access: 1999-02-01 21:35:00.000000000 +0200
Modify: 2019-05-14 22:57:09.320314528 +0300
Change: 2019-05-14 23:42:49.016445166 +0300
Birth: -

Editing the mtime timestamp:

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

$ touch -m --date="1989-02-01" LinuxHintEmptyFile
$ stat LinuxHintEmptyFile
File: LinuxHintEmptyFile
Size: 0          Blocks: 0         IO Block: 4096   regular empty file
Device: 802h/2050d    Inode: 13766630  Links: 1
Access: (0000/----------)  Uid: ( 1003/linuxhint)   Gid: ( 1003/linuxhint)
Access: 1999-02-01 21:35:00.000000000 +0200
Modify: 1989-02-01 00:00:00.000000000 +0200
Change: 2019-05-14 23:49:56.560465553 +0300
Birth: -

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

$ touch -m --date="1989-02-01 20:22" LinuxHintEmptyFile
$ stat LinuxHintEmptyFile
File: LinuxHintEmptyFile
Size: 0     Blocks: 0         IO Block: 4096   regular empty file
Device: 802h/2050d    Inode: 13766630  Links: 1
Access: (0000/----------)  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-14 23:52:10.156471924 +0300
Birth: -

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

$ touch LHFile2
$ 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:02:14.020500718 +0300
Modify: 2019-05-15 00:02:14.020500718 +0300
Change: 2019-05-15 00:02:14.020500718 +0300

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:

$ touch -r LinuxHintEmptyFile LHFile2
$ 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: -

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
$ 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: -

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.

How to download and Play Sid Meier's Civilization VI on Linux
Introduction to the game Civilization 6 is a modern take on the classic concept introduced in the series of the Age of Empires games. The idea was fai...
How to Install and Play Doom on Linux
Introduction to Doom The Doom Series originated in the 90s after the release of the original Doom. It was an instant hit and from that time onwards th...
Vulkan for Linux Users
With each new generation of graphics cards, we see game developers push the limits of graphical fidelity and come one step closer to photorealism. But...

Uusimmat artikkelit käyttöjärjestelmistä. Paljon mielenkiintoisia oppaita ja hyödyllisiä vinkkejä. Tunne omasi modernin tekniikan maailmassa