Bash brace expansion to remove part of filename

bashbrace-expansionstringsyntax

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.

Best Answer

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

Or:

$ echo offlineimap.conf{.minimal,}
offlineimap.conf.minimal offlineimap.conf
Related Question