Re: rename filenames
On 26-03-13 08:43, Frans van Berckel wrote:
>> Frans van Berckel schreef op 2013-03-25 17:23:
>>
>> #!/bin/bash
>> # name of this script: remove-spaces.sh
>>
>> # Check for arguments before starting
>> if [ $# -gt 0 ]; then
>> working_directory=$1
>>
>> # Find wav files in set directory
>> found=$(find "${working_directory}" -type f -iname "* .*")
>>
>> # Need to change IFS or files with filenames containing spaces will not
>> # be handled correctly by for loop
>> IFS=$'\n'
>>
>> # This is a loop for renaming
>> for thisone in ${found};
>> do
>> basename="${thisone%.*}"
>> extension="${thisone##*.}"
>> renamingbase="$(echo "$basename" | sed -e 's/ *$//g')"
>> newfilename=${renamingbase}.${extension}
>> if [ -f ${newfilename} ];
>> then
>> echo "Error renaming: ${renamingbase}.${extension}"
>> echo "We can't rename because the filename already exists"
>> echo "Not renaming: $thisone" >>/var/log/remove-spaces.log
>> else
>> echo "Renaming: ${newfilename}"
>> mv "${thisone}" "${newfilename}"
>> fi
>> done
>>
>> else
>> echo "Please enter directory to process. Ex: remove-spaces.sh
>> directory"
>> exit 1
>> fi
>
> Het door mij gebruikte find commando, vindt teveel bestanden. Dat is omdat
> er ook elders in de bestandsnaam wel eens een spatie en punt voorkomt.
>
> # find . -type f -iname "* .*"
>
> Daarom de vraag, wat gaat er precies fout gaat met de regex schrijfwijze,
> dat het niet werkt?
>
> # find . -type f -regex " .[^.]*$"
>
> Na wat testen zag ik, dat het met find | egrep wel werkt.
>
> find | egrep ' \.[^.]*$'
d'oh. Dat zal me leren, dingen mailen zonder te testen.
De regex die ik gegeven heb is fout. Je moet de regex gebruiken die je
zelf opgegeven hebt :-)
>
> Met vriendelijke groet,
>
>
> Frans van Berckel
>
>
--
Copyshops should do vouchers. So that next time some bureaucracy
requires you to mail a form in triplicate, you can mail it just once,
add a voucher, and save on postage.
Reply to: