スクリプトはディレクトリによって異なる動作をします。

スクリプトはディレクトリによって異なる動作をします。

名前に奇妙な文字を含むファイルの名前を変更するように特別に設計された、より大きなスクリプトから抽出された単純なスクリプトがあります。あるディレクトリで正常に実行されると述べたように、同じコンピュータ上の別のディレクトリで正常に実行しましたが、何らかの理由でより高いレベルのディレクトリで実行すると、ファイル名の変更エラーが発生します。たとえば、UnsortedMusicというフォルダに対してこのコマンドを実行しました。やるべきことは次のとおりです。

/UnsortedMusic/Dir 001/SubDir 001/Song To Sing.flac > /UnsortedMusic/Dir 001/SubDir 001/SongToSing.flac

名前からスペースを削除し、デフォルトで同じコードを使用しますが、特にディレクトリのディレクトリ名からスペースを削除します。

/UnsortedMusic/Dir 001/SubDir 001/SongToSing.flac > /UnsortedMusic/Dir001/SubDir001/SongToSing.flac

これは他のディレクトリではうまく機能しますが、何らかの理由で特定のディレクトリでは次のことが行われます。

 /UnsortedMusic/Dir 001/SubDir 001/Song To Sing.flac > /UnsortedMusic/Dir001/SubDir001/UnsortedMusicDir001SubDir001SongToSingflacSongToSingflac

ご覧のとおり、すべてのディレクトリ名にファイル名を変更するだけでなく、「.」も削除します。 .flacでは、これは何千ものファイルを手動で変更する必要があることを意味します。

このコードは次のとおりです。

#!/bin/bash
#set -e

FolderName="Unsorted" #Dir location to be sorted

# Log Location on Server.
LOG_LOCATION=$(pwd) #/home/user/scripts/logs
exec > >(tee -i $LOG_LOCATION/RenameFile.log)
exec 2>&1
Start by seeing if the new directory allready exists, if not, make it

echo "Looking in $FolderName for files with abnormal characters"
#Rename any directories that might have a weird name structure that could conflict
find ${FolderName} -type f \( -name "* *" -o -name "*(*" -o -name "*[*" -o -name "*&*" \) -print0 | sort -rz | while read -d $'\0' file;
    do
        mv -v "$file" "$(dirname "$file")/$(basename "${file//[^a-zA-z0-9_-.]/}")";
done

#echo "Log Location should be: [ $LOG_LOCATION ]"
echo "Finished Process"

これは、次のような大きなプロセススクリプトの一部です。

#!/bin/bash
#set -e

NewDirName="TestDirV2" #New Dir name
FolderName="TestDir" #Dir location to be sorted

# Log Location on Server.
LOG_LOCATION=$(pwd) #/home/user/scripts/logs
exec > >(tee -i $LOG_LOCATION/Organizer.log)
exec 2>&1
#Start by seeing if the new directory allready exists, if not, make it
if [ ! -d ${NewDirName} ];
then 
    mkdir -p ${NewDirName}
    chmod -R a+rwx ${NewDirName}
fi

echo "Looking in $FolderName for files with abnormal characters"
#Rename any files that might have a weird name structure that could conflict 
find ${FolderName} -type f \( -name "* *" -o -name "*(*" -o -name "*[*" -o -name "*&*" \) -print0 | sort -rz | while read -d $'\0' file;
    do
        mv -v "$file" "$(dirname "$file")/$(basename "${file//[^a-zA-z0-9_-.]/}")";
done

echo "Looking in $FolderName for directories with abnormal characters"
#Rename any directories that might have a weird name structure that could conflict 
find ${FolderName} -type d \( -name "* *" -o -name "*(*" -o -name "*[*" -o -name "*&*" \) -print0 | sort -rz | while read -d $'\0' file;
    do
        mv -v "$file" "$(dirname "$file")/$(basename "${file//[^a-zA-z0-9_-.]/}")";
done


for FileType in $(find $FolderName -type f | perl -ne 'print $1 if m/\.([^.\/]+)$/' | sort -u)
do


    echo "Finished removing odd characters that may result in problems"

    NewDir=$(echo "${NewDirName}/${FileType}");      

    if [ ! -d ${NewDirName}/${FileType} ];
        then
            echo "$NewDir does not exist"      
            echo "Making $NewDir"
            mkdir -p ${NewDir}
            chmod -R a+rwx ${NewDir}
    fi

    echo "Now looking for renamed $FileType files"
    for FileToMove in $(find ${FolderName} -name "*.${FileType}");
    do  
        echo "Dir to move ${FileToMove}"        
        echo "File to move ${FileToMove##*/}"

        SpecificFile=$(echo "${FileToMove##*/}");       

        echo "Checking to see if $SpecificFile already exist in $NewDir"

        if [ -f ${NewDir}/${SpecificFile} ];
        then    
            x=0
            while [ -f ${NewDir}/${SpecificFile} ]        
                do
                echo "File ${SpecificFile} already exists, renaming"
                SpecificFile=${x}${SpecificFile}
                x=$(( x++ ));
                echo "Renamed to $SpecificFile"
            done
        fi    

    #rsync -rav --progress ${FileToMove} ${NewDir}/${SpecificFile}
    mv -v ${FileToMove} ${NewDir}/${SpecificFile}
    done

done
chmod -R a+rwx ${NewDir}

echo "Log Location should be: [ $LOG_LOCATION ]"
echo "Finished Process"

ベストアンサー1

おすすめ記事