Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

share|improve this question
add comment

4 Answers 4

You can change the modification time of a file using the touch command:

touch filename

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:

touch -d "2 hours ago" filename

If you want to modify the file relative to its existing modification time instead, the following should do the trick:

touch -d "$(date -r filename) - 2 hours" filename

If you want to modify a large number of files, you could use the following:

find DIRECTORY -print | while read filename; do
    # do whatever you want with the file
    touch -d "$(date -r "$filename") - 2 hours" "$filename"
done

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:

find DIRECTORY -exec touch -d "2 hours ago" {} +

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

share|improve this answer
    
To mention the simpler case when all files are in the same folder: touch -d "2 hours ago" /path/*.txt, for example. –  enzotib Sep 22 '11 at 7:05
1  
I'd also add that changing the ctime is not possible in any standard way. –  arrange Sep 22 '11 at 7:40
    
+1 James: A really good and complete answer. –  Richard Holloway Sep 22 '11 at 20:56
add comment

Thanks for the help. This worked for me:

In the terminal go to the directory for date-edit. Then type:

find -print | while read filename; do
    # do whatever you want with the file
    touch -t 201203101513 "$filename"
done

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.

See my webpage

share|improve this answer
add comment

This little script at least works for me

#!/bin/bash

# find specific files
files=$(find . -type f -name '*.JPG')

# use newline as file separator (handle spaces in filenames)
IFS=$'\n'

for f in ${files}
do
 # read file modification date using stat as seconds
 # adjust date backwards (1 month) using date and print in correct format 
 # change file time using touch
 touch -t $(date -v -1m -r $(stat -f %m  "${f}") +%Y%m%d%H%M.%S) "${f}"
done
share|improve this answer
add comment

just change date and time in settings. then save your file, it automatically changes

share|improve this answer
2  
Snej is looking for a batch or command line solution that can be used to modify multiple files. Changing system time and date and the modify the files is probably not an ideal solution. –  fabricator4 Nov 28 '12 at 9:35
    
too much triiiiiiicky!!! –  Philippe Gachoud Oct 8 '13 at 21:25
    
For those with other OS's brought here via web search, this answer is the simplest way to change the date of a file on Mac OSX ('touch -d' results in 'illegal option'). –  alanning Feb 18 at 3:50
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.