Home > bash, linux > bash script to recursively sanitize folder and file names

bash script to recursively sanitize folder and file names

Below is a bash script to recursively sanitize folder and file names. It leaves all numbers, letters, dots, hyphens and underscores untouched, but replaces all other characters with underscores.

#! /bin/bash

sanitize() {
  shopt -s extglob;

  filename=$(basename "$1")
  directory=$(dirname "$1")

  filename_clean="${filename//+([^[:alnum:]_-\.])/_}"

  if (test "$filename" != "$filename_clean")
  then
    mv -v --backup=numbered "$1" "$directory/$filename_clean"
  fi
}

export -f sanitize
find $1 -depth -exec bash -c 'sanitize "$0"' {} \;
Advertisements
Categories: bash, linux
  1. Raimondo
    May 30, 2013 at 12:47

    Thank you, that was helpful!

  2. June 1, 2014 at 07:28

    Just wanted to say thanks, just the thing to save me from a directory of filenames which should never have existed!

  3. October 16, 2014 at 20:07

    touch “_\* uu ~ \( \) .txt ”

    Now run your sanitizer on this. Let me know what happens. Personally I get:

    mv: rename ./_* uu ~ ( ) .txt to ./_uu_txt: No such file or directory

    tai@recluse:~/Downloads/tester$bash -version
    GNU bash, version 3.2.51(1)-release (x86_64-apple-darwin13)
    Copyright (C) 2007 Free Software Foundation, Inc.

    • October 16, 2014 at 23:22

      Thanks for pointing this out.

      I changed the script to not use “read” anymore, because that was causing the problems.

      Instead I now use find -exec with a shell function.

    • October 16, 2014 at 23:25

      I also added a –backup option to the mv command to make sure that multiple files that would all be sanitized to the same file name do not get lost in translation. :o)

  4. October 16, 2014 at 20:08

    Related to the above: I managed to put a newline in the filename but the HTML sanitizer from WordPress won’t let me paste it in. :) So the filename above should have a Return just before the final quote. FYI.

  1. No trackbacks yet.

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: