Is there any sort option available in find command to get directory with least access date/time
find . -type d -printf "%A@ %p\n" | sort -n | tail -n 1 | cut -d " " -f 2-
If you prefer the filename without leading path, replace %p
by %f
.
This sound like more of a job for ls
:
ls -ultd *|grep ^d
The problem with using find
, at least on my system (cygwin/bash), is that find
accesses the dirs, so all access-times result in current time, defeating your apparent purpose.
find -type d -printf '%T+ %p\n' | sort | head -1
source
the below linux command displays the access and modified time along with size
stat -f
find -type d -printf '%T+ %p\n' | sort
A simple shell script will also do:
unset -v oldest
for i in "$dir"/*; do
[ "$i" -ot "$oldest" -o "$oldest" = "" ] && oldest="$i"
done
note: to find the oldest directory use "$dir"/*/
above (thanks Cyrus) and -type d
below with the find
command.
In bash if you need a recursive solution, then you can rewrite it as a while loop with process substitution using find
unset -v oldest
while IFS= read -r i; do
[ "$i" -ot "$oldest" -o "$oldest" = "" ] && oldest="$i"
done < <(find "$dir" -type f)