group image
Tech Gyan
A shoppezine by JavaGuru
17
 
Posts

 

How to find the latest file in a directory

 
Home> Blogs> Tech Gyan> How to find the latest file in a directory
 

There are multiple ways you can find the latest file in a directory. Using a combination of "ls" and "awk" is the easiest one.

$cd DIRECTORY_NAME

$ls -ltr *.* | awk '{ f=$NF }; END{ print f }'

The "ls -ltr" command lists the files in the directory ordered by their time stamp. You can look for specific file names/extensions by replacing *.* with whatever pattern you want to look for. For example: ls -ltr *.txt will list only files having .txt extension.

awk command above will pick the last listed file i.e. the latest file and print its name. You can store the name of the file as below:

latest_file=`ls -ltr *.* | awk '{ f=$NF }; END{ print f }'`

echo $latest_file

 
By: Java  1912 days ago
 
 
 
 
 

Latest from TolMol Editors

  
 
 
 
 
Be the first one to comment on this post
 
 

Your Reply

 
 
  
 
 
 

Blog Fans

 
 

Photos

No photo uploaded for this blog.
 
 

Featured Blogs