0

I have a bunch of files that were exported (from my trac wiki) that are named like ParentPage%2FSubPage but they actually should be like ParentPage/SubPage. Anyone have a quick and dirty way to rename and organize them, so that ParentPage will be a directory, and then SubPage will be a file inside that?


Slightly modified from @Christopher Karel's answer, which is what I used:

for FILE in $(ls | grep "%2F")
do
  CONVERTED=$(echo $FILE | sed -e 's/%2F/\//g')
  DIRNAME=$(dirname $CONVERTED)
  if [ -f $DIRNAME ]; then  mv $DIRNAME $DIRNAME.page; fi
  mkdir -p $DIRNAME
  if [ -f $DIRNAME.page ]; then  mv $DIRNAME.page $DIRNAME/$(basename $DIRNAME); fi
  mv $FILE $CONVERTED
done

and the test data I used (in an empty dir), before running the above:

touch Test; touch Test%2FA%2F1; touch Test%2FA%2F2; touch Test%2FB
gregmac
  • 1,579
  • 4
  • 18
  • 27

1 Answers1

1

OK, here's a bash method to do this. Not foolproof, but quick and dirty.

for FILE in $(ls | grep "%2F")
do
CONVERTED=$(echo $FILE | sed -e 's/%2F/\//')
mkdir -p $(dirname $CONVERTED)
mv $FILE $CONVERTED
done



Eduardo Ivanec
  • 14,881
  • 1
  • 37
  • 43
Christopher Karel
  • 6,582
  • 1
  • 28
  • 34
  • Thanks, that basically did the trick. I had to add a quick check because I didn't think of the situation where there is already a file with the target directory name (that now gets moved to the new directory), and I modified it slightly to handle multiple levels of directories. I'll post my version – gregmac May 20 '11 at 18:27