Make a variable = filename without the extension
Hi all,
I am trying to strip the .wav file extension from a file name so that I can pass the result to lame encoder. I started to write a BASH script that looks like this: Code:
for f in /home/user/wavfiles*; do FILE=basename $f .wav; lame $FILE; done I'm hoping someone here at LQ can help. Many thanks in advance Greenie |
You don't need basename in the shell, just use stripping. FILE="${f%.*}".wav % strips shortest matching suffix, %% longest, # shortest matching prefix, ## longest. So ${f##*.} gets you the suffix, ${f%/*} gets the dirname, etc. And since you're not preserving the value of FILE for anything, just do it: do lame "${f%.*}".wav; done.
What you were looking for is useful other times, though, and it's called "command substitution" in the manpage. Do it with $() (<-parens instead of curlies) or backticks. $(basename "$f"). |
"basename" has nothing to do with extensions. It simply returns the last item in a slash-separated path (i.e. get the actual name of the file from a path). Similarly, "dirname" returns the path, removing the filename from the end.
And just in case you didn't know, Linux or its filesystems has no concept of extensions. Some programs do pay attention to them, though (most notably GUI file maangers, to tell the type of file, becasue it's faster to read the file's name than open in and look throught it to figure out its contents). |
Shows how long it's been since I used basename I guess. :-P greengrocer's just lucky it was a side point. Sorry 'bout that.
|
Lame has trouble handling file names with blanks in them. Here is a discussion of the subject, and a slick method that works:
http://ubuntuforums.org/archive/inde...t-1089562.html |
Quote:
|
Quote:
Code:
for i in *.wav ; do I ended up with a screen full of "excess arg" errors. Lame would consider any name up to the first blank space to be one argument. It could be a shell problem. Probably the escape characters get stripped out of the file names. Anyway I tracked down that thread, and their little copy and paste command worked. Since the OP's real problem was wanting to convert wav files to mp3 files, I thought I would pass it on. |
DIdn't I already say that basename has nothing to do with extensions?!? Just scroll up to the 3rd post.
And it's the shell that splits filenames with spaces into separate arguments. To stop it from splitting the contents if a variable into multiple args, surround it in double-quotes (i.e. "$i"). |
Quote:
|
All times are GMT -5. The time now is 07:24 PM. |