15

I have many files in a folder and I want to add either prefix or a suffix (not both) to them. I checked here and found out I can use

for filename in *.jpg; do mv "$filename" "prefix_$filename"; done;

to add a prefix to all files ending in .jpg (and if I remove the .jpg, it will add the prefix to all the files in the current folder).

However, I'd like to be able to

  • Add a sufix (that is, rename filename.ext to filename.whatever.ext),
  • Check if the prefix or suffix is already present and then skip,
  • Create an alias that accepts arguments such as addprefix whatever *.ext or addsufix whatever *.*

4 Answers4

13

If you're using bash, then this one-liner can do it for you (assuming you have the variables $prefix, $suffix and $extension readily available)

mv "$filename" "$prefix${filename%.$extension}$suffix.$extension"

You could have your scripts be like this

#!/bin/bash
# Usage: addprefix <prefix> <files>

prefix=$1
shift
for f in "$@"
do
  mv "$f" "$prefix$f"
done

and

#!/bin/bash
# addsuffix <suffix> <files>

suffix=$1
shift
for f in "$@"
do
  extension=${f##*.}
  if [ -z $extension ]; then
    mv "$f" "$f$suffix"
  else
    mv "$f" "${f%.$extension}$suffix.$extension"
  fi
done
  • That's almost perfect! It would be perfect if it (1) checked if the prefix or suffix is already present (and then skipped), and (2) worked on files with no extension. Sorry for bothering so much... – That Brazilian Guy Sep 20 '13 at 14:13
  • For (1) you could (I think, not tested) add the prefix check by wrapping the mv in an if [ $f == $prefix* ] and you can change the suffix one with if [ $f == *$suffix$([ ! -z $extension ] && echo -n '.')$extension ] around the couple mv commands in addsuffix. For (2) it should already work on files with no extension, unless I am a horrible programmer. – Aaron Okano Sep 21 '13 at 00:30
8

There's a Perl rename command that should do the trick. Beware though: there are several different commands called rename, so make sure you have got the Perl script that expects a perlexpr as its argument. This is the rename command provided by the perl package on Debian and derivatives (Ubuntu, Mint, …), but not on other distributions which may have a different utility called rename.

martin@martin ~/test % touch a.txt
martin@martin ~/test % touch b.txt
martin@martin ~/test % rename 's/^(prefix)?/prefix/' *.txt
martin@martin ~/test % ll
insgesamt 0
-rw-rw-r-- 1 martin martin 0 Sep 19 23:56 prefixa.txt
-rw-rw-r-- 1 martin martin 0 Sep 19 23:56 prefixb.txt
martin@martin ~/test % rename 's/^(prefix)?/prefix/' *.txt
martin@martin ~/test % ll
insgesamt 0
-rw-rw-r-- 1 martin martin 0 Sep 19 23:56 prefixa.txt
-rw-rw-r-- 1 martin martin 0 Sep 19 23:56 prefixb.txt

As you can see, it's idempotent - it won't add the prefix again when you call it multiple times.

It works by either replacing the (zero-length) start of the strings (^) or the start followed by an optional prefix string with prefix.

I'll leave encapsulating this in either a shell script or a shell function as an exercise for you :)

Postfix is a bit harder though, because you have to figure out what part of the filename constitutes the extension...

  • That's a nice answer, but I was looking for something more universal; for this answer you need both perl and this specific (or equivalent) rename being present. And I do know suffixes are a bit harder, it wasn't covered in the linked question, and I'm a beginner on linux, bash, shell script, perl and regular expressions, so "a bit harder" is quite hard to me! :) – That Brazilian Guy Sep 19 '13 at 22:17
5

With zsh:

autoload zmv # in ~/.zshrc
zmv '(*~*suffix).ext' '${1}suffix.ext'

~ is the globbing operator that means except in zsh.

3

Simple example to change the prefix and the extension of all images in the current folder.

prefix=EG_
extension=.jpg
for i in $(ls); do mv $i "$prefix${i%.JPG}$extension";  done

If you want to check how it would effect your data before changing it replace the mv by echo

output for IMG_7993.JPG is EG_IMG_7993.jpg

GAD3R
  • 66,769
Jinxi
  • 131