3

For example, if a file has been changed in that directory, then directory modification date should be updated. Basically any changes that happen in that directory should trigger a "touch" on the directory.

Can I make the system do that automatically when these file operations are performed?

If yes, could also the parent directories be 'touched', until root?

  • 1
    Moving a file into a directory should update that directory's modification date, that's the default behavior. What file system do you use? And which operating system? – terdon Jul 24 '13 at 15:23
  • it's linux 2.6.32 and file system shows "ext3" – Annie Jul 24 '13 at 15:28
  • 1
    And you're sure that moving a file into a directory does not change the directory's mod date? Very strange. – terdon Jul 24 '13 at 15:31
  • you're right, it does on file move. Also works on rename and delete (sorry didn't know). But it doesn't on file content change :( – Annie Jul 24 '13 at 15:34
  • OK, could you please edit your question so it asks what you want to know? – terdon Jul 24 '13 at 15:40
1

On Linux, you can use the inotify interface to perform an action when a file is modified. From the shell, you can use inotifywait.

inotifywait -e modify --format '%f' /path/to/directory |
while read line; do
  if [ -n "$line" ]; then touch /path/to/directory; fi
done

If you want to update the timestamp of /path/to/directory when a file is modified in a subdirectory as well, add the -r option to inotifywait.

1

This is an addition to Gilles's answer.

To touch the directory and all of its parent directories, run something along the lines of this (untested):

dir=/path/to/directory
inotifywait -e modify --format '%f' "$dir" |
while read line; do
   if [ -n "$line" ]; then
      # Handle relative paths.
      if [ "$(echo "$line" | cut -c1)" != / ] ; then
          path=.
      fi

      echo "$dir" | tr '/' '\n' |
      while read part ; do
         touch "$path/$part"
      done
   fi
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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