[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: rename filenames



> 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 ' \.[^.]*$'

Met vriendelijke groet,


Frans van Berckel


Reply to: