0

I am trying to find the oldest directory inside a directory. Currently my bash script is returning a directory but it isn't the oldest one.

Code -

DIR=$(ls -t1 | tail -n 1)
testDIR=$(find /Users/BigO/Downloads/shell -type d -maxdepth 1| sort | tail -n 1)
echo $DIR
echo $testDIR

outout

5_6_project_files_test
/Users/BigO/Downloads/shell/utdse19_ff-UI_3-4-19

DIR prints out expected directory.
testDIR wrong directory found, expected 5_6_project_files_test

Directory- enter image description here

Any idea on what im doing wrong? any help is appreciated. Thanks.

BigO
  • 334
  • 1
  • 3
  • 16
  • Why on Earth do you expect `find | sort` to produce results in age order? If you take out the `sort` it might; but the proper solution is to `stat` the file and sort by age. The precise syntax depends on your platform; on Linux you can also use the `-printf` predicate of `find` – tripleee Mar 30 '23 at 05:35

0 Answers0