Linux find command, find 10 latest files recursively regardless of time span

I was told that this is the solution:

find . -type f -printf "%C@ %p\n" | sort -rn | head -n 10

The key point is the printf %C@ placeholder, which is the -ctime one. I found it by reading man find.

Result:

1336992789.0000000000 ./Mobilni Telefoni/05. iPhone/03. iPhone 4G Firmware/5.1.1/iPhone3,1_5.1.1_9B206_Restore.ipsw.filepart
1336928538.0000000000 ./GPS Navigacije/01. Garmin/03. Garmin Other/test.txt
1336922295.0000000000 ./GPS Navigacije/01. Garmin/03. Garmin Other/garmin_kgen_15.exe
1336868365.0000000000 ./Mobilni Telefoni/11. Samsung/1. FLASH FILES/1. SRPSKI HRVATSKI JEZICI/E/E2152/E2152_XXJH4_OXFJI2.zip.filepart
1336867426.0000000000 ./Mobilni Telefoni/11. Samsung/1. FLASH FILES/1. SRPSKI HRVATSKI JEZICI/E/E210/E210_XFGH2.rar
1336866301.0000000000 ./Mobilni Telefoni/11. Samsung/1. FLASH FILES/1. SRPSKI HRVATSKI JEZICI/E/E2330/FlashTool_E2_R6.zip
1336865921.0000000000 ./Mobilni Telefoni/11. Samsung/1. FLASH FILES/1. SRPSKI HRVATSKI JEZICI/E/E2330/E2330_OXFKE2.rar
1336865409.0000000000 ./Mobilni Telefoni/11. Samsung/1. FLASH FILES/1. SRPSKI HRVATSKI JEZICI/E/E2230/E2230_XXKC1_CDS.zip
1336865398.0000000000 ./Mobilni Telefoni/11. Samsung/1. FLASH FILES/1. SRPSKI HRVATSKI JEZICI/E/E2230/E2230_XXKC1_BIN.zip
1336864949.0000000000 ./Mobilni Telefoni/11. Samsung/1. FLASH FILES/1. SRPSKI HRVATSKI JEZICI/E/E2230/E2230_OXFKC1_CSC.zip

For a very large list of files, sort(1) with pipes might not be optimal for resource usage.

sort(1) could be replaced with perl(1) and buffer the ten highest entries, only. This has been outlined in unix command: how to get top n records for three, here an adoption for ten records.

It replaces the sort(1) and head(1) filters:

find . -type f -printf "%C@ %p\n" | perl -ane '
    BEGIN {@top = ([-1]) x 10}
    if ($F[0] > $top[0][0]) {
        @top = sort {$a->[0] <=> $b->[0]} @top[1..9], [$F[0], $_];
    }
    END {print for reverse map {$_->[1]} @top}
'

The result is identical.

Tags:

Linux

Find

Tail