Bash script to find and display oldest file

前端 未结 4 1256
梦如初夏
梦如初夏 2021-01-21 13:26

I\'m trying to write a script that will display the name of oldest file within the directory that the script is executed from.

This is what I have so far:



        
4条回答
  •  被撕碎了的回忆
    2021-01-21 14:14

    You can use this function to find oldest file/directory in any directory:

    oldest() { 
       oldf=
       for f in *; do
          # not a file, ignore
          [[ ! -f "$f" ]] && continue
          # find oldest entry
          [[ -z "$oldf" ]] && oldf="$f" || [[ "$f" -ot "$oldf" ]] && oldf="$f"
       done
       printf '%s\n' "$oldf"
    }
    

    And call it in any directory as:

    oldest
    

提交回复
热议问题