Linux touch command

The touch command is one of the ways to create empty files. One can easily update the modification and access time of the files by using touch command.

Syntax:

touch <filename>

Example: touch myfile1

codes@w3schools:~$ ls
 
cretecler Disk1 Downloads Music Pictures Templates
 
Desktop Documents example.desktop office Public Videos
 
codes@w3schools:~$ touch myfile1
 
codes@w3schools:~$ touch myfile2
 
codes@w3schools:~$ ls
 
cretecler Disk1 Downloads Music myfile2 Pictures Templates
 
Desktop Documents example.desktop myfile1 office Public Videos
 
codes@w3schools:~$

In the above example, we are creating two files named ‘myfile1’ and ‘myfile2’ using touch command. In order to create multiple files, you just need to type all the file names with a single touch command and press enter key. Let’s say you want to create ‘myfile1’ and ‘myfile2’ simultaneously, then you will use the following command:

touch myfile1 myfile2

touch Options

Option Function
touch -a This command is used to change file access and modification time.
touch -m To modify the time only of a file.
touch -r It is used to update the time of one file with reference to the other file.
touch -t This command is used to create a file by specifying the time.
touch -c This command doesn’t create an empty file.

Linux touch -a command

touch -a command is used to change the access time of a file. By default, it will use the current time of the system.

Syntax:

touch -a <filename>

Example: touch -a usr

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

codes@w3schools:~/Desktop$ stat usr
 
File: 'usr'
 
Size: 4096 Block: 8 IO Block: 4096 Directory
 
Device: 807h/2055d Inode: 655963 Links: 3
 
Access: (0755/drwxr-xr-x) Uid: (1000/ codes) Gid: (1000/ codes)
 
Access: 2016-05-25 11:03:31.579478467 +0530
 
Modify: 2016-05-11 17:55:13.834427646 +0530
 
Change: 2016-05-24 10:39:45.873020113 +0530
 
Birth: -
 
codes@w3schools:~/Desktop$
 
codes@w3schools:~/Desktop$ touch -a usr
 
codes@w3schools:~/Desktop$
 
codes@w3schools:~/Desktop$ stat usr
 
File: 'usr'
 
Size: 4096 Block: 8 IO Block: 4096 Directory
 
Device: 807h/2055d Inode: 655963 Links: 3
 
Access: (0755/drwxr-xr-x) Uid: (1000/ codes) Gid: (1000/ codes)
 
Access: 2016-05-25 16:50:14.663712615 +0530
 
Modify: 2016-05-11 17:55:13.834427646 +0530
 
Change: 2016-05-25 16:50:14.663712615 +0530
 
Birth: -
 
codes@w3schools:~/Desktop$

As you can see in the above example, we have used ‘stat’ command to check the status of our directory (usr). Now after we pass ‘touch -a usr’ command, you can notice the difference between the access time of directory (usr) from before. It has now set the default time of our system as access time.

Linux touch -m command

If you just want to change the modification time of a file only, then use the touch ‘-m’ option.

Syntax:

touch -m <filename>

Example: touch -m usr

codes@w3schools:~/Desktop$ stat linuxfun.pdf
 
File: 'linuxfun.pdf'
 
Size: 70226363 Blocks: 13720 IO Block: 4096 regular file
 
Device: 807h/2055d Inode: 658072 Links: 1
 
Access: (0664/-rw-rw-r--) Uid: (1000/ codes) Gid: (1000/ codes)
 
Access: 2016-05-25 11:07:36.806939775 +0530
 
Modify: 2016-05-11 18:40:09.000000000 +0530
 
Change: 2016-05-25 11:07:35.790939820 +0530
 
Birth: -
 
codes@w3schools:~/Desktop$
 
codes@w3schools:~/Desktop$ touch -m linuxfun.pdf
 
codes@w3schools:~/Desktop$
 
codes@w3schools:~/Desktop$ stat linuxfun.pdf
 
File: 'linuxfun.pdf'
 
Size: 70226363 Blocks: 13720 IO Block: 4096 regular file
 
Device: 807h/2055d Inode: 658072 Links: 1
 
Access: (0664/-rw-rw-r--) Uid: (1000/ codes) Gid: (1000/ codes)
 
Access: 2016-05-25 11:24:49.268540162 +0530
 
Modify: 2016-05-25 11:24:48.256540203 +0530
 
Change: 2016-05-25 11:24:48.256540203 +0530
 
Birth: -
 
codes@w3schools:~/Desktop$

Check in the above example carefully as only the modification time has changed.

Linux touch -r option

It is used update time with reference to the other specified command. It can be used in two ways but both work just the same.

In the below given example, we want to change the time-stamp of ‘Demo.txt’ with reference to the other file ‘demo.txt’. You can pass the commond in the following manner:

touch -r demo.txt Demo.txt

or

touch Demo.txt -r demo.txt

First we are going to see the status of both the files before using touch command.

codes@w3schools:~/Downloads$ stat demo.txt
 
File: 'demo.txt'
 
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
 
