2

I have an FTP process that is constantly putting files into a directory. The date created is part of the filename in a format like this:

YYYY-MM-DD-HH-MM-SS-xxxxxxxxxx.wav

I would like to move the files to another directory based on the date the file was created. I can use either the filename or the date stamp, whichever is easier. Only the month and year needs to be considered. I have created directories using the following format:

Jan_2016
Feb_2016

I have been creating directories and moving the files manually but I would like to automate this with a bash script that will create the directory if it does not exist.

What I have been doing so far is manually creating the directories and then running this command:

mv ./2016-02*.wav Feb_2016/

2
  • 2
    Your requirement seems pretty straight forward, using dateand cut commands and using if statement for comparison of directory names and existence. What have you done so far ? Can you show it in the original post by editing it ?
    – MelBurslan
    Feb 12, 2016 at 20:43
  • 2
    I would strongly recommend you rethink your destination directories. Use 2016-01 and 2016-02 -- those have the advantage of being listed in chronological order. Once you accumulate a few year's worth of <strike>crap</strike> treasure, you'll be annoyed that all the January months are grouped together, etc Feb 12, 2016 at 21:12

2 Answers 2

3
### capitalization is important. Space separated.
### Null is a month 0 space filler and has to be there for ease of use later.
MONTHS=(Null Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)

cd /your/ftp/dir                  ### pretty obvious I think
for file in *.wav                 ### we are going to loop for .wav files
do                                ### start of your loop
    ### your file format is YYYY-MM-DD-HH-MM-SS-xxxxxxxxxx.wav so
    ### get the year and month out of filename
    year=$(echo ${file} | cut -d"-" -f1)
    month=$(echo ${file} | cut -d"-" -f2)
    ### create the variable for store directory name
    STOREDIR=${year}_${MONTHS[${month}]}

    if [ -d ${STOREDIR} ]         ### if the directory exists
    then
        mv ${file} ${STOREDIR}    ### move the file
    elif                          ### the directory doesn't exist
        mkdir ${STOREDIR}         ### create it
        mv ${file} ${STOREDIR}    ### then move the file
    fi                            ### close if statement
done                              ### close the for loop.

This should be a good starting point for an inexperienced person. Try writing your script in the light of these instructions and commands. You can ask for help if you get stuck

0
0

This script may help. (please remove the echo to actually mv files):

#!/bin/bash

shopt -s nullglob

month=(Jan Feb Mar May Apr Jun Jul Aug Sep Oct Nov Dec)

for y in 2016; do
    for m in {01..12}; do
        fn="$y-$m"
        dn="${month[10#$m-1]}_$y"
        [[ ! -d $dn ]] && mkdir -p "$dn"
        for file in ./"$fn"*.wav; do
            echo mv "$file" "./$dn/${file#\./}"
        done
    done
done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .