Wednesday, February 27, 2008

Shell script to find last created file in a directory

I'm quite new to shell scripting and with my introduction to pipes and the beginners ls and tail commands I was able to come up with quite a simple shell script to find the last modified file in a directory.

The idea is very simple, its built on top of three facts :
1) ls -t -r command which sorts the file in a directory by creation date in reverse order,
2) tail -n 1 command which outputs the last line of the input passed to it, and
3) '' , the pipe which redirects the output of one process to the input of another.

So finally I came up with this simple script :)

ls -t -r | tail -n 1

--

7 comments :

[hide] Shams said...

My friend and colleague, Imran, has another version to do the same thing
ls -t | awk '{print $1}'

He says this is a more appropriate way of doing it

on February 27, 2008 at 9:37 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @6507462224210508976.0
  • @Shams
is the first line of your comment.
Click here to enter your reply
[hide] Zeinobia said...

Testing the reply feature

on December 13, 2010 at 1:27 AM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @9121055074996378.0
  • @Zeinobia
is the first line of your comment.
Click here to enter your reply
[hide] Anonymous said...

Me an my mom already have a contract with at&t if i buy the iphone do i have to buy the 2 year contract? If i do how does it work if i already have one.
[url=http://forexrobot-review.info]best forex software[/url] [url=http://gayz.pl/forum/viewtopic.php?p=4675#4675]unlock iphone[/url]

on January 3, 2010 at 11:55 AM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @8443358232719364040.0
  • @Anonymous
is the first line of your comment.
Click here to enter your reply
[hide] Anonymous said...

Hello I'd love to congratulate you for such a terrific made site!
Just thought this would be a nice way to introduce myself!

Sincerely,
Edwyn Sammy
if you're ever bored check out my site!
[url=http://www.partyopedia.com/articles/casino-party-supplies.html]casino shower Party Supplies[/url].

on January 9, 2010 at 10:03 AM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @6197383447355125761.0
  • @Anonymous
is the first line of your comment.
Click here to enter your reply
[hide] Zeinobia said...

Testing again

on December 13, 2010 at 1:25 AM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @7135528735448143631.0
  • @Zeinobia
is the first line of your comment.
Click here to enter your reply
[hide] Zeinobia said...

@Zeinobia testing again

on December 13, 2010 at 1:26 AM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @3542047760171428503.0
  • @Zeinobia
is the first line of your comment.
Click here to enter your reply
[hide] eMPee584 said...

That's bogus. awk '{print $1}' prints the first field of each line, head -1 selects the first line of a stream. Result may vary depending of what ls is aliased to.

on November 30, 2011 at 5:40 AM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @1784264993102245319.0
  • @eMPee584
is the first line of your comment.
Click here to enter your reply