Dateien umbennen und verschieben

MrBoe

MrBoe

Routinier
Hallo!

Ich beginne grade mich etwas in shell Programmierung einzuarbeiten. Ich habe mir ein Skript größtenteils geklaut und ein bisschen daran herumprobiert. Unter anderem habe ich einen Counter eingebaut, der seinen initialen Wert aus einer Datei liest.

Mag man über das Skript denken was man will.
Ich habe ein Problem.
Es funktioniert soweit ganz gut. Allerdings kann es nicht mit Dateinamen umgehen, die Leerzeichen enthalten. Wie kann ich das ändern?

Code:
#! /bin/bash
#
#  NAME: number_files_01.sh
#
#  SHELLS: bash
#
#  PURPOSE: utility that numbers the files in a directory consecutively
#
#  USAGE: number_files_01 <directory>
#+ As in "number_files_01 ~/Bilder/Urlaub_2006/", which numbers all picture files in the specified directory


if [ -z "$1" ]                  # check whether a command line argument was specified by the user

then

    echo "Usage: `basename $0` <directory>"     # error message

    exit 65                             # stop execution of the script

elif [ ! -d $1 ]                # check whether the argument specified by the user is really a directory

then

    echo "$1 is not a directory!"               # error message

    exit 1                      # stop execution of the script

fi                              # end of first IF-clause

COUNTER=$(cat nummer)           # establish a counter for the new filenames

cd $1                           # change the current directory to the one specified by the user

for FILENAME in *.jpg         # traverse all pictures in the directory specified

do                              # begin loop

    if [ -f "$FILENAME" ]       # check whether there is a file to rename

    then

        let "COUNTER += 1"                      # increase the counter
        new_name=`printf %06d.jpg $COUNTER`     # generate the new filename: consisting of 4 digits

        if [ ! -d ./tmp ]                       # check whether a subdirectory already exists

        then

            mkdir tmp 2> /dev/null              # try to create a temporary directory in $1
            if [ $? -ne 0 ]                     # check whether the subdirectory was generated

            then

                echo 'The necessary subdirectory could not be created!'  1>&2     # error message

                exit                            # stop execution of the script

            fi                                  # end of fourth IF-clause

        fi                                      # end of third IF-clause

        cp -i $FILENAME ./tmp/$new_name         # the combination of "cp" and "rm" seems more safe than the plain "mv"

        if [ $? -eq 0 ]                         # check whether the file was copied

        then

            rm $FILENAME                        # delete the original file

        fi                                      # end of fifth IF-clause
        echo $COUNTER > nummer

    fi                                          # end of second IF-clause

done                            # end of the loop

mv ./tmp/*.jpg $1            # move the renamed files to the directory specified by the user

if [ $? -eq 0 ]                 # check whether the files could be moved

then

    rm -r ./tmp                 # delete the temporary directory

else

    echo "The renamed files are still in the directory ./tmp ..."

fi                              # end of sixth IF-clause

exit 0                          # end of the script
 
Hi,

Du musst die Leerzeichen entweder "escapen" (" "="\ ") oder den Namen in Anführungszeichen schreiben.
 
... oder du machst es mit 'read'.
Gruss
d22
 
Also das mit dem for FILENAME in "*.jpg" klappt nicht.
Da scheint er die for Schleife direkt zu beenden.

Wie mache ich das mit read? Muss ich da erste ein Liste mit "ls -l" erstellen, aus der dann gelesen wird?
 
Alle Variablen die Dateinamen enthalte konsequent an allen Stellen Quotings ("") rein.
Bei verschiedenen Befehlen klappt es nicht, da die Shell die Argumente am IFS (default Leerzeichen und Tab) zerstückelt.

Ein mögliches Konstruckt um Leerzeichen und anderen (Sondermüll) in Dateinamen zu verdauen ist find mit xargs.
Ja, die Leerzeichen sind sehr schnell uncool, wenn die mal in einem Skript verarbeitet werden müssen. ;)

Geht es nur um Leerzeichen, dann hilft auch schon read:
Code:
find -type f -iname "*jpg" |while read filename;
do
echo "$filename"
...
done
ls -l ist keine gute Idee, da du ja das Longformat dann erst wieder parsen musst.


Gruß Wolfgang
 
Mit 'ls -1' wärs auch ne möglichkeit:
Code:
ls -1 | while read filename
do 
  echo $filename
  ...
done
Gruss
d22
 
Hoho, vielen Dank!

Bash ist ja schon ne ganz coole Geschichte, was?
 

Ähnliche Themen

Zugriff Ubuntu 16.04. auf Freigabe 18.04. LTS nicht möglich

Verschlüsseltes Backup-Script mit rsync

HandbrakeCLI Shell Skript

Port generieren, wenn nicht dann

Crontab und Scripts - Problem

Zurück
Oben