26th July 2024

Linux File & Folder Creation and TimeStamp Change – Touch Command

The touch command changes the timestamp of the existing file or folder, creates a new blank file if the file or folder does not exist. When a file is produced, modified, sent, received and/or saved, it is stored as a “timestamp“. While the file can be created with the touch command, the timestamp such as the access time of the existing files can be updated. The file or folder name is written after the command.

We will try to give you some practical examples of the touch command. The touch command is standard in Linux/Unix operating systems.

How To Create an  Empty File?

If there is no file with the same name in the directory where the Touch command is run, an empty file (zero-byte) is created.

omer@ubuntu:~$ touch cyber
Create a  Blank File
Create a  Blank File

 

Multiple File Creation

You can create multiple files with the Touch command. For example, with the command below, we will create three empty files named file1, file2, file3.

omer@ubuntu:~$ touch file1 file2 file3
Multiple File Creation
Multiple File Creation

 

Changing File Access and Modification Time – touch -a

The touch command changes the access and modification timestamp of the file associated with the -a parameter. In the example below, the access time and modification time of file2 are changed.

omer@ubuntu:~$ touch -a file2
Changing File Access and Modification Time
Changing File Access and Modification Time

 

Changing the Timestamp of the File – touch -m

In order to change the modification time of a file, the touch command and the -m parameter are used. With this parameter, only the modify time changes, the access time does not.

omer@ubuntu:~$ touch -m file2
Changing the Modification Time of the File
Changing the Modification Time of the File

 

LEARN MORE  What Is Linux And Linux Fundamentals with Examples

FILE’s Modification And Updating of Access Time with a Specific Time – touch -c -t MMDDHHMM

You can update the modify time and access time values ​​of a file at a specific time, for this, we will use the -c and -t parameters. You can use the format below for this.

touch -c -t MMDDHHMM cyber
FILE's Modification And Updating of Access Time with a Specific Time
FILE’s Modification And Updating of Access Time with a Specific Time

 

For example, with the command below, we will modify the file and set the access time to 20:15 October 20 and the current year (2020).

omer@ubuntu:~$ touch -c -t 10202015 file2
touch -c -t 10202015 file2
touch -c -t 10202015 file2

 

Using a Different File’s Time Stamp – touch -r

With the touch command used with the -r parameter below, we will update the time-stamp of file1 with the time-stamp of file2.

omer@ubuntu:~$ touch -r file2 file1
Using a Different File's Time Stamp
Using a Different File’s Time Stamp

 

Creating a File Using a Specific Time – touch -t YYYYMMDDHHMM.SS

You can create a file with the touch command using a different time than the present. You can use the format below for this.

touch -t YYYYMMDDHHMM.SS cyber
Creating a File Using a Specific Time
Creating a File Using a Specific Time

 

For example, we will create the cyber file using the touch command -t parameter and the time-stamp of July 4, 1984, 11:30:11.

omer@ubuntu:~$ touch -t 198407041130.11 cyber
time-stamp (touch -t)
time-stamp (touch -t)

 

Creating a File with a Specified Time Stamp – touch -d

You can create a file with a specific timestamp with the -d parameter.

omer@ubuntu:~$ touch -d "4 july 1984" omerbirth.txt
Creating a File with a Specified Time Stamp - touch -d
Creating a File with a Specified Time Stamp – touch -d

 

More detailed use of the Touch command

For more detailed use of the touch command, you can examine it using the “man touch” or “touch –h” command.

omer@ubuntu:~$ man touch 

omer@ubuntu:~$ touch --h
man touch
man touch
touch --h
touch –h

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *