Chapter 2. Linux Key Commands

Touch (Create File)

Touch (Create File)
Tag:

Using the touch command, you can create a new file. You can also use the touch command to change the time stamp of the existing file.

Creating a new file

Simply run the command with a file path with a new file name at the end of the path. For example, to create a new file named new_file_a under your working directory, run the command below.

Command Line - INPUT
touch new_file_a

Change the timestamp of an existing file or directory

We'll explain how to change the timestamp of an existing file or directory using the two files like shown below.

Command Line - INPUT
ls -l
Command Line - RESPONSE
-rw-rw-r-- 1 ubuntu ubuntu    0 Oct 11  2018  reference.txt
-rw-rw-r-- 1 ubuntu ubuntu    0 Dec 29 04:57  sample.txt

-d option : specify date and time

To change the timestamp of the sample.txt to "2021-11-12 06:00", run the command below.

Command Line - INPUT
touch -d "2021-11-12 06:00" sample.txt

You can see the timestamp changed below.

Command Line - INPUT
ls -l
Command Line - RESPONSE
-rw-rw-r-- 1 ubuntu ubuntu    0 Oct 11  2018  reference.txt
-rw-rw-r-- 1 ubuntu ubuntu    0 Nov 12  2021  sample.txt

-r (--reference) option : apply another file's timestamp

To change the timestamp of sample.txt to the same timestamp as reference.txt has, run the command below.

Command Line - INPUT
touch -r reference.txt sample.txt

You can see that the timestamp changed as shown below.

Command Line - INPUT
ls -l
Command Line - RESPONSE
-rw-rw-r-- 1 ubuntu ubuntu    0 Oct 11  2018  reference.txt
-rw-rw-r-- 1 ubuntu ubuntu    0 Oct 11  2018  sample.txt

Using the touch command, you can create a new file. You can also use the touch command to change the time stamp of the existing file.

Creating a new file

Simply run the command with a file path with a new file name at the end of the path. For example, to create a new file named new_file_a under your working directory, run the command below.

Command Line - INPUT
touch new_file_a

Change the timestamp of an existing file or directory

We'll explain how to change the timestamp of an existing file or directory using the two files like shown below.

Command Line - INPUT
ls -l
Command Line - RESPONSE
-rw-rw-r-- 1 ubuntu ubuntu    0 Oct 11  2018  reference.txt
-rw-rw-r-- 1 ubuntu ubuntu    0 Dec 29 04:57  sample.txt

-d option : specify date and time

To change the timestamp of the sample.txt to "2021-11-12 06:00", run the command below.

Command Line - INPUT
touch -d "2021-11-12 06:00" sample.txt

You can see the timestamp changed below.

Command Line - INPUT
ls -l
Command Line - RESPONSE
-rw-rw-r-- 1 ubuntu ubuntu    0 Oct 11  2018  reference.txt
-rw-rw-r-- 1 ubuntu ubuntu    0 Nov 12  2021  sample.txt

-r (--reference) option : apply another file's timestamp

To change the timestamp of sample.txt to the same timestamp as reference.txt has, run the command below.

Command Line - INPUT
touch -r reference.txt sample.txt

You can see that the timestamp changed as shown below.

Command Line - INPUT
ls -l
Command Line - RESPONSE
-rw-rw-r-- 1 ubuntu ubuntu    0 Oct 11  2018  reference.txt
-rw-rw-r-- 1 ubuntu ubuntu    0 Oct 11  2018  sample.txt
Tag: