The touch
command's primary purpose is manipulating the timestamps of files, and for creating files.
Examples
1. creating files
$ ls -l
total 0
$ touch file{1..3}
$ ls -l
total 0
-rw-rw-r--. 1 saml saml 0 Jan 12 13:33 file1
-rw-rw-r--. 1 saml saml 0 Jan 12 13:33 file2
-rw-rw-r--. 1 saml saml 0 Jan 12 13:33 file3
NOTE: The total 0
output from ls -l
is correct. This implementation of ls
shows the number of blocks being used by the files when they're listed. You can assure yourself of this fact by adding the -s
switch to ls
. This will list the blocks in use by each file. Here I've added 2 characters to file1 & file2.
Example
$ ls -ls
total 8
4 -rw-rw-r--. 1 saml saml 3 Jan 13 12:07 file1
4 -rw-rw-r--. 1 saml saml 3 Jan 13 12:09 file2
0 -rw-rw-r--. 1 saml saml 0 Jan 13 12:05 file3
2. time/date info of a file - stat command
$ stat file1
File: ‘file1’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd02h/64770d Inode: 11403667 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/ saml) Gid: ( 1000/ saml)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2014-01-12 13:33:38.279456149 -0500
Modify: 2014-01-12 13:33:38.279456149 -0500
Change: 2014-01-12 13:33:38.279456149 -0500
Birth: -
We can use touch
to manipulate the various timestamps on a given file.
3. excerpt from touch man page
-a change only the access time
-m change only the modification time
-t STAMP
use [[CC]YY]MMDDhhmm[.ss] instead of current time
4. manipulating access time
$ touch -a -t200001010000 file1
$ stat file1
File: ‘file1’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd02h/64770d Inode: 11403667 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/ saml) Gid: ( 1000/ saml)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2000-01-01 00:00:00.000000000 -0500
Modify: 2014-01-12 13:33:38.279456149 -0500
Change: 2014-01-12 13:38:52.023434696 -0500
Birth: -
5. manipulate modify time
$ touch -m -t200001010000 file1
$ stat file1
File: ‘file1’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd02h/64770d Inode: 11403667 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/ saml) Gid: ( 1000/ saml)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2000-01-01 00:00:00.000000000 -0500
Modify: 2000-01-01 00:00:00.000000000 -0500
Change: 2014-01-12 13:39:31.060432026 -0500
Birth: -
You might be wondering about the change time (ctime). That cannot be manipulated using touch
. That tracks the time any of the meta data was touched on the file. See this U&L Q&A for more details, titled: What can you do to a file without triggering the "Change" Timestamp?.
echo -n > filename
, you can run the following:>filename
Unless to create the false impression about the age of a file
. Not necessary false impression. What if you want to change the modification time? It can be useful in scripts. This script heavily depends on thetouch
command and it is very convenient and simple to have it like that.tar
(or other de-archivers) do when they extract an archive. Generally they set the modification time of the file to the time from the archive, not the time the archive was extracted, and this is a desirable feature. Since it's legitimate for a user-mode program to create false timestamps there's not much argument whytouch
(or some other command-line program) shouldn't allow it based on a command-line argument. Learning C doesn't make you more legitimate than someone writing ash
script ;-)age
of a file. That would be acreation
timestamp and there is no such timestamp for files in unix.