?
Solved

Batch script to sort files

Posted on 2010-11-09
6
Medium Priority
?
634 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 1000 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
Understanding Linux Permissions

Linux for beginners: How to view the permissions associated with files and directories and also how you can change them.

 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 1000 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

771 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