Tuesday, June 28

touch command:


touch command:

We all know the command touch and we generally use it for creating a 0 byte file (similar to “filename” or “cat /dev/null > filename” or “cp /dev/null filename”). But many of us don’t know that the main use of touch is to update/change the access, modification and last changed times of a file.

Basic Options:

touch 123.txt – will update the times of 123.txt to current time OR creates 123.txt with current date, if NOT exists.

touch –t “timestamp” 123.txt - will update the times of 123.txt to timestamp OR creates 123.txt with timestamp.

Timestamp - [[CC]YY]MMDDhhmm[.SS]

                          CC   The first two digits of the year. (optional, if its 19)

                          YY   The second two digits of the year.  [CCYY is optional, if its same year)

                          MM   The month of the year (01-12).

                          DD   The day of the month (01-31).

                          hh   The hour of the day (00-23).

                          mm   The minute of the hour (00-59).

                          .SS   The second of the minute (00-61). (optional, if its 00)

touch -t 01010000.00 123.txt
-rw-r--r--   1 vs1250     ospcm            0 Jan  1 00:00 123.txt

touch -t 201501010000.00 123.txt
-rw-r--r--   1 vs1250     ospcm            0 Jan  1  2015 123.txt


touch -t 8601010000.00 123.txt
-rw-r--r--   1 vs1250     ospcm            0 Jan  1  1986 123.txt

Now, whatever we see above will update the timestamps of both access and modification times of the file.

–a option is to change only the access time and
–m option is to change only the modified time.

touch -a -t 201501010000.00 123.txt - will update the access time of123.txt to timestamp. If NOT exist, it creates 123.txt with timestamp as access time and current time as modified time.
touch -m -t 201501010000.00 123.txt - will update the modify time of123.txt to timestamp. NOT exist; it creates 123.txt with timestamp as modified time and current time as access time.

-r option is to change the times of a file with that of another file.

touch -r 123.txt newfile.txt - will update the times of newfile.txt with that of 123.txt.
-rw-r--r--   1 vs1250     ospcm            0 Jan  1 00:00 newfile.txt
-rw-r--r--   1 vs1250     ospcm            0 Jan  1 00:00 123.txt

You can mix the combination of the options and use them for various needs. BUT, always –t and –r should be used exclusively.

No comments:

Post a Comment