0

I'm trying to insert certain characters into music files to rename them in a certain way within directories that have many files, so doing this manually is very time consuming and I would like to automate this process using the terminal or a bash script.

As this is a music collection there are many files, over many directories.

I have tried a few 'awk', 'rename' & 'sed' terminal options but I seem to be getting really crazy output in the terminal (possibly due to spaces & or punctuation) and I have to Ctrl+z to stop the job or it just errors out. Also tried 'Krename' & 'Easytag' with no luck.

For example, I want to change:

Chicane - Behind the Sun - 01 Overture.mp3

to:

Chicane - Behind the Sun - 01 - Overture.mp3

So at column or position 31 I want to add '- ' to change the filename, but there are 10 more files within this folder and there are many more, other folders (albums) I want to change so the position and the characters I want to add will vary.

My bash/scripting skills are 101, I've managed some backup scripts using compression and other easy tasks but this is beyond me at the moment, any help would be appreciated.

I know this is a duplicate but I have tried many options without success but I'm learning and with your help a solution is at hand, so a Thanks in advance to you.

3
  • The existing name of that file and what you want to change it to are the same. Add a section of the existing name(s) and what you actually want the changes to be to your question. Apr 10 at 20:35
  • Rather than get hooked on character position, is there a pattern that will identify where you want the hyphen? For example, "after the two digit number"
    – roaima
    Apr 10 at 21:02
  • Handling the spaces is not really a problem for many programs. The problem is more that you need to define clear criteria on what to change or where to add. Given the great diversity in the way music files are named, this can be very difficult to do based on filenames alone. There are some music collection management programs out there that can read the ID3 tags, and possibly identify a music file by it's signature/fingerprint, and then use that rename files in a consistent format. Are you able to write programs in Python, Perl, etc?
    – C. M.
    Apr 21 at 10:32
2

With the Perl rename tool,

to insert - (a dash and a space) after the first 30 characters (or bytes, I don't know if it enables locales):

rename 'substr($_, 30, 0) = "- "' *.mp3

to add - after the first occurrence of two digits and a space:

rename 's/(\d\d )/$1- /' *.mp3

Note that there's two different rename commands, the one from util-linux and the Perl one. They do different things, see:

0

Handling filenames with spaces calls for find and xargs. Read man find xargs, and

find . -type f -print0 | xargs -0 --no-run-if-empty someprogram ...

The -print0 tells find to produce a list of filenames, separated by ASCII NUL (0x00) bytes. NUL (and "/") will never occur in filenames.

xargs -0 tells xargs to consume such a zero-seperated list, and pass as many filenames as the system will allow to someprogram, and will invoke someprogram over and over again, until there are no more filenames.

xargs --show-limits </dev/null will show these limits.

1
  • Thanks alot ilkkachu, works great and waltinator I will definately read up on your advice. Thanks guys.
    – rustyshark
    Jun 5 at 1:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.