Get most recent file in a directory on Linux

前端 未结 21 2214
余生分开走
余生分开走 2020-11-27 09:24

Looking for a command that will return the single most recent file in a directory.

Not seeing a limit parameter to ls...

相关标签:
21条回答
  • 2020-11-27 09:46

    If you want to get the most recent changed file also including any subdirectories you can do it with this little oneliner:

    find . -type f -exec stat -c '%Y %n' {} \; | sort -nr | awk -v var="1" 'NR==1,NR==var {print $0}' | while read t f; do d=$(date -d @$t "+%b %d %T %Y"); echo "$d -- $f"; done
    

    If you want to do the same not for changed files, but for accessed files you simple have to change the

    %Y parameter from the stat command to %X. And your command for most recent accessed files looks like this:

    find . -type f -exec stat -c '%X %n' {} \; | sort -nr | awk -v var="1" 'NR==1,NR==var {print $0}' | while read t f; do d=$(date -d @$t "+%b %d %T %Y"); echo "$d -- $f"; done
    

    For both commands you also can change the var="1" parameter if you want to list more than just one file.

    0 讨论(0)
  • 2020-11-27 09:47

    All those ls/tail solutions work perfectly fine for files in a directory - ignoring subdirectories.

    In order to include all files in your search (recursively), find can be used. gioele suggested sorting the formatted find output. But be careful with whitespaces (his suggestion doesn't work with whitespaces).

    This should work with all file names:

    find $DIR -type f -printf "%T@ %p\n" | sort -n | sed -r 's/^[0-9.]+\s+//' | tail -n 1 | xargs -I{} ls -l "{}"
    

    This sorts by mtime, see man find:

    %Ak    File's  last  access  time in the format specified by k, which is either `@' or a directive for the C `strftime' function.  The possible values for k are listed below; some of them might not be available on all systems, due to differences in `strftime' between systems.
           @      seconds since Jan. 1, 1970, 00:00 GMT, with fractional part.
    %Ck    File's last status change time in the format specified by k, which is the same as for %A.
    %Tk    File's last modification time in the format specified by k, which is the same as for %A.
    

    So just replace %T with %C to sort by ctime.

    0 讨论(0)
  • 2020-11-27 09:48

    Recursively:

    find $1 -type f -exec stat --format '%Y :%y %n' "{}" \; | sort -nr | cut -d: -f2- | head
    
    0 讨论(0)
  • 2020-11-27 09:50

    using R recursive option .. you may consider this as enhancement for good answers here

    ls -arRtlh | tail -50
    
    0 讨论(0)
  • 2020-11-27 09:51

    I needed to do it too, and I found these commands. these work for me:

    If you want last file by its date of creation in folder(access time) :

    ls -Aru | tail -n 1  
    

    And if you want last file that has changes in its content (modify time) :

    ls -Art | tail -n 1  
    
    0 讨论(0)
  • 2020-11-27 09:52

    Presuming you don't care about hidden files that start with a .

    ls -rt | tail -n 1
    

    Otherwise

    ls -Art | tail -n 1
    
    0 讨论(0)
提交回复
热议问题