Is there a way to change the date when a file was modified/created (which is shown in Nautilus or with the ls -l command)? Ideally I am looking for a command which can change the date/time stamps of a whole bunch of files to a certain amount of time earlier or later (e.g. +8 hours or -4 days etc.).
As long as you are the owner of the file (or root), you can change the modification time of a file using the
touch
command:By default this will set the file's modification time to the current time, but there are a number of flags, such as the
-d
flag to pick a particular date. So for example, to set a file as being modified two hours before the present, you could use the following:If you want to modify the file relative to its existing modification time instead, the following should do the trick:
If you want to modify a large number of files, you could use the following:
You can change the arguments to
find
to select only the files you are interested in. If you only want to update the file modification times relative to the present time, you can simplify this to:This form isn't possible with the file time relative version because it uses the shell to form the arguments to
touch
.As far as the creation time goes, most Linux file systems do not keep track of this value. There is a
ctime
associated with files, but it tracks when the file metadata was last changed. If the file never has its permissions changed, it might happen to hold the creation time, but this is a coincidence. Explicitly changing the file modification time counts as a metadata change, so will also have the side effect of updating thectime
.Easiest way - accessed and modified will be the same:
Where:
If you wish to use
NOW
just drop the-t
and the timestamp.To verify they are all the same:
stat fileName.ext
See: man touch
Thanks for the help. This worked for me:
In the terminal go to the directory for date-edit. Then type:
You wil see a ">" after you hit enter, exept for the last time -> "done".
Note: You may want to change "201203101513"
"201203101513" = is the date you want for all the files in this directory.
Touch can reference a file's date all by itself, no need to call
date
or use command substitution. Here's a bit from touch's info page:For example, to add 8 hours to a file's date (filename of
file
quoted just in case of spaces, etc):Using a loop over all files in the current dir:
I've heard that using a
*
and lettingfor
pick the filenames itself is safer, but usingfind -print0 | xargs -0 touch ...
should handle most crazy characters like newlines, spaces, quotes, backslashes in a filename. (PS. try not to use crazy characters in filenames in the first place).For example, to find all files in
thatdir
whose filenames start with ans
, and add one day to those file's modified timestamp, use:This little script at least works for me:
It's been a long time since I wrote any kind of Unix program, but I accidentally set the year incorrectly on a bunch of Christmas photos, and I knew if I didn't change the date from 2015 to 2014 it would be a problem later on.
Maybe, this is an easy task, but I didn't find any simple way to do it.
I modified a script I found here, which originally was used to modify the date by minus one month.
Here's the original script:
Here's my modified script that forced the date to the year "2014":
I now realize I could have done a more generic version:
To use this file you would need to write it and
to execute:
fn=your-file-name-that-is-your-command
Example
will change the date by minus one year in the directory where you are.