Device: 807h/2055d Inode: 658414 Links: 1
 
Access: (0664/-rw-rw-r--) Uid: (1000/ codes) Gid: (1000/ codes)
 
Access: 2016-05-25 10:45:00.276624267 +0530
 
Modify: 2016-05-24 10:37:35.265025647 +0530
 
Change: 2016-05-25 10:44:59.264624225 +0530
 
Birth: -
 
codes@w3schools:~/Downloads$
 
codes@w3schools:~/Downloads$ stat Demo.txt
 
File: 'Demo.txt'
 
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
 
Device: 807h/2055d Inode: 658414 Links: 1
 
Access: (0664/-rw-rw-r--) Uid: (1000/ codes) Gid: (1000/ codes)
 
Access: 2016-05-25 10:43:52.356621370 +0530
 
Modify: 2016-05-24 10:37:01.761027070 +0530
 
Change: 2016-05-24 10:37:22.945026171 +0530
 
Birth: -
 
codes@w3schools:~/Downloads$

As you can see, after using touch -r demo.txt Demo.txt command, time of Demo.txt has been changed with reference to time of file demo.txt

codes@w3schools:~/Downloads$ touch -r demo.txt Demo.txt
 
codes@w3schools:~/Downloads$
 
codes@w3schools:~/Downloads$ stat Demo.txt
 
File: 'Demo.txt'
 
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
 
Device: 807h/2055d Inode: 658511 Links: 1
 
Access: (0664/-rw-rw-r--) Uid: (1000/ codes) Gid: (1000/ codes)
 
Access: 2016-05-25 10:45:00.276624267 +0530
 
Modify: 2016-05-24 10:37:35.265025647 +0530
 
Change: 2016-05-25 11:51:51.924471400 +0530
 
Birth: -
 
codes@w3schools:~/Downloads$
 
codes@w3schools:~/Downloads$ stat demo.txt
 
File: 'demo.txt'
 
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
 
Device: 807h/2055d Inode: 658414 Links: 1
 
Access: (0664/-rw-rw-r--) Uid: (1000/ codes) Gid: (1000/ codes)
 
Access: (0664/-rw-rw-r--) Uid: (1000/ codes) Gid: (1000/ codes)
 
Access: 2016-05-25 10:45:00.276624267 +0530
 
Modify: 2016-05-24 10:37:35.265025647 +0530
 
Change: 2016-05-25 11:44:59.264624225 +0530
 
Birth: -
 
codes@w3schools:~/Downloads$

Linux touch -t command

You can change the access time of a file with this command by determining a specified time to it. It will help you modify the time by specified time instead of the default time of your system.

Format of time will be as follows:

touch -t YYYYMMDDhhmm.ss

In the below example, it displays the status of file 2.png before using the touch command,

codes@w3schools:~/Pictures$ stat 2.png
 
File: '2.png'
 
Size: 108673 Blocks: 216 IO Block: 4096 regular file
 
Device: 807h/2055d Inode: 660019 Links: 1
 
Access: (0664/-rw-rw-r--) Uid: (1000/ codes) Gid: (1000/ codes)
 
Access: 2016-05-24 16:00:31.389688464 +0530
 
Modify: 2016-05-18 11:18:07.936242778 +0530
 
Change: 2016-05-18 11:18:07.936242778 +0530
 
Birth: -
 
codes@w3schools:~/Pictures$
 
Now, the below example displays the time of file 2.png that has been modified by our specified time after using this command.
 
codes@w3schools:~/Pictures$ touch -t 201607052052.37 2.png
 
codes@w3schools:~/Pictures$
 
codes@w3schools:~/Pictures$ stat 2.png
 
File: '2.png'
 
Size: 108673 Blocks: 216 IO Blocks: 4096 regular file
 
Device: 807h/2055d Inode: 660019 Links: 1
 
Access: (0664/-rw-rw-r--) Uid: (1000/ codes) Gid: (1000/ codes)
 
Access: 2016-05-24 12:19:52.836400179 +0530
 
Modify: 2016-07-05 20:52:37.000000000 +0530
 
Change: 2016-05-25 12:19:51.632400232 +0530
 
Birth: -
 
codes@w3schools:~/Pictures$

Linux touch -c command

This option ‘c’ with touch command will not create an empty file if that file doesn’t exist before.

Syntax:

touch -c <filename>

Example: touch -c movie

codes@w3schools:~$ ls
 
cretecler Disk1 Downloads Music myfile2 Pictures Templates
 
Desktop Documents examples.desktop myfile1 office Public Videos
 
codes@w3schools:~$
 
codes@w3schools:~$ touch -c movie
 
codes@w3schools:~$
 
codes@w3schools:~$ ls
 
cretecler Disk1 Downloads Music myfile2 Pictures Templates
 
Desktop Documents examples.desktop myfile1 office Public Videos
 
codes@w3schools:~$
 
In the above example, we were trying to create file 'movie' but as we used 'c' option, no file has been created.
 
&nbsp;
Content Protection by DMCA.com