86 lines
2.5 KiB
Bash
Executable File
86 lines
2.5 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
set -e
|
|
function helpexit() {
|
|
echo Delete files with a progress bar.
|
|
echo This command is always recursive to folders!
|
|
echo '-f Dont ask questions'
|
|
echo '--nc Dont count files first'
|
|
exit
|
|
}
|
|
|
|
FORCE=0
|
|
FOLDERS=( )
|
|
for ((i=1; i<=${#@}; i++)) {
|
|
[[ "${!i}" = "-h" ]] && helpexit
|
|
[[ "${!i}" = "--help" ]] && helpexit
|
|
[[ "${!i}" = "-f" ]] && { FORCE=1; RMFORCE="-f"; continue; }
|
|
[[ "${!i}" = "--nc" ]] && { NOCOUNT=1; continue; }
|
|
[[ "${!i}" = "-"* ]] && helpexit
|
|
FOLDERS+=( "${!i%/}" )
|
|
}
|
|
[[ "${#FOLDERS[@]}" -eq 0 ]] && helpexit
|
|
|
|
function listfiles() {
|
|
while IFS= read -r -d $'\0' line; do
|
|
files=$((files+1))
|
|
printf "\r%02d:%02d:%02d %d" $(($SECONDS/3600)) $(($SECONDS%3600/60)) $(($SECONDS%60)) $files
|
|
done < <(find "$@" \( -type f -or -type l \) -print0)
|
|
printf "\r"
|
|
}
|
|
function deletefiles() {
|
|
i=0
|
|
while IFS= read -r -d $'\0' line; do
|
|
i=$((i+1))
|
|
[[ $files -ne 0 ]] && percent=$((200*$i/$files % 2 + 100*$i/$files))
|
|
printf "\r%02d:%02d:%02d %6d/%d %3d%% %s\033[0K" \
|
|
$(($SECONDS/3600)) $(($SECONDS%3600/60)) $(($SECONDS%60)) $i $files "$percent" "$line"
|
|
rm "$line"
|
|
done < <(find "$@" \( -type f -or -type l \) -print0)
|
|
printf "\n"
|
|
}
|
|
function listfolders() {
|
|
while IFS= read -r -d $'\0' line; do
|
|
folders=$((folders+1))
|
|
printf "\r%02d:%02d:%02d %d" $(($SECONDS/3600)) $(($SECONDS%3600/60)) $(($SECONDS%60)) $folders
|
|
done < <(find "$@" -type d -print0)
|
|
printf "\r"
|
|
}
|
|
function deletefolders() {
|
|
i=0
|
|
while IFS= read -r -d $'\0' line; do
|
|
i=$((i+1))
|
|
[[ $folders -ne 0 ]] && percent=$((200*$i/$folders % 2 + 100*$i/$folders))
|
|
printf "\r%02d:%02d:%02d %6d/%d %3d%% %s\033[0K" \
|
|
$(($SECONDS/3600)) $(($SECONDS%3600/60)) $(($SECONDS%60)) $i $folders "$percent" "$line"
|
|
rm -r "$line"
|
|
done < <(find "$@" -depth -type d -print0)
|
|
printf "\n"
|
|
}
|
|
|
|
# return line wrapping
|
|
trap "printf '\033[?7h'" 1 9 15
|
|
files=0
|
|
folders=0
|
|
if [[ "$NOCOUNT" -ne 1 ]]; then
|
|
echo Listing files in "${FOLDERS[@]}" ...
|
|
# stop line wrapping
|
|
printf '\033[?7l'
|
|
listfiles "${FOLDERS[@]}"
|
|
echo Prepared to delete $files files
|
|
fi
|
|
# stop line wrapping
|
|
printf '\033[?7l'
|
|
if [[ $FORCE -eq 0 ]]; then
|
|
echo '<ctrl-c> to quit'
|
|
read foo
|
|
fi
|
|
deletefiles "${FOLDERS[@]}"
|
|
if [[ "$NOCOUNT" -ne 1 ]]; then
|
|
listfolders "${FOLDERS[@]}"
|
|
echo Removing remaining $folders folders
|
|
fi
|
|
deletefolders "${FOLDERS[@]}"
|
|
# return line wrapping
|
|
printf '\033[?7h'
|