How to find the latest file on Unix or Linux

Please help me out how to identify the latest file in one directory by looking at file's timestamp or datestamp. You can say using system command. Thanks

cd directory
ls -l -rt | head -1

is one way.

You would use tail instead of head for the latest file.

Jim, Lazytech:

Thanks very much for your advice. I mean the latest file by timestamp or datestamp. Using head -l or tail -l only list file in the begining or in the end. I don't care the order of the file listing. I want to know how to get or find the latest file based on date or time. Thanks.

Well, if you man ls, you'll see what the switches mean. The -t switch sorts the listing by 'time' so the output is displayed from newest to oldest. The -r switch reverses it so the newest file is at the end of the listing instead of the beginning. And -l gives you the long listing.

$ ls -lrt
total 178880
-rw-r--r--  1 carlsche  carlsche      2727 13 Jul  2005 badlist
-rwxr-xr-x  1 carlsche  carlsche      1076 13 Jul  2005 scanlist
-rw-r--r--  1 carlsche  carlsche   1428173 22 May 21:25 dirlist.album
-rwxr-xr-x  1 carlsche  carlsche       429 22 May 21:26 dirout
-rw-r--r--  1 carlsche  carlsche   1404661 22 May 21:32 dirlist.song
-rw-r--r--  1 carlsche  carlsche   1404035 22 May 21:33 dirlist.artist
drwxr-xr-x  6 carlsche  carlsche       204 24 Sep 22:18 Previous iTunes Libraries
drwxr-xr-x  3 carlsche  carlsche       102 24 Sep 22:19 Album Artwork
drwxr-xr-x  9 carlsche  carlsche       306 24 Sep 22:40 iTunes Music
-rw-r--r--  1 carlsche  carlsche  42887486 29 Oct 18:32 iTunes Library
-rw-r--r--  1 carlsche  carlsche  44442517 29 Oct 18:32 iTunes Music Library.xml

Carl

Carl:

Thanks for your explanation. It is clear. Question is: if I want to find the latest file (if reversed, it will be last file) to re-name and move to another directory on Linux. What kind of code should I write? Since using ls -lrt |tail -l, it will show a list of the latest file, not the last file. May I write code like this:

ls -lrt |tail -l |mv /directory1/file1 /directory2/file2

Please advise. Thank you so much.

Probably this'd work best:

$ mv `ls -tr | tail -1` /tmp/newfile

Carl

Carl:

Thanks so much. It works.

ls -tr, -t sort by time, -r switch reverses it so the newest file is at the end of the listing. Then pipe to tail -1, it will pick up the latest file ( or say last file in the list) by time. Then it will move to new directory. Because I need re-name the file, so I code like this:

cp `ls -tr | tail -1` /temp/newfile_name

You could also:

$ mv `ls -t | head -1` /temp/newfile_name

The difference is one switch (r - reverse) and using head since without reverse, the newest is put at the top of the list.

Also, you've been saying move however in your final reply you used the copy command. Just checking.

Carl

Carl:

You are right. I put 'move' in my first post. That was not accurate. I should use 'cp'. anyway, thanks a lot for your help.

Try below
> mv `find ./ -maxdepth 1 -atime 10 | tail -1` new_file_name

But this will work for files only in current directory and only if there are files that are accessed in last 10 days(you can change the velue)
[I am a newbie . excuse me if there is anything wrong]

Any way [ $ mv `ls -t | head -1` /temp/newfile_name ] by BOFH is straight forward