我編寫了一個腳本來一次重命名多個文件並添加前導零。 腳本將第一個參數作爲要重命名的文件,第二個參數是新名稱,第三個參數可以給出新擴展名linux腳本:使用空格對文件進行批量重命名
只要文件不包含空格(test asd 1。 TXT /測試ASD 2.txt),因爲那麼輸出是:
~/Desktop $ gpRenameWithZero test\ asd\* test_ mp3
ls: cannot access test: No such file or directory
ls: cannot access asd*: No such file or directory
ls: cannot access test: No such file or directory
ls: cannot access asd*: No such file or directory
下面是腳本:
#!/bin/bash
#rename a group of files with adding padding zero: gpRenameWithZero $1=filesToBeRenamed $2=newName $3=filetype: gpRenameWithZero \* newName_ jpg
#123 files -> length of number are 3 digits
numberOfDigits=$(ls $1| wc -l | xargs expr length)
#take extension from command line or take from filename
if [ $# -gt 2 ]; then
extension=$3
else
extension=$(ls -rt $1 | head -n 1 | rev | cut -d . -f1 | rev)
fi
#Preview
ls -rt $1 | cat -n | while read n f; do echo mv "$f" `printf "$2%0$numberOfDigits"d".$extension" $n`; done
read -p "Do you wish to rename [y/n]?" yn
case $yn in
[Yy]*) ls -rt $1 | cat -n | while read n f; do mv "$f" `printf "$2%0$numberOfDigits"d".$extension" $n`; done;;
[Nn]*) ;;
esac
我已經使用引號試過/雙引述vriables和參數,逃避/不逃避。
如何解決這種情況?還是有一個更簡單的腳本,它將文件重命名,新名稱和擴展名作爲參數)重命名多個文件。
[不要解析'ls'!](http://mywiki.wooledge.org/ParsingLs) – Biffen 2014-08-27 09:28:23
爲什麼不使用'rename'工具?例如,我有時使用'rename's// _/g「*'來刪除我的文件名中的空格 – Emilien 2014-08-27 09:30:35