views:

153

answers:

3

Hi, When I create a directory say d1 and after 5 seconds d1/d2, then d1 timestamp gets updated to that of d2. After 5 seconds, when I create d1/d2/d3, only d2 timestamp gets updated to d3 but not that of d1.

Basically, my requirement is that not only the parent folder but all the folders from root to parent folder must get updated with the time of the parent folder.

Is there any way to update the timestamp of d1 with that of d3?

Please clarify.

+3  A: 
find . -type d -exec touch -m -r d3 {}\;

Finds all directories in the current directory and updates the timestamp to the current time...

Chinmay Kanchi
He wants the timestamp of all directories changed to timestamp of d3. Your solution will change it to current time.
gameover
Good point. Fixed now...
Chinmay Kanchi
+2  A: 
find . -type d -exec touch  -r d1/d2/d3 -m {} \;
touch options:
-r :reference file. The timestamp of this ref file will be used for touching.
-m :change the modification time.

This will find all the directories under pwd, and will modify the modification time of each to the modification time of d1/d2/d3 directory. It is assumed that you are in the directory that has directory d1.

gameover
A: 

This will set the modification time of only the directories that are on the path to the added file.

So in this tree

d1
d1/d2
d1/d2/d3  <-- this is the one we are adding
d1/d2a
d1/d2a/d3a

Only d1 and d1/d2 will be affected.

CHILD="d1/d2/d3"
DIR=`dirname "$CHILD"`
while [[ "$DIR" != "." ]]
do
    touch -m -r "$CHILD" "$DIR"
    DIR=`dirname "$DIR"`
done
martin clayton