我想傳遞一個文件列表,包括通配符文件和我想要刪除的目錄,但檢查它們是否在刪除之前先存在。如果它們被刪除,則通知該目錄已被刪除,而不是目錄中的每個單獨文件。即如果我刪除/root/*.tst只是說,「我刪除* .tst」。Bash腳本:一種更好的方法來刪除帶有通配符的文件和目錄列表
#!/bin/bash
touch /boot/{1,2,3,4,5,6,7,8,9,10}.tst
touch /root/{1,2,3,4,5,6,7,8,9,10}.tst
mkdir /root/tmpdir
#setup files and directories we want to delete
file=/boot/*.tst /root/*.tst /root/tmpdir
for i in $file; do
if [[ -d "$file" || -f "$file" ]]; then #do I exist
rm -fr $i
echo removed $i #should tell me that I removed /boot/*.tst or /root/*.tst or /root/tmpdir
else
echo $i does not exist # should tell me if /boot/*.tst or /root/*.tst or /root/tmpdir DNE
fi
done
我似乎無法使單或雙引號或轉義的任何組合*使上述做我想做的事。
這隻適用於不包含空格的模式(例如,'rmglob'/ root/tmp dir''不起作用)。 – chepner 2014-09-12 23:59:50
這幾乎完全是我正在尋找通知刪除的東西。你將如何修改上述內容以確保捕獲帶有空格的文件? – vlord 2014-09-15 15:07:26