Here is a simple script in pure bash
:
for f in *; do # for all entries in the current directory
if [ -f "$f" ]; then # if the entry is a regular file (i.e. not a directory)
mv "$f" "${f/-*-/-}" # rename it by removing everything between two dashes
# and the dashes, and replace the removed part
# with a single dash
fi
done
The magic done in the "${f/-*-/-}"
expression is described in the bash manual (the command is info bash
) in the chapter 3.5.3 Shell Parameter Expansion
The *
pattern in the first line of the script can be replaced with anything than can help to narrow the list of the filles you want to rename, e.g. *.txt
, *File*.txt
, etc.