Have You Found Your File?

When I need to find a file it’s normally when I am in Vim and I’ll happly use Ctrl P. However, there are tasks outside of Vim that require my attention daily and I have found the following tips and tricks to listing and finding files very usefull.

Of course, everyone knows what ls is, for those who don’t we’ll take a moment to weep for their souls… ls has more power to it than you may know. For instance, if you use ls -t it provides a list of files ordered by most recently modified.

$ ls -t | head

The real powerhouse of file finding is aptly named find. If you do any kind of command line work this is a command you should know how to harness the power of. My favorite of these is the -name option, which matches files like bash would.

$ find -name '*.markdown' | sort
# ...

Want a ls version of that find? No problem dude

$ find -name '*.markdown' -ls | sort -k 11
3805915 4 -rw-r--r-- 1 bfalk bfalk 1303 Mar 7 2014 ./CHANGELOG.markdown
3805918 4 -rw-r--r-- 1 bfalk bfalk 2901 Mar 7 2014 ./README.markdown
4856703 4 -rw-r--r-- 1 bfalk bfalk 388 Mar 7 2014 ./source/_posts/2013-11-20-let-me-start-this-up.markdown
4856704 4 -rw-r--r-- 1 bfalk bfalk 1203 Mar 7 2014 ./source/_posts/2013-11-21-las-vegas-ruby-users-group.markdown
4856705 4 -rw-r--r-- 1 bfalk bfalk 1232 Mar 7 2014 ./source/_posts/2013-11-22-image-scraping-my-wedding-photos.markdown
# ...

sort is also a powerful ally for just about everything command, in this case the sort -k 11 is sorting the 11th column, which is the default of ls, but you could very easily sort by any other column.

Other noteble features of find

  • -mtime – Find files edited before or after a certain time, for instance -mtime -2 would list all files edited in the last two days while -mtime +2 would list all files that haven’t been edited in the last two days
  • -newer <file> finds all files more recently modified than <file>
  • -exec run a command against each file, for instance find -name '*.rb' -exec flog {} \; will run flog for every ruby file

There are a ton more uses for find but these are the ones I use on a daily basis. I suggest trying them out and crack open the man pages to discover new features you might find useful!