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
– AMADANON Inc. Jan 12 '14 at 20:12Unless 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. – VL-80 Jan 13 '14 at 02:01tar
(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 ;-) – Steve Jessop Jan 13 '14 at 02:14age
of a file. That would be acreation
timestamp and there is no such timestamp for files in unix. – Kevin Fegan Jan 13 '14 at 02:21stat
command used in that particular unix showsBirth: -
, mine does not. – Kevin Fegan Jan 13 '14 at 14:20touch
when building release disks of software to set the time on the files to the version we were putting out. For version 5.5 we'd touch them with 5:50am or something. That way you could tell from a directory listing what version was installed (otherwise customers usually just said "the latest"). – Kevin Rubin Jan 13 '14 at 17:53I also use touch to force make to rebuild files frequently when I'm want to test changes I've made to a Makefile.
– Alan De Smet Jan 13 '14 at 19:54uname -a
I get:Linux ... 3.2.45 #4 SMP Wed May 15 19:43:53 CDT 2013 x86_64 x86_64 x86_64 GNU/Linux
. The only unix machine I have access to right now is toSSH
to my web-host, so I don't know much else about the machine/OS. – Kevin Fegan Jan 13 '14 at 21:28"stat --version"
I get:"stat (GNU coreutils) 8.4, Copyright (C) 2010 Free Software Foundation, Inc."
. For"stat 'filename'"
, the last 3 lines are like:"Access: 2014-01-13 14:50:40.825830546 -0600 Modify: 2014-01-12 22:18:47.819406814 -0600 Change: 2014-01-12 22:18:47.819406814 -0600"
... no"Birth:"
timestamp. – Kevin Fegan Jan 13 '14 at 23:08