HerrCrazi
HerrCrazi

Reputation: 142

Sort files numerically by name, then by parent directory in BASH

So, I'm facing a pretty edgy problem. I have a bunch of directories, named by numbers (0, 1, 2, and so on...), containing files also named by numbers.

When I use find . -type f | sort -nr, I got my list of files sorted, but by parent directory first, and then by file name.

Like this :

/0/0.png
/0/1.png
/0/2.png
/1/0.png
/1/1.png
/1/2.png

Instead of this (what I would like) :

/0/0.png
/1/0.png
/0/1.png
/1/1.png
/0/2.png
/1/2.png

How could I get the last behavior to happen ?

Thanks by advance !

Upvotes: 1

Views: 220

Answers (2)

choroba
choroba

Reputation: 241828

Use / as the separator, first sort by the filename, then by the directory name:

find . -type f | sort -t/ -k3,3n -k2,2n

Upvotes: 2

Derviş Kayımbaşıoğlu
Derviş Kayımbaşıoğlu

Reputation: 30565

Please try this

find . -type f | sort -n -t "/" -k3

Upvotes: 1

Related Questions