0

Which command can I use to:

  • Enter the last modified directory in the current directory.
  • If no directory exists or is possible to enter, do nothing.

I'm looking for an alias named ca that will work at least in zsh, but also in bash and ash, if possible.

2 Answers 2

0

Here's one possible solution:

alias ca='cd $(find . -maxdepth 1 -type d -printf "%T@ %p\n" | sort -n | tail -1 | cut -d" " -f2-)'

But it has the following problems:

  • Using the output directly from find without using something like print0, which results in directories with spaces in the name to not be handled correctly.
  • Entering . if that is the last modified directory, instead of one of the directories within ..
0

This should work on zsh and bash as far as I know (not an alias, though):

ca() { 
  local dir newer

  for dir in ./*; do
    if [[ ! -d "${dir}" ]]; then
      continue
    fi
    if [[ "${dir}" -nt "${newer}" ]]; then
      newer="${dir}"
    fi
  done

  if [[ -x "${newer}" ]]; then
    cd -- "${newer}"
  fi
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .