Listing files modified on a certain day

You can retrieve a list of files that were last modified yesterday in a wide variety of ways with scripting languages. The “find” command would probably be the simplest method in Linux by using the mtime option like so:

#find -maxdepth 1 -mtime +1 -mtime -2

The find command is disabled however in Cloud Sites for performance reasons, so an alternative must be found. Here is one example of a line that can be run in a .sh file that can also give a list of files modified yesterday.

ls -l --time-style='+%F' /mnt/stor15-wc2-dfw1/123456/backups/ | awk '{print $6,$7}' | grep "`date --date='1 day ago' '+%F'`" | awk '{print $2}'

The directory path must be replaced by your full server side path before using this command.

This command can be run through a cron job within a .sh file in the same manner that unzip commands are run through a .sh file in this article:
http://cloudsites.rackspacecloud.com/index.php/Use_cron_to_extract_and_compress_%28zip_%26_unzip%29

This bash one liner will first list the contents of a directory putting the time in the %F format (consult documentation on the date command), cuts out all columns save for the date and filename, only displays those lines with yesterday’s date within them, and then displays just the filename column. The output of this command will be a list of filenames, one per line, of files within this directory that were modified within the last day which can then be processed using an xargs command or piped into a while loop like so:

ls -l --time-style='+%F' /full/path/backups/ | awk '{print $6,$7}' | grep "`date --date='1 day ago' '+%F'`" | awk '{print $2}' | while read line; do rm /full/path/$line; done

More information on modifying the day to check for using the date command can be found here:
http://ss64.com/bash/date.html

Share and Enjoy:
  • Print
  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks
No Comments Posted in Uncategorized

Leave a Reply

*

Using Gravatars in the comments - get your own and be recognized!

XHTML: These are some of the tags you can use: <a href=""> <b> <blockquote> <code> <em> <i> <strike> <strong>