Avatar of StevenHook
StevenHook
 asked on

Batch script to sort files

Hi,
I have a bash script to sort files into "month" folders (I think I broke it now but it used to work)
The problem is that I always forget to run it. Is there a way that I can auto-fill the numbers for the month and the year and get cron to run it on a monthly or weekly bases?
Thanks
Steven
#!/bin/bash
#  mkdir /var/spool/asterisk/monitor/old-2010-08/
find . -maxdepth 1 -type f -name "*201008*" | while read fname
do
    mv $fname  /var/spool/asterisk/monitor/old-2010-08/
done

Open in new window

Scripting LanguagesShell ScriptingVoice Over IP

Avatar of undefined
Last Comment
compaqus

8/22/2022 - Mon
compaqus

date='eval date +%Y%m'
will get you the date like "201008"
#!/bin/bash
date='eval date +%Y%m'
mkdir /var/spool/asterisk/monitor/$date/
find . -maxdepth 1 -type f -name "*$date*" | while read fname
do
    mv $fname  /var/spool/asterisk/monitor/$date/
done

Open in new window

StevenHook

ASKER
is it going to cause trouble that the destination folder is within the directory that the script is searching for files?
Should I rather move the destination?
ASKER CERTIFIED SOLUTION
compaqus

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Tintin

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
StevenHook

ASKER
Thanks - I went with Tims with some minor alterations - I added a line so I can manually enter a date - or use the automatic one (it would be better if the automatic one could be last month so that this month's files stay where they are supposed to be - I only realised this after running it)
also fixed a typo
Thanks for the help!
Steven
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
compaqus

this gives you last month

date=$(date --date='last month' +%Y%m)