Skip to content

Instantly share code, notes, and snippets.

@adamesque
Created October 17, 2011 02:01
Show Gist options
  • Select an option

  • Save adamesque/1291770 to your computer and use it in GitHub Desktop.

Select an option

Save adamesque/1291770 to your computer and use it in GitHub Desktop.
Bash alias to get the last modified file in a directory
# Naive NOT-FOR-PRODUCTION method. Fails for filenames that contain newlines.
alias lastmod="ls -t | head -n 1"
@adamesque
Copy link
Author

ls -t: List the files in the directory sorted by mod date

head -n 1: Print the first 1 line of the input (default is 10)

I'm working through the K&R book, keeping all the examples and exercises in separate files. I use this to make it easier to compile whichever source file I most recently touched: cc lastmod``, because I am extremely lazy.

@tkaemming
Copy link

since you're writing the file names, this probably won't be a big deal, but you may want to read this: http://mywiki.wooledge.org/ParsingLs

@adamesque
Copy link
Author

Ha! You know, I really ought to know better; I fixed that exact same bug in node: nodejs/node-v0.x-archive@7f30f13

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment