Touch command in Linux

Posted at October 28, 2016 at 4:21 pm by Jithin

The touch command is used to create an empty file and also to change the modified time of a file.

The syntax of the touch command is

 touch [OPTION]… FILE…

1) Create a blank file.

Touch command allows to create a blank file. In case if the file already exist it will change the access time of the file.

$ touch test.txt

2) Create multiple files with touch.

By giving a line of separation among the name of the files will allow you to create multiple files at a time.

$ touch test.txt test1.txt test2.txt

3) Create a variety of files with touch.

Touch command allows to create as much of files needed with a wide variety of patterns such as

$ touch {A..Z}

$ touch {1..100}

$ touch {1..1000}.mp3

By listing the command after the execution of the touch command will it help you to see the files created.

4) Avoid creating new files

This option will allow to modify the access time of an existing file in the system, without creating the file again. For this you need to use the option “ -c”.

$ touch  -c  image.jpg

5) Change file access time – ‘a’

This option will only modify the access time of the file.

$ touch test.txt

$ stat test.txt

File: ‘test.txt’

Size: 0                                    Blocks: 0                       IO Block: 1024   regular empty file

Device: fd03h/64771d            Inode: 99825               Links: 1

Access: (0644/-rw-r–r–)         Uid: (    0/    root)         Gid: (    0/    root)

Access: 2016-10-13 09:15:54.000000000 -0400

Modify: 2016-10-13 09:15:54.000000000 -0400

Change: 2016-10-13 09:15:54.000000000 -0400

Birth: –

6) Change the modified time ‘-m’

With the help of -m option we can alter the modified time of the file.

$ touch -m test.txt

After the execution of the touch command, check the statistics of the file with the help of stat command

$ stat test.txt

File: ‘test.txt’

Size: 0                                     Blocks: 0                       IO Block: 1024   regular empty file

Device: fd03h/64771d            Inode: 99825               Links: 1

Access: (0644/-rw-r–r–)         Uid: (    0/    root)         Gid: (    0/    root)

Access: 2016-10-13 09:15:54.000000000 -0400

Modify: 2016-10-13 09:20:16.000000000 -0400

Change: 2016-10-13 09:20:16.000000000 -0400

Birth: –

We also have the provision to alter the modification time of the multiple files with the help of wild cards.

$ touch -m *.mp3

7) Change access and modification time together.

Both the access time and modification time can be altered simultaneously.

$ touch -am test.txt

$ stat touch.txt

File: ‘test.txt’

Size: 0                                     Blocks: 0          IO Block: 1024   regular empty file

Device: fd03h/64771d            Inode: 99825   Links: 1

Access: (0644/-rw-r–r–)         Uid: (    0/    root)         Gid: (    0/    root)

Access: 2016-10-13 09:27:24.000000000 -0400

Modify: 2016-10-13 09:27:24.000000000 -0400

Change: 2016-10-13 09:27:24.000000000 -0400

Birth: –

8) Set a specific access/modify time instead of current time.

This option will give the privilege to the user for set the access/modify time in the format of [[CC]YY]MMDDhhmm[.ss] with the help of -t option.

$ touch -c -t 1610142004.16  test.txt

or

touch -c -t 20161610142004.16  test.txt

Usage of -c option will skip the process of file creation. If the file mentioned does not exist then it will create a new file with the specified access/modify time.

9) Set as a reference. 

We can also set access/modify time of another file as a reference, to the desired file. Here the access/modify time of reference.txt is set to the test.txt

$ touch -r reference.txt  test.txt

10) Specify datetime as a string.

In the given example it modifies the date and by default it sets the time to 00:00

$ touch -c -d ’15 Oct’ test.txt

In the given example the time specified is modified and the current date is set.

$ touch -d ’20:35′ abc.txt

If you need any further assistance please contact our support department.

 

0.00 avg. rating (0% score) - 0 votes

You can skip to the end and leave a response. Pinging is currently not allowed.

Leave a Reply