2

I would like to change all of the timestamps of some files to the immediate date. It appears that I would need to use touch -A * (more or less; realizing that I am going to use a more complex form of globbing), however it requires arguments in the following from: [-][[hh]mm]SS

How would I coax date into providing the properly formatted output to call as an argument from touch to accomplish this in the format provided above for touch?

4
  • 1
    @PM2Ring - OSX touch has -A (adjust). OP, could you elaborate on "immediate date" ? Does that mean current time/date ? Oct 28, 2014 at 1:11
  • 1
    @don_crissti Thanks. I think the OP wants to set atime and mtime of the selected files to the current time. And if that's so, using -A seems like a complicated way to do it.
    – PM 2Ring
    Oct 28, 2014 at 1:27
  • @PM2Ring I want to set all time attributes: creation, modification and access to now.
    – ylluminate
    Oct 30, 2014 at 17:58
  • You can't change the ctime of a file using touch, only mtime &/or atime. The ctime can be modified, but it's not easy to set it to a time of your choice. See this Stack Exchange question java.nio.Files - setting “Change time” attribute on a file for some info.
    – PM 2Ring
    Oct 31, 2014 at 1:25

3 Answers 3

1

I advise you to not worry about changing a file's ctime, it's really not worth the hassle (as I discovered myself a month or two ago when writing a script to organize files for simple MP3 players). As well as the methods discussed in the SO question I linked to above, there is another straight-forward method: copy the file to a temporary name, delete the original, and then rename the temporary. Of course, that thrashes the disk a fair bit if you want to process a lot of files.

According to the touch man page provided above by don_crissti, the purpose of the -A option in your version of touch is to allow you to apply a time delta to the current mtime &/or atime of the file(s). So to use that -A option to set the atime and mtime of a file to now means that you'd have to calculate the time difference between now and the file's existing atime and mtime, which is an unnecessary complication, as I alluded to in my comment.

The easiest way to set the mtime & atime of a set of files to the current time is to simply use touch with its default options. Or, if you want the files to all have identical times, touch one file first, and then use that as the reference file on the rest of the files, using touch's -r option.

0
0

You can use command substitution for this

$ touch -t $(date +%m%d%H%M) file
1
  • Close! But not quite, this works for -t, but not -A.
    – ylluminate
    Oct 27, 2014 at 18:45
0

The touch utility on macOS has a -A option that allows you to adjust the modification and access timestamps on set of files ("mtime" and "atime" respectively) by a number of hours, minutes and/or seconds. In comments, you appear to want to set the timestamps to the current time, not adjust them. The -A option would therefore be the wrong option to use.

To change the mtime and atime timestamps on a set of files to the current time, use

touch -c pattern

where pattern is some pattern that matches the files you'd like to touch. The -c option makes sure that you don't create files if the pattern does not match, or if you provide an explicit list of files that contains nonexistent names.

As a side effect of modifying the mtime and atime timestamps, the ctime timestamp ("inode change timestamp") will also be modified.

To set the btime ("birth timestamp", which may be what you call the "creation timestamp" in your comment), you may want to use the (deprecated) SetFile command, which is used to "set attributes of files and directories" on macOS.

SetFile -d "$(date +'%m/%d/%Y %H:%M:%S')" pattern

Use SetFile with the same datestamp string with the -m option to update the mtime. Setting the btime or mtime in this manner also updates the ctime of the file. The SetFile utility does not/can not update the atime timestamp.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .