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
link|improve this question

Which OS? ..... – Hyppy May 19 '11 at 14:58
Judging by the directory slash, i'd assume *nix – Ryan Gibbons May 19 '11 at 15:03
Linux (Ubuntu 9.10, specifically), though could also be for Windows 7 (I have a feeling this is way easier on Linux though) – gregmac May 19 '11 at 15:53
feedback

1 Answer

up vote 1 down vote accepted

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



link|improve this answer
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
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.