How to sort files in directory numerically?

Trying to sort a bunch of files numerically but can't seem to get the command just right. This is in a IBM AIX machine.

I have a directory that has...

backup.bk1
backup.bk100
backup.bk2
backup.bk200
backup.bk3
backup.bk300

There are a lot more files but this is shortened for the example.

If I simply do a ls -l on the directory I get..

backup.bk1
backup.bk100
backup.bk2
backup.bk200
backup.bk3
backup.bk300

I need the list to include the full directory path (in this case the files are stored in /usr1/temp ) and the file name like this..

/usr1/temp/backup.bk1
/usr1/temp/backup.bk2
/usr1/temp/backup.bk3
/usr1/temp/backup.bk100
/usr1/temp/backup.bk200
/usr1/temp/backup.bk300

Anyone able to help me out?

Are there any attempts or ideas from your side, some code you gave a shot ?

Regards
Peasant.

Yes I tried using this..

ls -l /usr1/tmp/backup.bk* | sort -tk -k 3.1b,3n

But it does not give the desired results, I am reading through the sort manual but it is very confusing.

I find your approach working. Could be somewhat simplified, like

ls  back* | sort -tk -k3n
backup.bk1
backup.bk2
backup.bk3
backup.bk100
backup.bk200
backup.bk300

With your -l option to ls , which outputs user and group of the file - is it possible those files' have a k in either of the two fields?

t(){
for file in $(ls *.bk*)
do
    n=$(echo $file |cut -dk -f3)
    echo $n $file
done
} 
l()
{
while read a b
do
    echo $b
done
}
t |sort -n|l
1 Like

Using the script you provided works, thank you :slight_smile: