Unix Commands Galore

Couple of days ago a friend of mine pointed me to commandlinefu.com. It’s strange how addictive a service like that can be! I’ve perused a good chunk of the commands posted on the site, learned quite a few new things, augmented the command aliases on my servers, and posted few of my brainchildren as well as posted suggested fixes to some that I found to be a cool ideas but that didn’t work for me as they were presented (such as the command to copy database from a local MySQL server to a remote MySQL server over SSH).

Unix Shell: find files by a date range

I needed to restore some files from an archive on UNIX, but only the files of a particular date-range were needed.  It took a few moments to find and figure out how I could easily extract files older than a particular date, or files from a particular date-range. This is how:

  1. Create a perimeter file, like so:
    touch -t yyyymmddHHMM marker_date

  2. List files older than the marker_date:
    find . -type f ! -newer marker_date -ls
    Of course, instead of `-ls’ parameter (to list), you can use `-print’ and a pipe to xargs to, for example, delete the selected files, etc.

Likewise, for a range of dates:

  1. Create the perimeter files:
    touch -t yyyymmddHHMM range_start
    touch -t yyyymmddHHMM range_end

  2. List the files between the range dates:
    find . -type f -newer range_start ! -newer range_end -ls

NOTE: For an even easier way to accomplish the same, see comment by Hampus below!