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 :