ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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,
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.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.