Skip to content

Instantly share code, notes, and snippets.

@jackeylu
Created July 27, 2012 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackeylu/3188132 to your computer and use it in GitHub Desktop.
Save jackeylu/3188132 to your computer and use it in GitHub Desktop.
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