new_perl_user
asked on
Touch command in unix
Hi
I want to change the file's date from 9/27/2011 to 9/9/2011. How can I do this in unix using touch.
It should change only the date.
I want to change the file's date from 9/27/2011 to 9/9/2011. How can I do this in unix using touch.
It should change only the date.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
FILE=/path/to/testfile
DATE="20110909"
touch -t ${DATE}$(echo $(stat -c "%z" $FILE) | awk -F":| |\\\." '{print $2 $3 "." $4}') $FILE
DATE="20110909"
touch -t ${DATE}$(echo $(stat -c "%z" $FILE) | awk -F":| |\\\." '{print $2 $3 "." $4}') $FILE
touch -t 201109090000 filename
or, since the year is the same, touch -t 09090000 filename
-t STAMP
use [[CC]YY]MMDDhhmm[.ss] instead of current time
Woops didn't see the other answers, walked away for a few while writing my answer :p
OK, for the 100 files
touch -t 201127090000 /tmp/from
touch -t 201127092359 /tmp/to
DATE="20110909"
find /dir -type f -newer /tmp/from ! -newer /tmp/to |while read FILE
do
touch -t ${DATE}$(echo $(stat -c "%z" $FILE) | awk -F":| |\\\." '{print $2 $3 "." $4}') $FILE
done
touch -t 201127090000 /tmp/from
touch -t 201127092359 /tmp/to
DATE="20110909"
find /dir -type f -newer /tmp/from ! -newer /tmp/to |while read FILE
do
touch -t ${DATE}$(echo $(stat -c "%z" $FILE) | awk -F":| |\\\." '{print $2 $3 "." $4}') $FILE
done
You can also use
... stat -c "%y" ... ...
to keep the time of last modification, or
... stat -c "%x" ... ...
to keep the time of last access, instead of my
.. stat -c "%z" ... ...
which will keep the time of last change.
wmp
... stat -c "%y" ... ...
to keep the time of last modification, or
... stat -c "%x" ... ...
to keep the time of last access, instead of my
.. stat -c "%z" ... ...
which will keep the time of last change.
wmp
Is this something you want to do just once or several times?
If several times, accept one of the fine answers above.
If just once, and you are good with vi, you can do something like thsi
ls > foo
vi foo
1,$s/^/touch -d '2007-01-31 8:46:26' /
:x
sh foo
Credit goes to -W- for providing the touch command.
I don't want any points for this because this is a scripting thread and I gave a vi answer and the other answers are good and earlier. (It's just quicker for me, sometimes, when I just have to do whatever the task is once.)
If several times, accept one of the fine answers above.
If just once, and you are good with vi, you can do something like thsi
ls > foo
vi foo
1,$s/^/touch -d '2007-01-31 8:46:26' /
:x
sh foo
Credit goes to -W- for providing the touch command.
I don't want any points for this because this is a scripting thread and I gave a vi answer and the other answers are good and earlier. (It's just quicker for me, sometimes, when I just have to do whatever the task is once.)
ASKER