0

Would like to easily located folders to archive/trash and an easy way to automate this would be to choose folders I haven't made any use of for years. Since folders themselves can have different modification dates than the files they contain, I'm wondering if there's already a *nix command that does this for me.

Otherwise I suppose I could write a script that first locates directories that haven't been updated or accessed before a certain date, and then recursively loop through their contents to make sure they also haven't been accessed or modified since that date.

But if a tool like this already exists, I'd rather not re-invent the wheel.

1
find /some/where -type d \( -atime +1234 -o -mtime +1234 \) -print | ...
1
  • Note that this answer works, but only because, in osx at least, it appears that each time a file is accessed, created, or modified, the access time for each of its parent subdirectories is also updated. Probably just the atime filter is needed, I can't think of any situation where a directory would be modified but not accessed. – Jordan Reiter Aug 20 '15 at 15:21
1

The access time on a directory is rarely useful. The first time you run ls or find in the directory, its access time is updated.

You could simply look for files that haven't been accessed in a long time:

find . -type f -atime +999 -print

If you're only interested in directories where none of the files have been accessed in a long time, you can combine a find command to explore directories and a find command to explore files within a directory.

find . -type d -exec sh -c '
    recent=$(find "$0" -mindepth 1 -maxdepth 1 ! -type f -atime +999 -print | head -n 1)
    if [ -z "$recent" ]; then printf %s\\n "$0"; fi
  ' {} +

Note that this snippet can print out a directory where none of the regular files have been accessed recently but there are files that have been accessed recently. Skipping these isn't easy with find alone, unless you just remove -maxdepth 1 to traverse directories independently which would trigger a lot of repeat disk activity. Here's an untested bash script that does the directory walk manually, keeping track of which directories only contain files that haven't been accessed recently.

#!/bin/bash
shopt -s dotglob
explore () {
  local entry keep=
  for entry in "$1/"*; do
    if [ -L "$entry" ]; then
      :
    elif [ -d "$entry" ]; then
      if explore "$entry"; then
        keep=1  # Keep this directory, it contains kept directories
      fi
    elif [ -f "$entry" ]; then
      if [ -z "$(find "$entry" -atime +999 -print)" ]; then
        keep=1  # Keep this directory, it contains a recently-accessed file
      fi
    fi
  done
  if ((!keep)); then
    echo "$1"
    return 1
  fi
  return 0
}
explore .

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.