Touch command with examples in linux / Unix

Touch command with examples in linux / Unix

Touch command is used to create a empty file in linux / Unix. Using touch command time stamp of a file can be changed. Touch command can change a file’s access and modification time. Why we are waiting then lets explorer the uses of touch command with examples.

Syntax:

#touch [options] file(s)

How to create a empty file using touch command

#touch newfile
#cat newfile
#ls -l newfile
#stat newfile

Touch command with examples in linux / Unix

The above screen shot explained you that using touch command you have created a empty file in linux/Unix. stat command will explain about the file created, modified time , Owner and many more details.

Touch command to create multiple empty files.

#touch 1 2 3 4 5
#ls -l

Touch command with examples in linux / Unix

we have created five empty files using the above command.

Touch command to change the time stamp to old one

OK! Lets change the time stamp of the newfile to an old one. To change the time stamp the syntax is given below

Syntax:

#touch -t [[CC]YY]MMDDhhmm[.ss]

#ls -l newfile
#touch -t 201201021020.30 newfile
#ls -l newfile
#stat newfile

Touch command with examples in linux / Unix

As you can see that the file access and modified time is changed to 2012 January Second and 10:20:30(hh:mm:ss) hours.

2012 – Years
01   – Month, January
10   – Date, Tenth
10:20:30(hh:mm:ss)

Touch command to change only modification time

#ls -l newfile
#stat newfile
#touch -m -t 201206070809.10 newfile
#stat newfile
#ls -l newfile

Touch command with examples in linux / Unix

Touch command to recursively change time stamp to multiple file

Time stamp can be changed to multiple files using -r option

Syntax:

#touch -r file file1 file2 file3

Options:

-r, –reference=FILE  use this file’s times instead of current time
#ls -l
#touch -r newfile 5
#ls -l

Touch command with examples in linux / Unix

As from the above command the time stamp of the newfile is copied to file name 5.

Touch command to change folders time stamp

Even the -r option can be applied also to folders.

#ls -ld ../testing
#touch -r newfile ../testing
#ls -ld ../testing
The above command will change the time stamp of the folder testing to same as like newfile.




Leave a Reply

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