Skip to main content

Filename sanitization examples in sed.

##
# It's a problem for every Bash programmer out there. you go out and write this
# awesome script that would work great, except that it fails because your
# automagically generated filenames are not properly sanitized.
#
# Through much experimentation I have creaded several sed commands that can be
# used to sanitize filenames based on the alowable set of characters.
#
# source: http://usalug.com/phpBB3/viewtopic.php?t=13980
##

### Set: A-Za-z0-9.

    # Delete:
    sed -e s/[^A-Za-z0-9.]//g
    # Replace:
    sed -e s/[^A-Za-z0-9.]/_/g

### Set: A-Za-z0-9.[]{}()-_

    # Delete:
    sed -e s/[^\]\[A-Za-z0-9._{}\(\)\-]//g
    # Replace:
    sed -e s/[^\]\[A-Za-z0-9._{}\(\)\-]/_/g

### Set: A-Za-z0-9~.,_[]{}()'-+

    # Delete:
    sed -e s/[^\]\[A-Za-z0-9~.,_{}\(\)\'\-\+]//g
    # Replace:
    sed -e s/[^\]\[A-Za-z0-9~.,_{}\(\)\'\-\+]/_/g

# All three of these example sets work well, although I suggest using the most
# restrictive set possible for your problem set for maximum portability.a