Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

We have an IP PBX that is running Asterisk and is set by the manufacturer to delete call recordings after a certain time period. I am backing these up to an Ubuntu server using rsync so that we are able to store them indefinitely.

The issue that I have is that all of the recordings are all placed into a single directory. Conveniently the files are named in the same structure:

What I would like to do is set up a directory structure that would be Year/Month/Day (with the month being correctly ordered), then move each of the files into the corresponding order.

Personal note: I am confident at working with Ubuntu at the command line but don't have experience of running or automating scripts so advice is greatly welcomed. (Happy to learn!) Thanks in advance.

The script that was supplied earlier is working great. I have come upto a further slight issue.

I am using a crontab to rsync IPPBX to Ubuntu Server, then another to file these recordings using the script below. Each evening the rsync is re-downloading all the recordings as it thinks that the destination folder is empty.

I have two questions: Can the rsync be incorporated into the script below so that one a single file is used? Can the script understand what has already been downloaded so that it is only downloading new files?

share|improve this question

migrated from Jun 30 '12 at 10:36

This question came from our discussion, support, and feature requests site for Ubuntu users and developers.

That's actually fairly simple with bash. Just iterate the files, extract the year, month and day, then move it accordingly.

#!/usr/bin/env bash

# iterate all the wav-files that has at least 3 dots in addition to the extension
for file in *.*.*.*.wav; do

    # In the case of no files matching the glob, file will contain the glob itself
    # which will make the mkdir later on create './*/*/*'. Avoid that by testing
    # if file contains an existing file.
    [[ -e $file ]] || continue

    # split out year month and day from the filename
    IFS=. read -r year month day _ <<< "$file"

    # make sure the directories exist, then move it
    mkdir -p "./$year/$month/$day" &&
    mv "./$file" "./$year/$month/$day"
share|improve this answer
Geriha,Thanks for the script. How do I specify which folder to run this script against? Or do I run it in the original folder that the files are in? – BatchHeader Jul 2 '12 at 12:09
@BatchHeader, same dir as the files, yes. Save the script somewhere in PATH, e.g. ~/bin/filerecs or /usr/local/bin/filerecs and make it executable chmod +x /usr/local/bin/filerecs. To use it, change to the right directory cd /path/to/recordings, then run filerecs. – geirha Jul 2 '12 at 21:38

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.