84

What's the point of the touch command? I know I can create empty files with it, but so is also the case with echo -n.

Otherwise, why would someone need to change the timestamps of a file? Unless to create the false impression about the age of a file, I don't see any other use, and this one is not a legitimate one (from my point of view).

Bernhard
  • 12,272
Quora Feans
  • 3,866
  • 18
    instead of echo -n > filename, you can run the following: >filename – AMADANON Inc. Jan 12 '14 at 20:12
  • 78
    What are the illegitimate uses? – bmargulies Jan 12 '14 at 23:54
  • 11
    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 the touch command and it is very convenient and simple to have it like that. – VL-80 Jan 13 '14 at 02:01
  • 7
    Consider what 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 why touch (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 a sh script ;-) – Steve Jessop Jan 13 '14 at 02:14
  • 5
    In unix, there is no timestamp that describes the age of a file. That would be a creation timestamp and there is no such timestamp for files in unix. – Kevin Fegan Jan 13 '14 at 02:21
  • 1
    "Unless to create the false impression about the age of a file ... this one is not a legitimate one (from my point of view)." Your point of view may be worth to be altered. – glglgl Jan 13 '14 at 12:22
  • @KevinFegan - Posix doesn't say unices can't represent create times (usually called birthtimes in Unix world, cf. the man page for modern finds; they exist in Solaris and Darwin/OSX, for instance), it is just that the semantics of the stat() syscall makes it hard to work with them (it returns a single record with all info), extensions have been ad hoc. The xstat() syscall from Solaris, with a query semantics, fixes the problem with stat() and is used with ZFS; I have the idea there were plans to bring the syscall to Linux, but I don't know what happened. – Charles Stewart Jan 13 '14 at 12:45
  • @CharlesStewart - "Birth time". Thanks for the info about that. I've never worked with a unix with that feature. I think it's a great idea to add that to (eventually) all unix distributions. I now notice that in an answer to this question, the stat command used in that particular unix shows Birth: -, mine does not. – Kevin Fegan Jan 13 '14 at 14:20
  • 2
    We used to use touch 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:53
  • 2
    Used recently: Makefile grabs files from remote source as a step; fetching tool keeps remote (old) timestamp. When I re-run make, it decides the files are too old and unnecessarily re-downloads them. The easy fix? "touch downloads/*"

    I 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:54
  • @KevinFegan: slm's output looks like its from the GNU coreutils stat(1), which I think has had support for xstat() since 2010. Are you on a BSD? – Charles Stewart Jan 13 '14 at 21:14
  • @CharlesStewart - for uname -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 to SSH to my web-host, so I don't know much else about the machine/OS. – Kevin Fegan Jan 13 '14 at 21:28
  • @KevinFegan: That's odd; maybe you have an older version of coreutils. I have an instance of v8.5 (from stat --version) that does not have the Birth field and v8.13 that does. – Charles Stewart Jan 13 '14 at 21:39
  • @CharlesStewart - for "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
  • @bmargulies Why don't you have a seat over there? – user3490 Jan 15 '14 at 13:57
  • If I see touch in a script, I don't have to think about shell redirection, etc.. It expresses intent more clearly IMHO. 2) touch only updates the time stamp if a file already exists. This could be used as a quick way to record an event time while leaving the contents of the file available for other uses.
  • – Joe Jan 20 '14 at 04:20
  • +1 for all the stuff I learned from reading all the answers. – Joe Jan 20 '14 at 04:40