Adding suffixes to file names according to previous suffixes I have a folder containing files like

ABC - 2001
DEF
EFG - 2001-2002
HIJ
KLM - 2003
NOP
QRS - 2004  

I want to add the year to all file names that currently have no year suffix. The suffix should always be the one of the last file with a year suffix: EFGcontains 2001 and the start of 2002, so HIJ needs the suffix 2002.

How could I do this?
This should do it for the file names you have given. It assumes that the name part consists of uppercase letters, spaces, and dashes. And in the case of two years present, that they are separated by a dash. Also, it assumes that the first file does contain a year (otherwise it has nothing to deduct it from), and it will process every file present in the directory.

Copy&amp;paste to the Terminal, and if you want to check first that it will do it correctly, add an echo before the mv.

for FILENAME in *
do
  # remove name part
  TRIM=`echo $FILENAME | sed -E 's/[A-Z -]*//'`
  # remove possible start year
  TRIM=`echo $TRIM | sed -E 's/[0-9]*-//'`
  if [ -z "$TRIM" ]
  then
    # file name didn't include year
    mv "$FILENAME" "$FILENAME - $YEAR"
  else
    # file name did include year
    YEAR=$TRIM
  fi
done