1

So my filename looks like this :

Building+a+Container+for+Robust+Conversations.pdf

I would like to use mmv to rename it like this :

Building_a_Container_for_Robust_Conversations.pdf

Or like this :

BuildingaContainerforRobustConversations.pdf

2
  • 2
    Have you tried anything? mmv -n … would be a good place to start. Have you looked at man mmv, particularly the examples section?
    – roaima
    Feb 16, 2021 at 10:35
  • 3
    Usage of rename command could save you some troubles with mmv.
    – BlueManCZ
    Feb 16, 2021 at 10:47

2 Answers 2

5

(Assuming you want to perform the same operation on all files in the current directory...)

To replace + with _ :

mmv '*+*' '#1_#2'

To strip + :

mmv '*+*' '#1#2'

Now, mmv only replaces the first matching + for each file. If you are doing this manually from the shell, and only want/need to do this renaming process once, then you can just repeat the command as many times as is necessary. (Pressing the Up/Down Arrow keys is the easiest way to scroll through your command history.) If you are going to do this, then adding the -v (verbose) flag is advisable, as it will let you know what the mmv command did, and when there is no more work to do. For example:

$ mmv -v '*+*' '#1#2'
a+b+c.jpg -> ab+c.jpg : done
d+e+f.jpg -> de+f.jpg : done
g+h+i.jpg -> gh+i.jpg : done
$ mmv -v '*+*' '#1#2'
ab+c.jpg -> abc.jpg : done
de+f.jpg -> def.jpg : done
gh+i.jpg -> ghi.jpg : done
$ mmv -v '*+*' '#1#2'
*+* -> #1#2 : no match.
Nothing done.

If you want all occurrences to be replaced in all of the files in a single pass, or want to embed this command into a script of some sort, then a more elaborate solution, or a different command, is needed. Perhaps consider rename?

rename 's/\+/_/g' *
or
rename 's/\+//g' *
5
  • 1
    This is not correct because it only replaces the first + character in the filename. OP wants to replace all occurrences.
    – BlueManCZ
    Feb 16, 2021 at 12:31
  • 1
    and on a single file
    – ychaouche
    Feb 16, 2021 at 15:08
  • 2
    @BlueManCZ I had (rightly or wrongly) assumed that the command was being run manually and that efficiency wasn't paramount — so the OP could merely repeat the command as many times as necessary. I have amended the answer to make this clear, and suggest other alternatives if that is not the case. Thanks for pointing it out.
    – Tim
    Feb 16, 2021 at 22:56
  • 2
    @ychaouche On GNU Bash 4.4.20 (installed on Ubuntu 18.04) the command renames all of the files in the current directory, not just one. If you want just one, then you would need to make the matching strings unique enough... but at that point simply using mv would make more sense.
    – Tim
    Feb 16, 2021 at 23:01
  • @Tim Now it looks better. I would say repeating command multiple times is kinda.. meh.. but it does the trick. And it does indeed work for all files in a directory.
    – BlueManCZ
    Feb 17, 2021 at 8:35
2

rename all files with "+" to be with "_"

rename "s/+/_/" *

rename all files with "+" to be without any whitespaces

rename "s/+//" *

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .