sorted filenames in array

I have an array in bash

array=($(ls -At $base/SrcFiles/MoveIT))

I am using the -t modifier to put the filenames in oldest first.
I ahve tried -t and -tr

But the pop side of teh array always pushes out the newest file.

How do i make sure the array is sorted with oldest file first? or at least pops that way
Evan CutlerVolunteer Chief Information OfficerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


You should use the "-1" (the digit "1") option of "ls" to force one-column output. This might not really be necessary, but nevertheless ...

Also, use the "-r" option to reverse the "-t" sort order and get the oldest file first.

Then this

array=($(ls -Atr1 $base/SrcFiles/MoveIT))

should indeed produce an array with the oldest file in


and the newest in

n=$((${#array[*]} -1))


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
if you put the oldest file first, you're supposed to pop the newest first.

arrays act like stacks, not fifos. pop will retrieve the last element that was pushed


maybe not using an array is simpler

for file in `ls ...` ; do ... ; done

ls ... | while read line ; do ... ; done
Evan CutlerVolunteer Chief Information OfficerAuthor Commented:
Thanks much.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.