Created
July 23, 2020 09:39
-
-
Save pd12bbf7608ae1/df9f67a4333dd2b39531e9b9aa06e781 to your computer and use it in GitHub Desktop.
文件删除操作
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
dir="$HOME/Documents/" | |
cd "$dir" | |
info=$(df -h "$dir") | |
free=$(echo "$info" | awk '{print $4}' | sed -e "1d") | |
total=$(echo "$info" | awk '{print $2}' | sed -e "1d") | |
printf "总空间:\t${total}\t剩余空间:\t${free}\n\n" | |
printf "目录详情:\n" | |
if [[ -z $(ls -A) ]]; then | |
echo "目录为空!" | |
exit 0 | |
fi | |
j=0 | |
printf "编号\t大小\t文件或文件夹名\n" | |
for name in *; do # 遍历文件名 | |
j=$((j+1)) | |
size=$(du "$name" -ah --max-depth=0 | sed -e "s/[[:blank:]].*//g") | |
printf "${j}\t${size}\t${name}\n" | |
done | |
printf "\n" | |
echo "请输入需要删除的文件或文件夹的编号(10秒等待,0为删除所有,其他退出):" | |
read -t 10 input | |
result=$(echo "$input" | grep "^[[:digit:]]*$") | |
if [[ -z "$result" || "$input" -lt "0" ]]; then # 退出 | |
echo "退出" | |
exit 0 | |
fi | |
if [[ "$input" -eq "0" ]]; then # 删除所有 | |
echo "所有文件删除中..." | |
rm * -rf | |
else | |
j=0 | |
for name in *; do | |
j=$((j+1)) | |
if [[ "$j" -eq "$input" ]]; then | |
echo "${name} 删除中.." | |
rm "$name" -rf | |
break | |
fi | |
done | |
fi | |
echo "完成" | |
info=$(df -h "$dir") | |
free=$(echo "$info" | awk '{print $4}' | sed -e "1d") | |
total=$(echo "$info" | awk '{print $2}' | sed -e "1d") | |
printf "总空间:\t${total}\t剩余空间:\t${free}\n\n" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment