有提供給你幾個比較優雅的選擇。
一個將rm
包裝在一個函數中,該函數檢查是否有任何要刪除文件夾中的文件。你可以使用ls
,以檢查是否有符合通配符的任何文件,按照this question:
#!/usr/bin/env bash
rm_check() {
if ls *."${1}" 1> /dev/null 2>&1; then
rm *."${1}"
echo "All *.${1} files have been deleted"
else
echo "No *.${1} files were found"
fi
}
read -p "are you sure you want to delete the files? Y/N " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
rm_check torrent
rm_check zip
rm_check deb
fi
這個版本是不錯的,因爲它擁有一切奠定了最初計劃的方式。
在我看來,一個更清潔的版本只能查看與您的模式相匹配的文件。正如我在評論中建議,你可以用一個單一的find
命令做到這一點:
#!/usr/bin/env bash
read -p "are you sure you want to delete the files? Y/N " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
find -name '*.torrent' -o -name '*.zip' -o -name '*.deb' -delete
echo "all those files have been deleted............."
fi
這種方法使你的腳本很短。這種方法唯一可能的缺點是它不會報告缺少哪些文件類型。
你知道'find'命令嗎? –
沒有。我還是新手bash – mots
類似於'find -name'* .torrent'-o -name'* .zip'-o -name'* .deb'-delete'會完全繞過你的問題,但可能不會你想要什麼,因爲它沒有報告什麼時候沒有給定類型的文件開始。 –