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
tofilename.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
oraddsufix whatever *.*
Best Answer
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)You could have your scripts be like this
and