1

In order to rename music samples with .wav and .flac extensions I use this line:

for f in * ; do rename 's/[^a-zA-Z0-9:0:4]//g' "$f" ;done

But what happen is, when the name of the file is like this:

name * of ( the f?ile.wav 

it becomes:

nameofthefilewav

it shold be:

nameofthefile.wav

What do I need to get the extension dot on his place?

Crojav
  • 19
  • 3
  • Just replacing the extension? Maybe `rename 's/.wav$/.flac/' *.wav` ? – Reut Sharabani Jul 30 '18 at 10:17
  • You are trying to replace anything that is not inside `[..]` by doing `[^a-zA-Z0-9:0:4]`, but you've missed the `.` – Inian Jul 30 '18 at 10:22
  • Why are the there `[` and `]` part of filenames? Was it suggest an example? If not remove that – Inian Jul 30 '18 at 10:25
  • @ReutSharabani for f in * ; do rename 's/.wav$/.flac/' *.wav "$f" ;done that by that I have to take care of what the extension is - cq otherwise it will change all the wav into flac - it works but it not what I want. – Crojav Jul 30 '18 at 11:48

1 Answers1

-1

You can use basename and parameter expansion to get the filename and its extension.

filename=$(basename -- "$fullfile")
extension="${filename##*.}"
filename="${filename%.*}"

Answer taken from here

EDIT:

bash-4.1$ ls
1.mp3  2.mp3  3.mp3
bash-4.1$ for f in `ls`; do
filename=$(basename -- "$f")
extension="${filename##*.}"
filename="${filename%.*}"
echo "FROM: $filename.$extension TO: $filename.wav"
done
FROM: 1.mp3 TO: 1.wav
FROM: 2.mp3 TO: 2.wav
FROM: 3.mp3 TO: 3.wav
ItayBenHaim
  • 163
  • 7
  • I do not understand how to use this? I tried this with a echo on $filename but it don't print anything – Crojav Jul 30 '18 at 11:53
  • - for f in * ; do rename 's/ //' *.wav "$f" ;done - this is the same as what you have. It only removes the spaces. I would like to remove also unwanted characters like * ; ( & ) e.t.c - I don't wont change extension. – Crojav Jul 30 '18 at 17:01
  • this changes the extension, but in my case there is no extension left by that the dot before the extension is removed. – Crojav Jul 30 '18 at 18:55
  • Do the rename only on $filename and then concatenate it back with "\.$extension" – ItayBenHaim Jul 31 '18 at 11:07
  • Yes I think also that is possible solution, thanks for thinking with me - aldo I think it must be easy to replace a dot for the extension - i know it in Python. I will vote your answer, not really what I wont, but so far there is nothing else. – Crojav Aug 01 '18 at 12:24