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.)