USA Linux Users Group Forum Index
Log in Register FAQ Memberlist Search USA Linux Users Group Forum Index Album

Bash Scripting: Sanitize filenames

 
Post new topic   Reply to topic   printer-friendly view    USA Linux Users Group Forum Index » Shell Scripting and Programming
View previous topic :: View next topic  
Author Message
Lord.DragonFly.of.Dawn
Advanced Member


Joined: 18 Jul 2024
Posts: 607
Location: South Portland, Maine, USA, Earth, Sol System

PostPosted: Wed Jan 14, 2024 3:52 pm    Post subject: Bash Scripting: Sanitize filenames Reply with quote

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.

Set: A-Za-z0-9.
delete:
Code:
sed -e s/[^A-Za-z0-9.]//g

or replace:
Code:
sed -e s/[^A-Za-z0-9.]/_/g


Set: A-Za-z0-9.[]{}()-_
delete:
Code:
sed -e s/[^\]\[A-Za-z0-9._{}\(\)\-]//g

or replace:
Code:
sed -e s/[^\]\[A-Za-z0-9._{}\(\)\-]/_/g


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

or replace:
Code:
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.



_________________
ArchLinux x86_64 - Custom Built Desktop
ArchLinux x86_64 - Compaq CQ50 Laptop
ArchLinux i686 - Acer Aspire One Netbook
ArchLinux i686 - Dell Presario ze2000 (w/ shattered LCD)

PuppyLinux, CloneZilla, PartedMagic, DBAN - rescue thumbdrives
Windows 7 (x86_64 desktop alternate boot)
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    USA Linux Users Group Forum Index » Shell Scripting and Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All content © 2024-2009 - Usa Linux Users Group
This forum is powered by phpBB. © 2024-2009 phpBB Group
Theme created by phpBBStyles.com and modified by Crouse