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

find, linux, tail

Solution

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.

Problem

What have I tried so far... Command: ``` find . -type f -ctime -3 | tail -n 5 ``` Result: ``` ./Mobilni Telefoni/01. Box Update/05. DC Unlocker Client/dc-unlocker_client-1.00.0857.exe ./Mobilni Telefoni/01. Box Update/39. Z3X Box/01. Update/01. Samsung Box/SamsungTool_12.4.exe ./Mobilni Telefoni/10. Nokia/1. SRPSKI HRVATSKI JEZICI/BB5/3xx_Series/Asha 300/06.97/rm781_06.97_ppm_d.rar ./GPS Navigacije/01. Garmin/03. Garmin Other/garmin_kgen_15.exe ./GPS Navigacije/01. Garmin/03. Garmin Other/test.txt ``` This output is OK, doesn't work good if I put wider time span. (notice I use -ctime and not -mtime because some uploaded files are modified few years ago) Problem is that files can be uploaded once a month, or once in a year, and I still need to get 10 latest files, regardless of time span. If it can't be done, does `tail` only limit output, or somehow just fetches number specified without huge performance impact on large number of files. By using command from one answer on SO, I was able to get the files but some files were missing... ``` find . -type f -printf '%T@ %p\n' | sort -n | tail -10 | cut -f2- -d" " ``` Result: ``` ./Mobilni Telefoni/11. Samsung/1. FLASH FILES/1. SRPSKI HRVATSKI JEZICI/E/E2330/E2330_OXFKE2.rar ./Mobilni Telefoni/11. Samsung/1. FLASH FILES/1. SRPSKI HRVATSKI JEZICI/E/E2330/FlashTool_E2_R6.zip ./Mobilni Telefoni/11. Samsung/1. FLASH FILES/1. SRPSKI HRVATSKI JEZICI/E/E210/E210_XFGH2.rar ./Mobilni Telefoni/05. iPhone/07. iFaith/iFaith-v1.4.1_windows-final.zip ./Mobilni Telefoni/05. iPhone/09. iPhone Browser/SetupiPhoneBrowser.1.93.exe ./Mobilni Telefoni/05. iPhone/10. iPhone_PC_Suite/iPhone_PC_Suite_Eng_v0.2.1.rar ./Mobilni Telefoni/05. iPhone/10. iPhone_PC_Suite/iPhone_PC_Suite_Ok.rar ./test ./Mobilni Telefoni/11. Samsung/1. FLASH FILES/1. SRPSKI HRVATSKI JEZICI/E/E2152/E2152_XXJH4_OXFJI2.zip.filepart ./GPS Navigacije/01. Garmin/03. Garmin Other/test.txt ``` File `garmin_kgen_15.exe` is missing because it was created in 2008, but it was uploaded in last 24 hours.

Original source

Related problems