jump to navigation

moodle and fun unix tricks – command line file name cleaning March 4, 2010

Posted by ficial in Blogroll.
trackback

I just spent a while figuring this out and it seems like the sort of thing that may be useful to other. In brief, the unix file system allows file names to have pretty much any character, which seems like a nice idea. However, when one is trying to serve those files via the web there can be difficulties. Moodle uses the file system directly to generate links to file based on their name. So, if you have, for example, a file called “assignment#1.doc” that’s turned into a link like href=”assignment#1.doc”, and when that link is clicked the browser requests the file “assignment” and then would jump to the anchor “1.doc”. Since the file “assignment” doesn’t exist, this causes problems.

So, I’ve been working on a unix command that can clean up all the file names in a directory tree (to run either as a cron process or to be inserted into the moodle code base somewhere (probably in a couple of functions in lib/moodlelib.php – the upload and unzip ones spring to mind…). And, here’s what I have so far:


fixfiles() { for item in *; do i=`echo "$item" | sed 's/\s/-/g' | sed 's/[\!\#\$\%\&\+:\~\`\\]/_/g'`; if test "$item" != $i; then mv "$item" $i; fi; if test -d "$i"; then cd $i; fixfiles .; cd ..; fi; done }; fixfiles .;

This defines a function which looks at all the contents of the current directory, determines a clean name for each item, does the cleaning if necessary, and recurses into sub-directories. Then it just calls that function for the current directory.

It’s not fool-proof, but it should solve a lot of our headaches once it’s in place.

Advertisements

Comments»

No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: