我有一个简单的脚本,是从一个较大的脚本中提取出来的,该脚本专门用于重命名名称中包含奇数字符的文件。我已经在同一台计算机上的不同目录中成功运行了它,正如我们所说,它在一个目录中成功运行,但由于某种原因,当针对更高级别的目录运行它时,它会错误地重命名文件。例如,我已经针对名为 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"