Solved

Batch script to sort files

Posted on 2010-11-09
6
601 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
this gives you last month

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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I recently purchased a Bluetooth headset called the Music Jogger (model BSH10). The control buttons on it look like this: One of my goals is to use it as the microphone and speakers for Skype calls. In that respect, it works well. However, I …
If your business is like most, chances are you still need to maintain a fax infrastructure for your staff. It’s hard to believe that a communication technology that was thriving in the mid-80s could still be an essential part of your team’s modern I…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now