3

I would like to rename many files, named like this:

cc cc nn - ccccccc ccc cccccccc - nnnnnn.mp3

where "c" means a character and "n" means a number

cc cc nn - ccccccc ccc cccccccc - nnnnnn.mp3
                                  ^^^^^^ these (^) are the numbers I want to change.

I want to numbers to count up like 000001, 000002, ... , 084521

I am using linux, all the usual command line tools available.

Basically I want to replace the positions 35-40 of the file names with a number that it's counting up.

  • 1
    Try to format the question properly (lines starting with 4 spaces will use monospace fonts) and give an example. Have you seen the command rename? – ndemou Sep 21 at 5:53
  • @αғsнιη I'm not sure it's a dupe. Or, rather, it is a dupe but only if you're familiar enough with regular expressions and the various tools to be able to adapt the answers there to this one. It's clearly related, but I wouldn't call it a dupe since you need some expertise to be able to use the answers there for this issue. – terdon Sep 21 at 12:02
  • @tendon emm, well, you are right and agree with you, this is not exact duplicate; I will remember to don't flag next time for new user's question if really it was not answer to their question. thank you : ) – αғsнιη Sep 21 at 13:35
  • There are different "rename" commands on different OSes; see unix.stackexchange.com/a/238862/135943 for details. – Wildcard Sep 23 at 20:58
4

If you have perl-rename (called rename on Debian based systems, including Ubuntu), you can do:

rename -n 's/\d+.mp3$/sprintf("%06d",$c++) . ".mp3"/e' *mp3

Explanation

  • The -n causes rename to only print what it would do, without renaming anything. Remove it once you are sure the command does what you want.
  • s/foo/bar/e : this is the substitution operator; it will replace foo with bar. The e at the end causes any commands inside the right hand side of the operator to be evaluated and replaced by their output.
  • \d+.mp3$ : match the longest stretch of numbers that end with .mp3 and then the end of the file name ($).
  • sprintf("%06d",++$c) . ".mp3" : the sprintf command will print the content of the variable $c, after adding 1 to it, with 6 leading 0s. Since $c is uninitialized the first time the command runs, ++$c will become 1. This is then concatenated with .mp3.

The end result of all of this will be to replace the stretch of numbers before the extension with an incrementing, 0-padded number:

$ rename -n 's/\d+.mp3$/sprintf("%06d",++$c) . ".mp3"/e' *mp3
cc cc nn - ccccccc ccc cccccccc - 123401.mp3 -> cc cc nn - ccccccc ccc cccccccc - 000001.mp3
cc cc nn - ccccccc ccc cccccccc - 123402.mp3 -> cc cc nn - ccccccc ccc cccccccc - 000002.mp3
cc cc nn - ccccccc ccc cccccccc - 123403.mp3 -> cc cc nn - ccccccc ccc cccccccc - 000003.mp3
cc cc nn - ccccccc ccc cccccccc - 123404.mp3 -> cc cc nn - ccccccc ccc cccccccc - 000004.mp3
cc cc nn - ccccccc ccc cccccccc - 123405.mp3 -> cc cc nn - ccccccc ccc cccccccc - 000005.mp3
cc cc nn - ccccccc ccc cccccccc - 123406.mp3 -> cc cc nn - ccccccc ccc cccccccc - 000006.mp3
cc cc nn - ccccccc ccc cccccccc - 123407.mp3 -> cc cc nn - ccccccc ccc cccccccc - 000007.mp3
cc cc nn - ccccccc ccc cccccccc - 123408.mp3 -> cc cc nn - ccccccc ccc cccccccc - 000008.mp3
cc cc nn - ccccccc ccc cccccccc - 123409.mp3 -> cc cc nn - ccccccc ccc cccccccc - 000009.mp3
cc cc nn - ccccccc ccc cccccccc - 123410.mp3 -> cc cc nn - ccccccc ccc cccccccc - 000010.mp3
cc cc nn - ccccccc ccc cccccccc - 123411.mp3 -> cc cc nn - ccccccc ccc cccccccc - 000011.mp3
cc cc nn - ccccccc ccc cccccccc - 123412.mp3 -> cc cc nn - ccccccc ccc cccccccc - 000012.mp3
cc cc nn - ccccccc ccc cccccccc - 123413.mp3 -> cc cc nn - ccccccc ccc cccccccc - 000013.mp3
cc cc nn - ccccccc ccc cccccccc - 123414.mp3 -> cc cc nn - ccccccc ccc cccccccc - 000014.mp3
cc cc nn - ccccccc ccc cccccccc - 123415.mp3 -> cc cc nn - ccccccc ccc cccccccc - 000015.mp3
cc cc nn - ccccccc ccc cccccccc - 123416.mp3 -> cc cc nn - ccccccc ccc cccccccc - 000016.mp3
cc cc nn - ccccccc ccc cccccccc - 123417.mp3 -> cc cc nn - ccccccc ccc cccccccc - 000017.mp3
cc cc nn - ccccccc ccc cccccccc - 123418.mp3 -> cc cc nn - ccccccc ccc cccccccc - 000018.mp3
cc cc nn - ccccccc ccc cccccccc - 123419.mp3 -> cc cc nn - ccccccc ccc cccccccc - 000019.mp3
cc cc nn - ccccccc ccc cccccccc - 123420.mp3 -> cc cc nn - ccccccc ccc cccccccc - 000020.mp3
3

As a shell script:

tempdir="$(mktemp -d)"
i=0; for file in *.mp3; do
  i=$((i+1))
  mv -- "$file" "$tempdir/${file%%[0-9]*.mp3}$(printf '%06d.mp3' "$i")"
done
mv "$tempdir"/* . && rmdir "$tempdir"

Run this script in your mp3 directory. It will create a temporary directory and move renamed files to this dir to avoid renaming to filenames that already exist. When all files are processed, the files are moved backed to the current directory.

While looping through the filenames, ${file%%[0-9]*.mp3} removes the existing suffix nnnnnn.mp3 from the filename and $(printf '%06d.mp3' "$i") adds a new zero-prefixed suffix to the target filename.

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.