|
Author: Lubos Rendek
Date: 21.03.2010
Name
touch [man page] - change file timestamps
Usage
When working with a Linux operating system, sometimes a need arises to create an empty file. Some service, which an administrator is configuring, requires a certain file to be created before the service can start and what is more this file may need to have certain access time stamp. If you are, for example, running a photo album web application which automatically adds your images to the album and sorts them according to the image creation time, the images you upload must have set accurate creation time in order this can work. What if the time set in your camera is incorrect?
A tool which comes handy in both scenarios is the touch command. Touch command not only provides a simple way to create files, but it also allows a user to alter access and modification time for a given file.
NOTE: Linux system does not store a file creation time. The information stored instead is the access time. The access time does not necessarily represent creation time and in many situations it represents access time only. This is because access time gets updated every time a file is accessed / read.
NOTE: In this article, we are going to use the stat command to observe changes to file's access and modification time made by touch command.
File creation
As it was already mentioned a touch command provides a simple way of creating empty files. The syntax is rather simple:
linuxconfig.org:~$ ls linuxconfig.org:~$ touch fileA linuxconfig.org:~$ ls fileA linuxconfig.org:~$
Execution of touch command with an argument fileA will produce a single empty file named fileA. Touch command can accept more than one argument and as result:
linuxconfig.org:~$ touch fileB fileC fileD linuxconfig.org:~$ ls fileA fileB fileC fileD linuxconfig.org:~$
it will create three additional files called fileB fileC and fileD. It is important to understand that all files created by touch command are empty, which means that touch command does not add any characters to newly created files.
Change file timestamps
At this point, we need to have a close look at earlier created files. stat command will return verbose information associated to a file given to it as its argument.
linuxconfig.org:~$ stat fileA File: `fileA' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 802h/2050d Inode: 1867780 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 1001/linuxconfig) Gid: ( 1001/linuxconfig) Access: 2010-03-21 06:22:52.000000000 +1100 Modify: 2010-03-21 06:22:52.000000000 +1100 Change: 2010-03-21 06:22:52.000000000 +1100 linuxconfig.org:~$
stat command returned detailed information about fileA status in regard to a present file system. The only interesting bit connected to this article is access and modify time.
Access time
Access time refers to a time when to file was last time accessed. In other words, every time some utility reads a content of fileA, fileA will change its access time. Following example demonstrates this point:
linuxconfig.org:~$ date Sun Mar 21 06:23:08 EST 2010 linuxconfig.org:~$ stat -c %x fileA 2010-03-21 06:22:52.000000000 +1100 linuxconfig.org:~$ cat fileA linuxconfig.org:~$ stat -c %x fileA 2010-03-21 06:23:19.000000000 +1100 linuxconfig.org:~$
First, we have used the date command to retrieve a current date and time. Next command executed is stat command which returns an access time of fileA. Cat command is used to access / read a content of fileA. There is no output since fileA is an empty file. Executing a stat command again with fileA as an argument indicates that access time has been changed.
Modify time
Modify time is altered when the content of a file itself has been modified. In the following example, we use echo command to modify a content of fileA:
linuxconfig.org:~$ date Sun Mar 21 06:23:23 EST 2010 linuxconfig.org:~$ stat -c %y fileA 2010-03-21 06:22:52.000000000 +1100 linuxconfig.org:~$ echo addline > fileA linuxconfig.org:~$ stat -c %y fileA 2010-03-21 06:23:35.000000000 +1100 linuxconfig.org:~$
Once again, we have used a date command to show a current system time. Next command executed was a stat command which in this case displayed modify time of a fileA. Next, we have used an echo command to add new line into fileA. Lastly, stat command confirms that modify time has been changed. As a side-note it should be mentioned that modifying content of file will alter also file's change time.
Change time
In regard to a touch command, we are not interested in change time. Nevertheless, just for a completeness we shortly examine also change time. Change time holds a time when file's meta data or inode information is altered. Meta data includes change of permissions or file ownership. Following example illustrates this idea:
linuxconfig.org:~$ date Sun Mar 21 06:23:40 EST 2010 linuxconfig.org:~$ stat -c %z fileA 2010-03-21 06:23:35.000000000 +1100 linuxconfig.org:~$ chmod +x fileA linuxconfig.org:~$ stat -c %z fileA 2010-03-21 06:23:51.000000000 +1100 linuxconfig.org:~$
Same as in both preceding examples date command displays a current date and time. stat command reveals a current value of file's change time. chmod command added executable permissions to a file. When comparing both time values, it is clear that a modification of fileA's permissions resulted in updating a change time value to a current system time.
|