[英]bash script, get name of subdirectory
我正在制作一個 bash 腳本來刪除舊的時移快照,然后創建一個新快照,然后更新我的系統。 我被困在如何從 timeshift 目錄中提取快照目錄名稱。
#!/bin/bash
#We need to figure out which snap is older than the other without explicitly typing its name...
#Below, the names are explicitly typed!
#snaps=/timeshift/snapshots
snap1=/timeshift/snapshots/2020-12-29_16-43-41
snap2=/timeshift/snapshots/2021-01-01_09-59-12
if [ $snap1 -ot $snap2 ]
then
snap1=2020-12-29_16-43-41
sudo timeshift --delete --snapshot $snap1
if sudo timeshift --create --comments "Weekly update"; then
sudo pacman -Syu
else
echo "Something didn't work"
fi
fi
快照名稱不會是 static。 我怎樣才能讓這個腳本每次都讀取新名稱而無需手動添加它?
如果/timeshift/snapshots
中只有兩個文件,並且它們都是由 timeshift 創建的文件夾,並且它們的名稱不能包含任何類型的空格,那么您可以使用以下命令刪除最舊的文件:
rm -fr `ls -tr /timeshift/snapshots | head -1`
如果只有一個而不是兩個,那么您必須對以下內容更具創意,只要它們的名稱不同,就會刪除較舊的頭部和尾部。 我假設您不想在進行下一次備份之前意外刪除唯一剩余的備份。
cd /timeshift/snapshots
snap1=`ls -tr | head -1`
snap2=`ls -tr | tail -1`
if [ "$snap1" != "$snap2" ]; then
rm -fr "$snap1"
fi
如果需要更高的魯棒性,引號允許空格。
這是我嘗試了所有方法后采用的解決方案。 我不會標記其他答案,因為我發現評論答案(現已刪除)更有幫助。 不過還是謝謝。
#!/bin/bash
snaps=(/timeshift/snapshots/*)
if [[ ${snaps[0]} -ot ${snaps[1]} ]]
then
#$snaps becomes $snaps[0] here
sudo rm -fr $snaps && echo "$snaps has been deleted."
echo "Remaining snapshot:" && ls /timeshift/snapshots
if sudo timeshift --create --comments "weekly update"
then
sudo pacman -Syu
fi
else
echo "Something went wrong..."
fi
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.