Remplacer les espaces par des underscores

Petit script trouvé sur Space Replace 1.0.1 qui permet de remplacer tous les espaces d’un répertoire ainsi que son contenu par des undescores.
Par exemple, intricate – in pectra devient intricate_-_in_pectra
Script to replace all space in file and directory with an undescore. Script found Here

Il suffit de copier le contenu du script ci dessous dans un fichier dans votre /usr/local/lib. Vous pouvez également télécharger le script ainsi qu’un script d’installation ici

Just copy this file in you /usr/local/lib for example.
You can also Download this script with an installation script Here


#!/bin/bash
# spacereplace v1.0.1 by Richard van Kampen - july 2005
function helpme {
echo ""
echo "Help for spacereplace"
echo ""
echo "spacereplace version 1.0.1 - written by Richard van Kampen"
echo "Tool to substitute spaces in file and directory names with underscores."
echo ""
echo "Usage: spacereplace [option] [PATH]"
echo ""
echo "List of options:"
echo ""
echo "-h or --help : show help."
echo "-r : recursive. Also commit changes to all sub-directories."
echo ""
echo "You have to specify the path to the directory you want to process. Not specifying a path will just display help"
echo ""
echo "Example: 'spacereplace -r /home/richard/video' changes all spaces to underscores in /home/richard/mp3 and all directories below /home/richard/mp3."
echo ""
exit
}

for arg in "$@" # grab command line options
do
if [ "$arg" != "-r" ] && [ "$arg" != "-h" ]; then
dir="$arg"
orgdir="$dir"
sub=" and sub-directories"
fi
case "$arg" in
-h | --help ) help=1 ;; # help option true.
-r ) recursive=1 ;; # recursive is true
esac
done

if [ "$dir" == "" ];then
helpme
fi
if [ ! -d "$dir" ]; then
echo "Directory $dir does not exist. Please try again with full path"
exit
fi

if [ "$help" = "1" ]; then # do this if -h option is used
helpme
fi

function delspaces {
strips=1;
teller=1; # iterate through 'contentgrab' array
cd $dir;
# rename @ because we need it. Do it 5 times
until [ $strips = "5" ];do
rename @ - *
let strips+=1
done
for i in $(ls | sed 's/\n//g' | sed 's/\ /@/g'); do
i=${i//@/ }
contentgrab[$teller]=$i
replacespace=${contentgrab[$teller]// /_}
if [ "${contentgrab[$teller]}" != "$replacespace" ] && [ ! -f "$replacespace" ] && [ ! -d "$replacespace" ]; then
mv -- "${contentgrab[$teller]}" "$replacespace"
fi
let teller+=1;
done
}

Ce contenu a été publié dans Linux, Solaris, avec comme mot(s)-clé(s) . Vous pouvez le mettre en favoris avec ce permalien.

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *