Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
    
Lots of reading material available at the bash tag info page: unix.stackexchange.com/tags/bash/info -- in particular, the Advanced Bash-Scripting Guide is a great resource. Specific to your question, read section 5.1 about quoting variables. – glenn jackman Aug 23 at 18:59
    
Possibly helpful: superuser.com/questions/670283/… – mr.spuratic Aug 23 at 19:37
    
Thanks for the links! A ton of good info in there! – saltycomms Aug 23 at 19:50
up vote 0 down vote accepted

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 \;
share|improve this answer
    
Thanks!!! Perfect! Makes so much sense when you actually see it! Greatly appreciated! – saltycomms Aug 23 at 19:51
    
Don't forget to accept the answer if you do. And upvote it if it's really that good, of course. – tomas Aug 23 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 at 20:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.