16
Mar

Convert Human Directory Name to Unix Directory Name

The following script will take a human directory name like “/My Favorites/Music/Obscure Band’s Hits/01 – Wish I Knew.mp3” from standard input or file to a directory name that unix commands will recognize like “/My Favorites/Music/Obscure Band’s Hits/01 – Wish I Knew.mp3”. This can be useful in a scripting. Ultimately, this isn’t very practical since all you really need to do is use quotes around the human directory name, but there might be a situation that presents itself when you need this, so here it is:

echo "directory name" | sed 's/ /\ /g' | sed s/"'"/'\'"'"/g | sed s/")"/'\'")"/g | sed s/"("/'\'"("/g | sed s/"&"/'\'"&"/g | sed s/","/'\'","/g

You can see that it will replace any character that requires a break including spaces, quotes, apostrophes, ampersands, commas, etc…

Example usage:

localhost:~ username$ echo "/My Favorites/Music/Obscure Band's Hits/01 - Wish I Knew.mp3" | sed 's/ /\ /g' | sed s/"'"/'\'"'"/g | sed s/")"/'\'")"/g | sed s/"("/'\'"("/g | sed s/"&"/'\'"&"/g | sed s/","/'\'","/g
/My Favorites/Music/Obscure Band's Hits/01 - Wish I Knew.mp3
localhost:~ username$ 

Feel free to donate if this post prevented any headaches! Another way to show your appreciation is to take a gander at these relative ads that you may be interested in:


There's 0 Comment So Far

Share your thoughts, leave a comment!