Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse print result of unix find without sort

I want to sort the result of the find as follows:

I am using:

find . -type f -print0 

Result is:

/mnt/sdcard/qu/led/t1/temp42.txt
/mnt/sdcard/qu/led/File.plist
/mnt/sdcard/qu/yellow.plist
/mnt/sdcard/SHA1Keys/SHA1SUMS
/mnt/sdcard/File.xml
/mnt/sdcard/File.plist
/mnt/sdcard/.DS_Store

But i want the result as:

/mnt/sdcard/.DS_Store
/mnt/sdcard/File.plist
/mnt/sdcard/File.xml
/mnt/sdcard/SHA1Keys/SHA1SUMS
/mnt/sdcard/qu/yellow.plist
/mnt/sdcard/qu/led/File.plist
/mnt/sdcard/qu/led/t1/temp42.txt

And if i do:

find . -type f print0 | sort -r 

The order gets all messed up. I saw this solution somewhere:

find . -type f -ls | awk '{print $(NF-3), $(NF-2), $(NF-1), $NF}'

But I can't use it since it prints the results.

Also note I don't have permissions to write to the filesystem, so writing to a file and reversing the lines is not an option.

like image 887
sri Avatar asked Dec 07 '22 04:12

sri


2 Answers

Use tac (cat backwards) to reverse output. You don't need to sort it in reverse order, you just need it reversed.

find . -type f | tac

If you want to keep the -print0 then use:

find . -type f -print0 | tac -rs '\0'
like image 104
John Kugelman Avatar answered Dec 31 '22 15:12

John Kugelman


Alternatively, you could use tail -r:

find . -type f | tail -r
like image 34
bibi Avatar answered Dec 31 '22 17:12

bibi