Iterate through list of filenames in order they were created in bash

前端 未结 8 1754
深忆病人
深忆病人 2020-12-05 19:27

Parsing output of ls to iterate through list of files is bad. So how should I go about iterating through list of files in order by which they were first created

8条回答
  •  被撕碎了的回忆
    2020-12-05 19:43

    You can try using use stat command piped with sort:

    stat -c '%Y %n' * | sort -t ' ' -nk1 | cut -d ' ' -f2-
    

    Update: To deal with filename with newlines we can use %N format in stat andInstead of cut we can use awk like this:

    LANG=C stat -c '%Y^A%N' *| sort -t '^A' -nk1| awk -F '^A' '{print substr($2,2,length($2)-2)}'
    
    1. Use of LANG=C is needed to make sure stat uses single quotes only in quoting file names.
    2. ^A is conrtrol-A character typed using ControlVA keys together.

提交回复
热议问题