Tag Info

Hot answers tagged

90

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


39

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


36

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


32

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


26

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


25

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


23

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


22

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


19

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.


19

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


19

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


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

rsync is probably the best tool for this. There are a lot of options on this command so read man page. I think you want the --checksum option or the --ignore-times


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

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

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

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


15

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


13

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


12

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


12

Try running this code snippet: if [[ 5 < 20 ]] then echo "5 < 20, as expected" else echo "5 is not less than 20, but why?" fi And the output would be 5 is not less than 20, but why?. The answer is that you're using the < conditional expression operator, which is documented as doing: string1 < string2 True if ...


11

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


11

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


10

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


10

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


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

The Unix time is given as seconds since the epoch, the number of seconds that has passed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970, not counting leap seconds. The GNU date command has some very nice features that allow you to translate between different time formats. These are explained very nicely in man date so I will only ...


8

The ext4 file system does store the creation time. stat %W can show it to you.


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



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