Solved

Batch script to sort files

Posted on 2010-11-09
6
607 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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 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…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…

932 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

12 Experts available now in Live!

Get 1:1 Help Now