以下腳本包含刪除以"._"和".pbb"的文件。
由于腳本是bash shell,所以在Windows上是不能運(yùn)行的唇牧,因此需要借助三方工具進(jìn)行運(yùn)行罕扎。我用的是cygwin,找到Installing Cygwin
直接下載對(duì)應(yīng)平臺(tái)安裝包丐重,根據(jù)提示默認(rèn)安裝完場(chǎng)即可腔召;
安裝完成后即可在cygwin中運(yùn)行bash shell;
將下面的腳本復(fù)制到.sh的文件中保存扮惦,將文件拖到cygwin窗口回車(chē)即可運(yùn)行臀蛛,運(yùn)行成功要求輸入指定的路徑,接著腳本會(huì)遍歷改路徑下所有的文件和文件夾崖蜜,找出隱藏文件和特定后綴的文件浊仆;
/cygdrive/c/
為Cygwin的映射;
handLog="/cygdrive/c/Users/Administrator/Desktop/pbbFileHand.log"
# handLog="pbbFileHand.log"
echo "操作日志:${handLog}"
touch "${handLog}"
hitFile="/cygdrive/c/Users/Administrator/Desktop/hitFile.txt"
# hitFile="hitFile.txt"
touch "${hitFile}"
echo " ------------------ `Date` ------------------ " >> "${handLog}"
echo " 命中文件:${hitFile}" >> "${handLog}"
echo " ------------------ `Date` ------------------" >> ${hitFile}
function recursionDir () {
# 傳入的路徑豫领,注意使用臨時(shí)變量抡柿,否則自動(dòng)視為生命周期中的全局變量
local dir=$1
if [[ -e "${dir}" ]]; then
# 注意文件路徑不對(duì)時(shí),文件為空等恐;路徑是目錄時(shí)洲劣,文件是特殊文件;
# 需要向下一級(jí)尋找课蔬,-a 獲取所有
# 文件名中有空個(gè)時(shí)囱稽,ls會(huì)換行處理;有"/"時(shí)购笆,ls會(huì)處理成":"
# 在使用"rm"時(shí)粗悯,系統(tǒng)對(duì)文件名中的"/"使用":"表示
originChar="[ ]"
targetChar="[^]"
for file in `ls -a "${dir}" | tr "${originChar}" "${targetChar}" `; do
dot="."
dotDot=".."
# 將"^"替換回" "
file=`echo ${file} | tr "[${targetChar}]" "[${originChar}]" `
if [[ ${file} == ${dot} ]]; then
continue
fi
if [[ ${file} == ${dotDot} ]]; then
continue
fi
# 不使用local,同for循環(huán)周期
filePath=${dir}/${file}
echo "文件 ${filePath}"
echo "${filePath}" >> "${handLog}"
if [[ -d "${filePath}" ]]; then
recursionDir "${filePath}"
else
deleteHideFile "${filePath}" "${file}"
# 如果存在.pbb文件就刪除
deletePBBFile "${filePath}.pbb"
fi
done
else
deleteHideFile "${dir}"
fi
}
function deleteHideFile () {
if [[ -f "$1" ]]; then
local fileName=$2
begin=`echo ${fileName:0:2}`
end=`echo ${fileName:0-4:4}`
prefix="._"
suffix=".pbb"
if [[ ${begin} == ${prefix} ]]; then
# if [[ "${end}" == "${suffix}" ]]
# then
# echo "begin: ${begin}"
# echo "end: ${end}"
echo "命中文件:$1"
echo "$1" >> "${hitFile}"
# 將有特殊符號(hào)的文件名當(dāng)著整個(gè)字符串傳入進(jìn)行刪除
rm "$1"
if [[ $? == 0 ]];
then
echo "$1 隱藏文件同欠,已移除" >> "${handLog}"
else
echo "$1 隱藏文件样傍,移除失敗" >> "${handLog}"
fi
# fi
fi
fi
}
function deletePBBFile () {
if [[ -e $1 ]]; then
echo "wc -c filename | awk '{print $1}'"
rm "$1"
if [[ $? == 0 ]];
then
echo "$1 PBB文件,已移除" >> "${handLog}"
else
echo "$1 PBB文件铺遂,移除失敗" >> "${handLog}"
fi
fi
}
echo "輸入指定的路徑:"
read tempDir
## 不能使用‘~’指定當(dāng)前用戶(hù)
# tempDir="/Users/niko/Documents/CSII/CSIICore_iOS.zip"
# cd "${tempDir}"
recursionDir "${tempDir}"