1

I would like to change this directory structure to the one following it. Although another directory batch rename question exists, this one is quite different.

l
'001. Aretha Franklin - Respect.mp3'*
'002. Ben E. King - Stand by Me.mp3'*
'003. Sam & Dave - Soul Man.mp3'*
'090. Archie Bell & The Drells; The Drells - Tighten Up, Pt. 1.mp3'*

for i in *.mp3; do echo $i | sed -E 's/([0-9]{3}. )(.*) *( - )(.*) *(.mp3)/\4\3\2\5/'; done;

Respect - Aretha Franklin.mp3
Stand by Me - Ben E. King.mp3
Soul Man - Sam & Dave.mp3
Tighten Up, Pt. 1 - Archie Bell & The Drells; The Drells.mp3

If I try to use the in-place option, I get this, with and without an input file:

for i in *.mp3; do echo $i | sed -iE 's/([0-9]{3}. )(.*) *( - )(.*) *(.mp3)/\4\3\2\5/'; done;
sed: -e expression #1, char 47: invalid reference \5 on `s' command's RHS

etc.

for i in *.mp3; do echo $i | sed -iE 's/([0-9]{3}. )(.*) *( - )(.*) *(.mp3)/\4\3\2\5/'<$i; done;
bash: $i: ambiguous redirect

etc.

for i in *.mp3; do echo $i | sed -Ei 's/([0-9]{3}. )(.*) *( - )(.*) *(.mp3)/\4\3\2\5/'; done;
sed: no input files

etc.

for i in *.mp3; do echo $i | sed -Ei 's/([0-9]{3}. )(.*) *( - )(.*) *(.mp3)/\4\3\2\5/' <$i; done;
bash: $i: ambiguous redirect

etc.

Back in the 1970s, I was able to edit the directory directly in Unix, as the directory listing itself is a file. I don't seem to be able to do that today.

vi . gives a blank screen filled with the normal empty file entries, and the bottom line shows this:

"." is a directory

while

ed .

Is a directory

How do I modify a directory listing in bulk today? As you can see, I finally do have my expressions properly worked out, at least in sed. If there's a problem with them, too, please let me know. I am most grateful for your help.

1

When you do

echo $i | sed ...

sed is operating on a stream of bytes read from the standard input stream, rather than on the file's directory entry - so editing "in place" makes no sense.

In addition, GNU sed treats an argument after -i as an optional backup suffix, so that when you write sed -iE, the E is no longer parsed as a flag indicating Extended Regular Expression syntax - which breaks your capture grouping.

If you want to rename files by manipulating their names using sed, you'd need to capture the resulting standard output stream in a command substitution and pass the result as a destination argument to the mv command ex.:

for i in *.mp3; do mv -- "$i" "$(echo "$i" | sed -E 's/pattern/replacement/')"; done

or

for i in *.mp3; do mv -- "$i" "$(sed -E 's/pattern/replacement/' <<< "$i")"; done

if you want to use shell redirection rather than piping to supply standard input to sed. Note that a single command cannot accept standard input from both a pipe and a redirection - that's an "ambiguous redirect".

(In the 1970s, you'd have used backticks like `echo "$i" | sed -E s/pattern/replacement/` for command substitution - they're deprecated now, although still supported.) Note the use of double quotes, without which the command will break on filenames that contain whitespace or other shell-special characters.

On current Ubuntu systems, there should be a Perl-based rename command that can operate on the dirents directly:

rename 's/pattern/replacement/' *.mp3

where pattern is a Perl Compatible Regular Expression (PCRE). There's also an mmv command (provided by an Ubuntu package of the same name) that works with shell globs instead of regular expressions and is often easier for simple re-arrangments like this ex.:

mmv '*. * - *.mp3' '#3 - #2.mp3'

Both rename and mmv have a -n option that lets you trial-run the re-namings.

2
  • I finished all my file renames using mostly the rename command, and then the mv command when rename failed. Rename is a very powerful tool! – Lenny217 Apr 26 at 22:57
  • and rename is also supported with gui approach using krusader file manager. – pierrely Apr 30 at 7:01
0

Try mmv. In your case:

mmv '[0-9][0-9][0-9]. * - *.mp3' '#5 - #4.mp3'

mmv uses #1 for the first wildcard match, #2 for the second etc.; in this pattern we have three wildcards for the leading digits, then a literal dot, a literal blank, then the song artist (#4) and the song title (#5) separated by " - ".

In the replacement part, we omit the unwanted numbers and swap artist and title.

Life can be so simple when you use the right tools. ;-)

mmv is a marvel. Read the man page and experiment; you will find it well worth-while.

You may have to install the package: sudo apt install mmv.

0

Alternatively, you could use a tool like kid3-qt that can read ID3 tags from MP3s and create your own rule how to rename the files based on several different tags to choose from. It can rename a whole directory worth of MP3 files at once.

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.