Solved

Batch script to sort files

Posted on 2010-11-09
6
623 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:StevenHook
  • 3
  • 2
6 Comments
 
LVL 7

Expert Comment

by:compaqus
ID: 34092925
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

0
 

Author Comment

by:StevenHook
ID: 34093139
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?
0
 
LVL 7

Accepted Solution

by:
compaqus earned 250 total points
ID: 34093399
since it's going only one depth level, I don't think so, you can test it anyway.
It is only moving files, not deleting them, so... nothing is lost.

Or make another dir, change the paths and test
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 250 total points
ID: 34098584
I'd do it like this:
#!/bin/bash
date=$(date +%Y%m)
olddir=/var/spool/asterisk/monitor-old/$date
mkdir -p $oldddir

find /var/spool/asterisk/monitor -maxdepth 1 -type f -name "*$date*" | xargs -i mv {} $olddir

Open in new window

0
 

Author Comment

by:StevenHook
ID: 34100085
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
0
 
LVL 7

Expert Comment

by:compaqus
ID: 34100949
this gives you last month

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

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
As companies replace their old PBX phone systems with Unified IP Communications, many are finding out that legacy applications such as fax do not work well with VoIP. Fortunately, Cloud Faxing provides a cost-effective alternative that works over an…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question