2

I have numerous of log files that I have to move from a production directory into an archive directory. I need to move them by creation date. Files from January 2016 go into an archive directory labeled 2016-01, as an example. I currently do this manually by typing:

$ find /creation/directory/filename -daystart -mtime vXX -exec mv "{}" /destination/directory \;

where vXX is the number of days ago to to begin selecting files (ie., +10 for files modified more than 10 days ago). The number of days ago changes based on things like when end of month occurs or when certain directories happen to fill up.

I am trying to write a bash script that will ask the user for input (the number days ago) and then incorporate that into the script that will perform the search and the move. I am having trouble with how to use the date command with the user input as a variable.

I know that $ date -d 'now - vXX days' gets me what I need from a command line, but I can't figure out how to put it into the script.

I've tried a few variations on:

days=0
echo -n "Enter number of days back to begin count > "
read days
echo "Calculated date is "
date -d 'now - ($days) days'

I have very little experience writing anything in bash and I've been doing OK with other scripts (thanks to help from you all, of course!), but variables within variables and utilizing user inputs are really killing me. Any help is greatly appreciated!

(I'm using RHEL 5.)

2

1 Answer 1

0

This script should head you in the right direction.

#!/bin/bash

read -p "Enter number of days back to begin count > " days

echo "Calculated date is "
date -d 'now - '"$days"' days'

find /creation/directory/filename -daystart -mtime +"$days" -exec mv "{}" /destination/directory \;
3
  • Thanks!!! Perfect! Makes so much sense when you actually see it! Greatly appreciated!
    – saltycomms
    Aug 23, 2016 at 19:51
  • Don't forget to accept the answer if you do. And upvote it if it's really that good, of course.
    – user147505
    Aug 23, 2016 at 19:55
  • I upvoted, but because I am new it doesn't show it! Sorry! I accepted the answer, though! Thanks again!
    – saltycomms
    Aug 23, 2016 at 20:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .