2015-11-23 68 views
-1
[email protected]# ./removedott.sh 
Mon Nov 23 10:07:04 EST 2015 - Renaming tester.xml.P.T extension to xml 
mv: `tester.xml.P.T' and `tester.xml' are the same file 

removedott.sh:擊:重命名文件擴展名,收到錯誤

if [[ $(find . -type f -name "*.T" | wc -l) -gt 0 ]] 
     then 
       for f in *.T 
         do 
           echo "`date` - Renaming $f extension to xml" 
           y=${f%%.*} 
           mv -v -- "$f" "${y}.xml" 
           done 
fi 

我不知道爲什麼這個錯誤就要到了,我知道該文件是一樣的..這就是整個目的..我只想重命名它。

+3

你在目錄中是否有'tester.xml'文件? (此外''find'測試可能更好地替換爲僅僅啓用'nullglob'和/或測試'* .T'作爲循環中'$ f'的值。) –

+0

在目錄I中: touch tester .ABc.T 然後運行該腳本,在第一次傳遞時刪除.abc.T並將其重命名爲tester.xml。 如果我然後mv tester.xml到tester.abc.T並再次運行腳本,那當我得到錯誤 – sgwrist

+2

如果它已經存在與你想要的名稱,你知道它,爲什麼你想要重命名它?我聞到[XY問題](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – tripleee

回答

0

所有這些解決方案,甚至礦山工作都很好。這最終成爲在重命名期間影響文件的主機上的進程。 Linux在此期間找不到該文件。

感謝您的幫助。

0

這不是一個答案,爲什麼你mv不工作,但做同樣的事情劇本只是另一個版本:

#!/bin/bash 
# Renames ".T" extensions to ".xml" extensions for files in the current 
# directory and subdirectories 
for file in $(find . -type f -name "*.T"); do 
    echo "$(date) - Renaming $file extension to .xml" 
    mv -v $file $(echo $file | sed 's/\.T$/\.xml/g') 
done 

這對我的作品。希望能幫助到你!

+0

不是一個安全的解決方案(不能正確處理帶有空格或shell水平字符的文件)。有關詳細信息,請參見[爲什麼不用'for for]讀取行(http://mywiki.wooledge.org/DontReadLinesWithFor)。 –