Solved

Batch script to sort files

Posted on 2010-11-09
6
630 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

717 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