LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 08-09-2012, 02:54 PM   #1
charly78
Member
 
Registered: Aug 2012
Location: Toronto,Canada
Posts: 73

Rep: Reputation: Disabled
turn a command to compress wav into a script


I have my recording of calls on in a voip phone deamon I run called asterisk. and it use to generate wav files in this one directory

/var/spool/asterisk/monitor/

after upgrading to needed new features and security. now it creates sub folders making it harder to run a command I had using sox to compress my wav's to ogg.

/var/spool/asterisk/monitor/2012/07/29

I figure someone will know or at least point me toward a script I can run at the main directory that will search through compress all wav files to ogg and remove the wav file.

here is my command

for x in *.wav ; do sox $x `echo $x|awk -F . '{print $1 ".ogg"}'`; done

then i just do a rm *.wav

life was simple then

anyone have a example of a shell script that will scour through sub directories. i don't even know where to start.
 
Old 08-09-2012, 10:13 PM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

Quote:
Originally Posted by charly78 View Post
Code:
for x in *.wav ; do sox $x `echo $x|awk -F . '{print $1 ".ogg"}'`; done
The first change I would make is
Code:
for x in *.wav ; do sox $x ${x%.*}.ogg && rm $x; done
This will do the conversion and remove the wav file if the conversion exited without error. That should work with your old setup.

Now with the nested directories you can do
Code:
for x in /var/spool/asterisk/monitor/*/*/*/*.wav ; do sox $x ${x%.*}.ogg && rm $x; done
HTH,

Evo2.
 
Old 08-10-2012, 04:09 PM   #3
charly78
Member
 
Registered: Aug 2012
Location: Toronto,Canada
Posts: 73

Original Poster
Rep: Reputation: Disabled
That is working from what I can see very well. wow

way more effective thank you !
 
Old 08-13-2012, 01:06 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Code:
for x in *.wav ; do sox $x ${x%.*}.ogg && rm $x; done
Don't forget to quote the variables, to keep filenames with spaces (and other reserved characters) from breaking the command.

And to set this up as a stand-alone script:

Code:
#!/bin/bash
# A simple set of loops that convert wav's to ogg files
# The input arguments are directories to work in:

for dir ; do

	(
	cd "$dir"
	echo "processing directory $PWD"

	for x in *.wav ; do

		sox "$x" "${x%.*}.ogg" && rm "$x"

	done
	)

done

exit 0
Note how I put the inner block inside (...) brackets. This runs them inside a subshell, and so you don't need to cd back out or anything at the end. When the subshell exits, the environment automatically reverts back to that of the parent.

bash from version 4.0 also has a new "globstar" recursive pattern matching option. To run the script on a directory and all its subdirectories, you can do this:

Code:
shopt -s globstar	#needs to be enabled first

./scriptname /var/spool/asterisk/monitor/**/
Alternately you could modify the script like this:

Code:
#!/bin/bash
# A simple set of loops that convert wav's to ogg
# files recursively from a given top directory.
# The input parameter is the directory to start from:
# This script requires bash 4.0+

shopt -s globstar

for dir in "$1/"**/ ; do

	(
	cd "$dir"
	echo "processing directory $PWD"

	for x in *.wav ; do

		sox "$x" "${x%.*}.ogg" && rm "$x"

	done
	)

done

exit 0
 
  


Reply



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
Script to compress pnmanojshenoy Linux - Newbie 2 06-07-2010 01:26 PM
compress command does not work sky rulz Linux - Newbie 2 08-18-2009 12:03 AM
edit wav files via command line in a script legolin Linux - Software 4 12-21-2005 10:09 AM
lp command with compress mode zita Linux - General 2 10-06-2005 12:36 PM
How to turn off Xserver in the gui and turn it off in the command line geminiviper Linux - Newbie 8 08-20-2004 08:05 AM

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

All times are GMT -5. The time now is 11:11 AM.

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