Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Rename all the papers from pattern [author][year] to [year]-[author][key] for the better view in explorer
#! /bin/bash
# rename files
#
# FIXME
# 1. filename with more than one year number
# a file name with more than one year number, like
# Chen2000-tech-Guanling-Chen-TR2000-381, the shell doesn't
# know how to deal with it, as the result of grep "-Po" returned
# with two number "2000\n" and "2000"
#
# 2. filename with spaces
# 3. same filename conflict
# files to be rename with the suffix: nh kdh pdf ps ppt or anything you like.
for filename in `find . -type f -and -name "*.nh" -or -name "*.kdh" -or -name "*.pdf" -or -name "*.ps" -or -name "*.ppt"`
do
echo "filename is $filename"
if [ -f "$filename" ]
then
dname=`dirname "$filename"`
fname=`basename "$filename"`
year=`echo "$fname" | grep -Po "^(19|20)\d{2}"`
if [ $year ]
then
echo "begin with year number, ignored"
continue
fi
year=`echo "$fname" | grep -Po "(19|20)\d{2}"`
if [ $year ]
then
newname="$dname"/$year-"$fname"
echo "new name is $newname"
mv "$filename" "$newname"
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment