LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Move certain files into different directories (https://www.linuxquestions.org/questions/programming-9/move-certain-files-into-different-directories-186940/)

facets 05-28-2004 09:44 AM

Move certain files into different directories : BASH or CSH
 
Hi Crew,

I have a situation where I have mulitple files in /audio/projectname/44100/ of various bit depths. ie 44100, 22050, 11020 etc. and I need to move them into their respective folder (which may need to be created) ie,

/audio/projectname/44100/
/audio/projectname/22050/
/audio/projectname/11020/

the filenames look like

radio_file_44100.wav
radio_file_22050.wav
radio_file_11020.wav

As the directy has not already been vreated it's hard to do any matching, also these names sometimes change so radio_file_44100.wav might be radio_file_part1_1.wav and so on.

i don't know enough about sc ripting to do this in any 'quick' way.

any help would be rad!

cheers in advance,

mw

mfeat 05-28-2004 10:58 AM

to move them into folders:

ls *.wav | while read a; do
n=`echo $a | sed s/radio_file_// | sed s/.wav//`
echo mv $a /audio/projectname/$n
done

output:
mv radio_file_11020.wav /audio/projectname/11020
mv radio_file_22050.wav /audio/projectname/22050
mv radio_file_44100.wav /audio/projectname/44100

making the folders is just a minor mod of the above code

facets 05-28-2004 12:37 PM

thanks for the quick reply. the above didn't actually work, it did print the command but it didn't action it. But if I did them individually it would. BUT that's not quite what I'm searching for..

'radio-file' needs to be extracted from the file name beacuse it could be any name, it could read delay_v01_01.wav or phase_v02_06.wav and they need to be mv'd into their correct folders.

am I any clearer?

thanks agian for you help.

cheers, mw

Ma3oiS 05-28-2004 12:51 PM

Hi
Code:

ls -1 *.wav | while read a; do
  directory=`echo "$a" | sed 's/^[^_]*_[^_]*_\(.*\)\.wav$/\1/'`
  mkdir "/audio/projectname/$directory" &> /dev/null
  mv "$a" "/audio/projectname/$directory"
done

If your file is named delay_v02_02.wav it will go into /audio/projectname/02

facets 05-28-2004 01:04 PM

Excellent. Almost there..
here's an actual file name.

radio403_00_delay_v01.0001.wav, radio403_00_delay_v01.0002.wav, radio403_00_delay_v01.0003.wav

radio403_00_phase_v01.0001.wav, radio403_00_phase_v01.0002.wav, radio403_00_phase_v01.0003.wav

so delay1-3 goes into /audio/projectname/delay
and phase 1-3 goes into /audio/projectname/phase

thanks again...

mw

Hko 05-28-2004 01:09 PM

And here's one that will use the actual sample rate like "22050" or "11025", even when the sample rate is not part of file name. It will read the sample rate from the file itself.

So a file called "radio403_00_delay_v01.0001.wav" will be moved to the sub-directory "22050" if it has a sample rate of 22050.

Is this what you are looking for?

Code:

#!/bin/bash

# This is where the files are now. Change it if you need it different.
#
WAVDIR=/audio/projectname/44100

# This is the directory where the subdirectories should
# be made. Change it if you need it different.
#
TOPDIR=/audio/projectname

# Go to directory where the .wav files are.
cd "$WAVDIR"

# Process the files one by one.
ls *.wav | while read FL ; do

        # Get information on the file.
        INFO=$(file "$FL")

        # Check whether it's really a WAV file. (maybe not necessary)
        if echo $INFO | fgrep -i "audio" >/dev/null 2>&1; then

                # Get sample rate from the file-info
                SAMPRATE=$(echo "$INFO" | sed 's/.*,[^0-9]*\(.*\) Hz/\1/')
               
                # Construct destination directory.
                DESTDIR="${TOPDIR}/${SAMPRATE}"

                # Only do somehting if destination dir is different from source dir.
                if [ "Q$WAVDIR" != "Q$DESTDIR" ] ; then

                        # If destination directory does not exist: make it.
                        test -d "$DESTDIR" || mkdir -p "$DESTDIR"
         
                        # Do the move...
                        mv -v "$FL" "$DESTDIR"
                fi
        fi
done
exit 0


facets 05-28-2004 01:20 PM

WOW, thats cool. although the previous one could be applied to images or vid sequences as well.
many thanks, it's been an interesting day ... mw.


All times are GMT -5. The time now is 03:27 PM.