Find the answer to your Linux question:
Results 1 to 3 of 3
I have an asterisk server setup for a call center and all the calls are recorded as a .wav file. The extension. The number called. The date. and the time. ...
  1. #1
    Just Joined!
    Join Date
    Dec 2007
    Posts
    1

    Help with script to move files.

    I have an asterisk server setup for a call center and all the calls are recorded as a .wav file. The extension. The number called. The date. and the time. wav files are huge and im going to compress them to mp3 using lame. I have a script to do that. But I also want to move the file after it is compressed into its own folder according to the year, month, and day.

    150_9xxxxxxxx1527_20071119-083522.wav compress to
    150_9xxxxxxxx1527_20071119-083522.mp3

    This is my script so far but im rather a newbie at bash scripting so I haven't gotten it quite right. I cant get the year, month and day to "cut" and make directories based on that string. And the check to see if the file type exists doesn't work quite right either. I get a too many parameters error.

    Could someone help please.
    Thanks.


    #!/bin/bash
    # Title: Wav2mp3
    # Discription: This script will convert the wav file to mp3 and move it based # on the filename date.
    # Purpose: To be used to move the recorded calls on asterisk spool directory # to a sturctured system based on date.
    # Date Last Modified: December 19, 2007

    # variables
    PATH_NAME=/u03/asterisk/monitor/spool
    date=00000000
    year=0000
    month=00
    day=00


    # convert every wav file in the current directory to an MP3 File
    # and remove the old wav file.


    #check if file type .wav exists and compress the file.
    if [ -e *.wav ]
    then
    for wavfile in *.wav
    do
    lame -h -b 24 "$wavfile" "${wavefile%.wav}.mp3"
    rm ${wavfile}
    done
    fi


    check if file type .mp3 exists
    if [ -e *.mp3 ]
    then
    for mp3file in *.mp3
    do

    #Everthing up to and including first "_" stripped out
    temp1=${mp3file#*_*}

    #Everthing up to and including second "_" stripped out
    temp2=${temp1#*_*}

    #Everything from the last "-" stripped out. Leaving only the date.
    date=${temp2:%*-*}

    #Get the year
    year=$($date | cut -c 1-4)

    #Get the month
    month=$($date | cut -c 5-6)

    #Get the day
    day=$($date | cut -c 7-


    cd ${PATH_NAME}


    # if the year directory doesn't exist then create it.
    if [ -d $year ];then
    cd ${year}
    else
    mkdir ${year}
    cd ${year}
    fi


    # if the month directory inside the year doesn't exist then create it.
    if [ -d $month ]; then
    cd ${month}
    else
    mkdir ${month}
    cd ${month}
    fi


    #if the day directory inside the year & month doesn't exist then create it.
    if [ -d $day ]; then
    cd ${day}
    else
    mkdir ${day}
    cd ${day}
    fi


    # move back to the spool directory
    cd ${PATH_NAME}

    # move the mp3 file into the newly created directory structure.
    mv -f ${mp3file} ${PATH_NAME}+${year}+${month}

    done

    fi

  2. #2
    Just Joined!
    Join Date
    Nov 2004
    Posts
    18
    This should do it:
    Code:
    #!/bin/sh
    PATH_NAME="/u03/asterisk/monitor/spool"
    
    for wavfile in *.wav: do
        date=$(echo ${wavfile##*_} |  awk -F'-' '{print $1}')
        newdir=$(echo "$PATH_NAME/${date:0:4}/${date:5:2}/${date:6}")
        mkdir -p $newdir
        lame -h -b 24 "$wavfile" "$newdir/${wavefile%.wav}.mp3"
        rm -f "$wavfile"
    done
    Last edited by patch_linams; 12-20-2007 at 07:41 PM.

  3. #3
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Or with sed:

    Code:
    day=$(echo mp3file|sed 's/.*\(..\)-.*/\1/')
    
    month=$(echo mp3file|sed 's/.*\(..\)..-.*/\1/')
    
    year=$(echo mp3file|sed 's/.*_\(....\).*/\1/')
    Regards

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...