Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using the data dynamically in paths with the crontab - can you do it? 2

Status
Not open for further replies.

stla

IS-IT--Management
Mar 12, 2003
190
DE
(Elementary user)

Good day,

I would like to make directories available for download for 24hours and then move them to another location, inside a subfolder folder with 'yesterday's' date. I would like to use a script and the crontab:

For example - here is the path to my download area:
/raid/downloads/ABC
/raid/downloads/TUV
/raid/downloads/XYZ


Here is the target path for the moved directories:
/raid/legacy_data/DATE FROM YESTERDAY/ABC
/raid/legacy_data/DATE FROM YESTERDAY/TUV
/raid/legacy_data/DATE FROM YESTERDAY/XYZ

Here is my unfinished script:
find . -maxdepth 1 -mtime 1 -name '*' -exec mv {} /raid/legacy_data/DATE FROM YESTERDAY/ \;

Can anyone help me solve the 'date' part of my script?

Best regards
 
What we do is run another job in cron.daily which captures the current date to a file. The 'date' file will remain unchanged until the same time tomorrow when the cron job will replace it. Our archive script uses that date before it is replaced.
 
Or, since you're using Linux, you can use the GNU extension to the date command: date --date yesterday.

Annihilannic.
 
(Elementary user)

Thank you for your help; yes I'm using Linux 5.

Your suggestion sounds ideal but I'm at a loss as to how to integrate it into my script; could you please help?

find . -maxdepth 1 -mtime 1 -name '*' -exec mv {} /raid/legacy_data/date --date yesterday/ \;
 
you need backticks surrounding the date command:

`date --date yesterday`
 
By the way, you may find this expression more suitable to your needs:
`date --date yesterday +%y%m%d`
 
Hello,

I think I can (almost) answer my own question!

I have modified my script as below however its is placing the contents of all the directories it finds into a folder with yesterday's date; is there a way of moving the found directories intact to a folder with yesterday's date?

----------------------------------------------------
cd /raid/downloads/

YESTERDAY=$(date -d "yesterday" '+%Y-%m-%d')

find . -maxdepth 1 -mtime 1 -name '*' -exec mv {} /raid/legacy_data/$YESTERDAY/ \;
----------------------------------------------------

Best regards
 
Add a -type d perhaps? I took out the -name '*' since it's superfluous. I also added -mindepth 1 so that it doesn't include "." in the list, otherwise you will probably get an error should you try to mv it.

Code:
cd /raid/downloads/

YESTERDAY=$(date -d "yesterday" '+%Y-%m-%d')

find . -maxdepth 1 -mindepth 1 -mtime 1 -type d -exec mv {} /raid/legacy_data/$YESTERDAY/ \;

Annihilannic.
 
Thank you so much for all your help Annihilannic; your solution works pefectly for my needs!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top