Tuesday, September 26, 2023
 Popular · Latest · Hot · Upcoming
rated 0 times [  4] [ 0]  / answers: 1 / hits: 12269  / 2 Years ago, thu, february 24, 2022, 7:48:26

I am modifying a simple BASH script to remove numbers and hyphens from the start of my MP3 files. The aim is to rename files such as:

  • 11 Song 1.mp3

  • 1-1-Song 2.mp3

  • 11-1 Song 3.mp3


  • Song 1.mp3

  • Song 2.mp3

  • Song 3.mp3

I have this script which works for all the files in the current directory

$ for f in [0-9]*; do mv "$f" "`echo $f | sed 's/^[0-9]*W*//'`"; done

and have modified it to look for all files inside subfolders:



for f in $(find $dir * -type f) ; do
mv "$f" "`echo $f | sed 's/^[0-9]*W*//'`";

The problem is that the $f value returns the subfolder and the filename and the mv line looks for files beginning with [0-9], therefore any files within a subfolder are not being renamed.

E.g. The file mp3/1-1 Song 1.mp3 begins mp3/, does not start with a numeric so it's not renamed.

Is there a way I can read the directory and file values into separate variables or is there a better way of doing this?


More From » bash


find & sed:
I would ask you to first remove the pipe and sh at the end, and see whether the mv command is getting generated properly, and then you can run the command as such:

find $dir -type f | sed 's|(.*/)[^A-Z]*([A-Z].*)|mv "&" "12"|' | sh

[#34936] Friday, February 25, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.

Total Points: 344
Total Questions: 122
Total Answers: 106

Location: Sint Maarten
Member since Mon, Oct 12, 2020
3 Years ago
lintical questions
Thu, Jan 12, 23, 03:07, 9 Months ago
Mon, Jun 28, 21, 07:01, 2 Years ago
Sat, Aug 7, 21, 04:29, 2 Years ago
Mon, Dec 5, 22, 00:03, 11 Months ago