Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This one has baffled me – I'm actually trying to make a Thunar custom action to copy the selected file into the same directory appending a number before the extension, e.g.

cp file.ext file_1.ext

Some commands seem to do this automatically like the mogrify -format (except obviously the extension changes).

For example I'd like to copy an image file so that I can mogrify -resize the copy (which will be overwritten).

share|improve this question
up vote 3 down vote accepted

The common way to do this in bash is with parameter expansion. If the file variable holds the filename do something like this:


cp "$file" "$new_file"

To use this in a script ( say), do something like this:



cp -v "$1" "${name}${infix}.${extension}"

Then call it as: FILE INFIX
share|improve this answer
You could of course wrap that in a shell function and actually copy the file. – slhck Jul 12 '12 at 21:43
Forgot it was about copying, modified answer. – Thor Jul 12 '12 at 21:45
cp "$name"{,_1}."${extension} can be used to eliminate the need for the new_file variable. – chepner Jul 12 '12 at 21:50
@chepner Post that as an answer. – slhck Jul 12 '12 at 22:02
for safety, one could include a loop checking whether new_file already exists, to avoid it being overwritten (something like while [ -f "$new_file" ]; do i=i+1; new_file="${name}_${i}.${extension}; done). – Izzy Jul 13 '12 at 9:25

This is just a slightly shorter version of @Thor's answer:


cp "$name"{,_1}."$extension"
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.