# Unix command to get the file which has latest time stamp



## deepak_850 (Jul 14, 2010)

I get the following disply in my system when i type ls -l

total 11450
-rw-r----- 1 deepak beginner 40 Jul 14 07:22 abc.txt
-rw-r----- 1 deepak beginner 1979 Jul 14 07:19 dir.txt
-rw-r----- 1 deepak beginner 0 Jul 14 07:36 example.txt
-rw-r--r-- 1 deepak beginner 60016 Feb 4 14:36 group_group.txt
-rw-r----- 1 deepak beginner 41169 Jul 14 06:31 beginnerfile.txt.gz
-rw-r----- 1 deepak beginner 1216 Jul 14 06:31 newfile.txt.sta
-rw-r----- 1 deepak beginner 3 Jul 14 07:18 newfile.txt
-rw-r----- 1 deepak beginner 5742321 Jul 14 06:22 newfile.txt.gz
-rw-r----- 1 deepak beginner 1389 Jul 14 06:22 newfile.txt.sta
-rw-r----- 1 deepak beginner 34 Jul 14 06:45 test.txt

Please speicfy the unix command such that
From this list i should take the a single file either newfile.txt.gz or newfile.txt whichever has the latest file stamp. As accordance with the above example the result would be newfile.txt.gz (Since it has timestamp Jul 14th which is the latest). Please reply.


----------



## deepak_850 (Jul 14, 2010)

I tried the below command and able to get both the files newfile.txt and newfile.txt.gz

ls -lt | nawk '($9 ~ /^newfile.txt.gz$/ || $9 ~ /^newfile.txt$/) { print $6, $7, $8, $9 }' > test.txt

But i need to still filter my search and make sure i get output of a single file which has the latest timestamp. Please advice.


----------



## lotuseclat79 (Sep 12, 2003)

Hi depak850,

Welcome to TSG!

In the man page for ls there is an option called --time-style=STYLE. The style you want is full-iso used in conjunction with the ls -lt as you already have.

-- Tom


----------



## Squashman (Apr 4, 2003)

sort the directory listing by date. Then take the top one by using the head command.


----------



## deepak_850 (Jul 14, 2010)

Thanks lotuseclat79 and Squashman for the reply. I tried the way in which squash man asked me to do and it worked well.

But as of interest in knowing new things i also tried with respect to what lotuseclat79 specified. I'm not familiar with the option "--time-style=STYLE". ... Can you please brief out for what it is used and how to append it in my coding. Thanks for all the help.

Thanks
Deepak
http://forums.techguy.org/members/83369-lotuseclat79.html


----------



## deepak_850 (Jul 14, 2010)

I forgot mention earlier that i was not able to use --time-style=STYLE, since i did not have any knowledge in that option.


----------



## lotuseclat79 (Sep 12, 2003)

Hi deepak_850,

If you are not able to read the man page for ls, i.e. $ man ls (where the $ is a prompt in a command line window), here is the command you need to issue (just replace ls -lt in your post with the following):

$ ls -lt --time-style=full-iso 
i.e. issue
$ ls -lt --time-style=full-iso | nawk '($9 ~ /^newfile.txt.gz$/ || $9 ~ /^newfile.txt$/) { print $6, $7, $8, $9 }' > test.txt

-- Tom


----------



## deepak_850 (Jul 14, 2010)

Hi,

I already tried the way which u said and i got the following error

Illegal option of ls. (Perhaps its not enabled in mine i believe). 

That's the reason i was asking on my earlier post as to what actually it does. its functionality stuff etc.,

Thanks brother for helping me as of until now. I'm actually a beginner in shell scripting program. Thats the reason i'm taking time in understanding concepts.

Thanks
Deepak


----------



## lotuseclat79 (Sep 12, 2003)

What Unix ditribution/version do you run? You can get the information by issuing the following command:
$ uname -a

-- Tom


----------

