0

I am trying to list all the directory latest modified folder first using select, but I am stuck.

Let's say I have:

Folder1
ThisIsAnotherDir
Directory
New Directory
This IS not_Same Directory

When I run following command:

ls -t -d */ 

I get the desired output.

But when I use select:

options=$(ls -t -d */)
select d in $options; do test -n "$d" && break; echo ">>> Wrong Folder Selection!!! Try Again"; done

It lists the folder modified first, but, if I modified New Directory last and run this, it outputs:

1)New 
2)Directory
3)Folder1
4)ThisIsAnotherDir
5)Directory
6)This 
7)IS 
8)not_Same 
9)Directory

I also tried:

select d in "$options[0]"; do test -n "$d" && break; echo ">>> Wrong Folder Selection!!! Try Again"; done

It fails also.

I hope this make sense. Thank you

kbulgrien
  • 830
KpDean
  • 3

1 Answers1

0

Combining the answer in https://unix.stackexchange.com/a/378304/330217 with your ls -t command you can try

IFS= mapfile -t files < <(ls -t -d */)

select d in "${files[@]}" do test -n "$d" && break echo ">>> Wrong Folder Selection!!! Try Again" done

Note that this solution doesn't work if you have a directory with a newline (or maybe other special characters) in its name.

Bodo
  • 6,068
  • 17
  • 27