Tag Info

Hot answers tagged

87

One advantage of touch is that it can specify arbitrary timestamps, while echo -n will always result in the current time. An example of a legitimate use is to update a timestamp of a source code file so a program like make will consider the source file newer than its compiled object and rebuild it. Other uses are to create files that function solely ...


38

The underlying system call (utime) is important for various utilities like tar to be able to set the timestamps of a newly copied (un-tarred) file. Some backup utilities can also be optioned to reset the last-accesstime of files that they have copied. One legitimate use of touch is to create a file with a particular timestamp. Such a "reference" file can ...


35

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 ...


26

Most unices do not have a concept of file creation time. You can't make ls print it because the information is not recorded. If you need creation time, use a version control system: define creation time as the check-in time. If your unix variant has a creation time, look at its documentation. For example, on Mac OS X (the only example I know of¹), use ls ...


24

Sometimes you don't even need the source code. Use strace. $ strace touch -t 201212121212 foobar execve("/usr/bin/touch", ["touch", "-t", "201212121212", "foobar"], [/* 61 vars */]) = 0 [...] lots of noise [...] open("foobar", O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK, 0666) = 3 dup2(3, 0) = 0 close(3) ...


19

The stat command may output this - I guess it depends on the filesystem you are using. stat calls it the "Birth time". On my ext4 fs though it is empty. %w Time of file birth, human-readable; - if unknown %W Time of file birth, seconds since Epoch; 0 if unknown stat foo.txt File: `foo.txt' Size: 239 Blocks: 8 IO Block: ...


19

Apart from the other two very good answers you got, yet another use is, in your words, to create the false impression about the age of a file for example for use in backup schemes. You might want to not update a backup with a newer one since they're identical, or since the changes are irrelevant compared to the expense of updating a backup on, say, a ...


18

moreutils includes ts which does this quite nicely: command | ts '[%Y-%m-%d %H:%M:%S]' It eliminates the need for a loop too, every line of output will have a timestamp put on it. $ echo -e "foo\nbar\nbaz" | ts '[%Y-%m-%d %H:%M:%S]' [2011-12-13 22:07:03] foo [2011-12-13 22:07:03] bar [2011-12-13 22:07:03] baz You want to know when that server came back ...


18

bash actually remembers the times until you close the shell. So try running HISTTIMEFORMAT='%x %X ' history If you also put HISTTIMEFORMAT=<some format> in your ~/.bashrc, it will also get written to ~/.bash_history on exit, so you can check what happened in previous shell sessions too.


18

touch calls the utimes system call to set the file's modification time and its access time. On some systems, instead of utimes, it opens the file and then sets the file times through the descriptor, e.g. with utimensat under Linux. You can see how touch works on your system by looking at the system calls it makes. Under Linux, use strace, e.g. strace touch ...


18

One of the most common uses of touch is -- or at least used to be -- to trigger a rebuild of code. The usual build tools -- make and its derivitives -- try to save work by not recompiling/rebuilding files unless one of the files which went into creating them has changed -- for example, if a .o file is more recent than the .c source, it's usually safe to ...


16

Linux offers three timestamps for files: time of last access of contents (atime), time of last modification of contents (mtime), and time of last modification of the inode (metadata, ctime). So, no, you cannot. The directory's mtime corresponds to the last file creation or deletion that happened, though.


16

Firstly, if you are expecting these timestamps to actually represent an event, bear in mind that since many programs perform line buffering (some more aggressively than others), it is important to think of this as close to the time that the original line would have been printed rather than a timestamp of an action taking place. You may also want to check ...


16

man 5 sudoers informs us that there is an option timestamp_timeout: timestamp_timeout Number of minutes that can elapse before sudo will ask for a passwd again. The timeout may include a fractional component if minute granularity is insufficient, for example 2.5. The default is 5. Set this to 0 to always ...


16

The POSIX standard only defines three distinct timestamps to be stored for each file: the time of last data access, the time of last data modification, and the time the file status last changed. That said, modern Linux filesystems, such as ext4, Btrfs and JFS, do store the file creation time (aka birth time), but use different names for the field in ...


15

You could skip the echo, and just put the message in the date command. date allows you to insert text into the format string (+%R in your example). For example: date +"%R usb device already mounted" You can also throw it into a shell function for convenience. For example: echo_time() { date +"%R $*" } echo_time "usb device already mounted" This is ...


12

Early versions of C didn't have unsigned integers. (Some programmers used pointers when they needed unsigned arithmetic.) I don't know which came first, the time() function or unsigned types, but I suspect the representation was established before unsigned types were universally available. And 2038 was far enough in the future that it probably wasn't ...


11

Generally speaking, when you're looking for files in a directory and its subdirectories recursively, use find. The easiest way to specify a date range with find is to create files at the boundaries of the range and use the -newer predicate. touch -t 201112220000 start touch -t 201112240000 stop find . -newer start \! -newer stop


11

add switch -h touch -h -t 201301291810 myfile.txt Mandatory arguments to long options are mandatory for short options too. -a change only the access time -c, --no-create do not create any files -d, --date=STRING parse STRING and use it instead of current time -f (ignored) -h, --no-dereference ...


10

I've worked with at least one web framework (can't remember which) that in development mode watched the source code files for changes and reloaded the application when that happened. Of course sometimes you wanted it to reload after changing something it wasn't watching, in which case touching its main configuration file did the trick. So, in the general ...


10

Legitimate use of touch: Update the timestamp of image files (GIF/JPG/etc) to the time they were taken. Not all renderers read or heed the EXIF information. Keep the c/m-time when mass updating a set of files where the timestamp is important for the bundle of files. Set the time of a batch (i.e. CSV) file if you need to do another import after a failure, ...


9

You can use touch -r to use another file's timestamp instead of the current time (or touch --reference=FILE) Here are two solutions. In each solution, the first command changes the modification time of the directory to that of the newest file immediately under it, and the second command looks at the whole directory tree recursively. Change to the directory ...


8

If you want to loop over files, never use ls*. tl;dr There are lots of situations where you'd end up deleting the wrong file, or even all files. That said, unfortunately this is a tricky thing to do right in Bash. There's a working answer over at a duplicate question my even older find_date_sorted which you can use with small modifications: counter=0 ...


8

date +%s.%N will give you, eg., 1364391019.877418748. The %N is the number of nanoseconds elapsed in the current second. Notice it is 9 digits, and by default date will pad this with zeros if it is less than 100000000. This is actually a problem if we want to do math with the number, because bash treats numbers with a leading zero as octal. This padding ...


8

TLDR; Use stap ("SystemTap") to create your own kernel API. Demo of ext4 creation time extraction below. You can extract the ext4 creation times on Fedora 19 systems. Here's mine: $ uname -a Linux steelers.net 3.11.1-200.fc19.i686.PAE #1 SMP Sat Sep 14 15:20:42 UTC 2013 i686 i686 i386 GNU/Linux It's clear that the inodes on my ext4 partitions have the ...


7

Mikel's answer is good, except that if you run the program and go away for a while, you can't really be sure when the process finished. So even if you have the time when you started the program, you don't know how much time it took. I don't have a solution for the case when you need to find out without preparation. However if you are going to do that again, ...


7

Here is how it works on Solaris. truss is used instead of strace which is quite a different command here. Like under Gnu/Linux, utimensat is the system call used. $ truss -vall -u a.out -f touch -t 1306080000 z 4160: execve("/usr/bin/touch", 0xF0770FC0, 0xF0770FD4) argc = 4 ... 4160/1@1: -> main(0x4, 0xf0770fc0, 0xf0770fd4, 0xf0770f7c) ... ...


7

One use is to correct the timestamp of a file that was improperly set. For example, most any way you copy a file (and for some ways that you might move a file) results in the new file having a timestamp of the current time, rather than the timestamp of the original file. Usually, this is not what is desired. Touch can "fix" that. Another use is to ...


6

Using Gilles' solution and after reading the man find(1) again I found a more simple solution. The best option is the -newerXY. The m and t flags can be user. m The modification time of the file reference t reference is interpreted directly as a time So the solution is find -type f -newermt 20111222 \! -newermt 20111225 The lower bound in ...


6

Assuming you don't need precision to the seconds, this should work. find . -type f -mmin -$(((`date +%s`-`date -d 20111222 +"%s"`)/60)) \! -mmin +$(((`date +%s`-`date -d 20111224 +"%s"`)/60)) EDIT: Changed cmin to mmin after @Eelvex's comment. EDIT: '\!' missing



Only top voted, non community-wiki answers of a minimum length are eligible