ls and pipe into less

A colleague of mine wrote a little script for me. Actually two lines of shell. I needed to read a directory and get the last file and copy it. I was thinking I would do a readdir in perl, but he just fired up a shell script with ls into tail to grab the last jepg filename.

/usr/bin/ls *.jpg | tail -1

He set a variable with this filename and then just copied it. Pretty sweet.

Note, in this case the filenames worked due to the name format. However, you might have to add -tr (time reverse) to the ls command to get the files in the right order to grab the last one.

Leave a Reply

You must be logged in to post a comment.