gychang wrote: Wed Mar 05, 2025 7:19 pm works, although I have no idea how...,
The simplest Linux tool for renaming a file is "mv". Do this:
1. Make a file named: a
2. Run: mv a b
The script just "mv"s each file in the loop to a new name made with some bash text substitution.
FWIW, if you don't have a functional 'rename' command, you can use this method along with 'find' to rename files. It would also work recursively on batches of files in subfolders.
Code: Select all
find -name '*.MP*' -exec sh -c 'n={} && mv $n ${n/.MP/}' \; #replace .MP with nothing
find -name '*JPEG' -exec sh -c 'n={} && mv $n ${n/JPEG/jpg}' \;
Or you could do it in one line with:
Code: Select all
find -iname '*.jp*' -exec sh -c 'n={} && nn=${n/.MP/} && mv $n ${nn/JPEG/jpg} 2>/dev/null' \;
You could simultaneously change either *.JPEG or *.jpeg to *.jpg with:
Code: Select all
find -iname '*.JPEG' -exec sh -c 'n={} && mv $n ${n%.*}.jpg' \; #strip off the extension and add a new one
You coud rename a bunch of .doc files to remove any spaces in the filenames with:
Code: Select all
find -name '*.doc' -exec sh -c 'n="{}" && mv "$n" ${n// /}' \; #replace all spaces with nothing - note the extra quoting
If you have a set of files named f1, f2, f3, f45, f56, f789, you could rename them with leading zeros f001, f002, etc.
Code: Select all
find -name 'f*' -exec sh -c 'n={} && nn=$(dirname $n)/f$(printf "%03d" ${n#*f}) && mv $n $nn' \; #pull out the number and format it with zeros
Here is a way to remove any upper case letters from filenames:
Code: Select all
find -name '*.txt' -exec sh -c 'n={} && mv $n ${n,,}' \; #use the bash upper-lower case conversion method
Here is an extreme example. It takes a bunch of png files, converts them to jpegs using the image-changer tool and stores them in a temp folder.
Code: Select all
find -name '*.png' -exec sh -c 'n={} && nn=$(basename $n png)jpg && image-changer $n /tmp/pics/$nn' \;
Or here's one that shrinks a collection of jpegs by 50%
Code: Select all
find -name '*.jpg' -exec sh -c 'n={} && nn=$(basename $n) && image-changer $n /tmp/pics/$nn 50' \;