LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-28-2004, 09:44 AM   #1
facets
LQ Newbie
 
Registered: May 2004
Distribution: Fedora Core 2 / Damnsmalllinux
Posts: 18

Rep: Reputation: 0
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

Last edited by facets; 05-28-2004 at 10:11 AM.
 
Old 05-28-2004, 10:58 AM   #2
mfeat
Member
 
Registered: Aug 2003
Location: Akron, OH
Distribution: Fedora Core 3
Posts: 185

Rep: Reputation: 30
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
 
Old 05-28-2004, 12:37 PM   #3
facets
LQ Newbie
 
Registered: May 2004
Distribution: Fedora Core 2 / Damnsmalllinux
Posts: 18

Original Poster
Rep: Reputation: 0
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
 
Old 05-28-2004, 12:51 PM   #4
Ma3oiS
LQ Newbie
 
Registered: May 2004
Posts: 12

Rep: Reputation: 0
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
 
Old 05-28-2004, 01:04 PM   #5
facets
LQ Newbie
 
Registered: May 2004
Distribution: Fedora Core 2 / Damnsmalllinux
Posts: 18

Original Poster
Rep: Reputation: 0
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
 
Old 05-28-2004, 01:09 PM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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

Last edited by Hko; 05-28-2004 at 01:14 PM.
 
Old 05-28-2004, 01:20 PM   #7
facets
LQ Newbie
 
Registered: May 2004
Distribution: Fedora Core 2 / Damnsmalllinux
Posts: 18

Original Poster
Rep: Reputation: 0
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cannot using FTP move command to move files adrianmak Linux - Networking 4 04-21-2009 12:01 PM
CHMOD directories.sub-directories.files zerojosh Linux - Software 2 11-19-2005 03:22 PM
the best way to move files around..... thegreggwong Linux - Software 5 08-28-2004 05:20 PM
partitioning a new drive, which directories can I move ? xucaen Linux - General 2 02-25-2004 09:03 PM
Can I move Untarred directories? Nigel_Tufnel Linux - General 5 08-08-2002 08:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration