Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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")
  CONVERTED=$(echo $FILE | sed -e 's/%2F/\//g')
  if [ -f $DIRNAME ]; then  mv $DIRNAME $; fi
  mkdir -p $DIRNAME
  if [ -f $ ]; then  mv $ $DIRNAME/$(basename $DIRNAME); fi

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
share|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
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")
CONVERTED=$(echo $FILE | sed -e 's/%2F/\//')
mkdir -p $(dirname $CONVERTED)

share|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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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