Is it possible to remove rather than adding substring to a filename using bash brace expansion?

Considering the following scenario, one can add a suffix to a filename by using the below technique:

mv offlineimap.conf{,.minimal}

What it does is renaming offlineimap.conf to offlineimap.conf.minimal which is very handy esp. for making backup files (eg. swith .bak extension).

But is it also possible to subtract substrings from given filenames, like so:

mv offlineimap.conf.minimal{,-minimal}

Here I use - as a hypothetical special character to substract the substring.

I expect the second technique to result in offlineimap.conf removing the .minimal suffix from the name of an existing file.


To use a brace command to remove a suffix, such as .minimal from the file offlineimap.conf.minimal, use:

mv offlineimap.conf{.minimal,}

More on brace expansion

The idea here is that brace expansion creates a series of strings using the comma-separated list of strings between the braces:

$ echo a{b,c}
ab ac

In your first use, the first of the two strings is empty:

$ echo a{,c}
a ac

In the desired solution, we switch it so that the second of the two strings is empty:

$ echo a{b,}
ab a


$ echo offlineimap.conf{.minimal,}
offlineimap.conf.minimal offlineimap.conf
  • It worked, and I'm accepting the answer (waiting for the 10 min. timeout to pass) as solving, but could you explain in a bit more details how it actually works, and if the last comma is required, and the role of it when the following left empty. – user86041 May 12 '17 at 20:23
  • 1
    It seems (reversing) the position of comma is all to remember in the syntax. – user86041 May 12 '17 at 20:26
  • 2
    @Chinggis6 The brace expansion is just repeating the string with the different parts in {...} in order. a{,b,c} expands to a ab ac while a{b,,c} expands to ab a ac etc. – Kusalananda May 12 '17 at 20:29
  • 1
    @Chinggis6 I added to the answer some examples of brace expansion with and without empty strings. – John1024 May 12 '17 at 20:31

As addition to the John1024 answer (can't commenting).

Tried this method and found shortcut:

  1. Type full file name, which needed cut, using autocompletion - tab key pressing.

    offlinei(tab key pressing) -> get full name -> offlineimap.conf.minimal

  2. type ,} then go one word backward (Alt + b in bash),and insert {,

And we are get needed construction offlineimap.conf.{minimal,}

  • 1
    Alt+b (or rather M-b) is for the default Emacs mode in Bash, I use a global Vi mode (set editing-mode vi >> /etc/inputrc) :) – user86041 May 12 '17 at 21:31
  • 1
    @ Yes, good correction. But my point was using first autocompletion, then brackets. Because someone can doing it in the reverse order (then autocompletion impossible), that is more error prone (mistyping file name) and more key pressing. That is, get full name, then put brackets on its place, not vice versa – MiniMax May 12 '17 at 21:45

Your Answer

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