Repository navigation
Expand file tree
/
Copy pathrmf.sh
More file actions
83 lines (71 loc) · 2.24 KB
/
Copy pathrmf.sh
File metadata and controls
83 lines (71 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/bash
# Description: Removes files from disk using a custom list of files
# Author: Victor Quebec
# Date: Dec 9, 2020
# check if the file supplied as a command-line argument exists
if [[ -f "$1" && $# != 0 ]]; then
# user warning
read -r -p "$(tput setaf 3)Running this script will remove files on your computer. Are you sure? [Y/N] " input
case $input in
[yY][eE][sS]|[yY])
clear;;
[nN][oO]|[nN])
exit;;
*)
echo "Invalid input!"
exit 1;;
esac
# backup the value of IFS variable
BKPIFS=$IFS
# set new value to IFS variable
IFS=$(echo -en "\n\b")
# sort the contents of the loaded file list and remove duplicate entries
# sort is optional
FILES=$(sort ${1} | uniq)
# initilizing variables to echo correct message(s).
# E.g. script doesn't echo file size even when file(s) exist(s)
# because [[ $ncount = 0 ]] is false.
(( fcount = 0 ))
(( fsize = 0 ))
(( ncount = 0 ))
(( total = 0 ))
(( ycount = 0 ))
for file in $FILES; do
(( fcount++ ))
if [ -f $file ]; then
# use 'cut' command because IFS is busy...
fsize=`du -s $file | cut -f1`
total=$(( $total + $fsize ))
rm $file
(( ycount++ ))
else
string="$string\n$file"
(( ncount++ ))
fi
done
echo "Number of supplied files: $fcount"
kb=$(awk -v total=$total 'BEGIN { print total * 1.024 }') # bash works with integers only!
if [ $ncount = 0 ]; then
echo "Total removed: $total KiB ($kb kB) in $ycount files"
elif [[ $ycount > 0 && $ncount > 0 ]]; then
echo "Total removed: $total KiB ($kb kB) in $ycount files"
echo "Number of null files: $ncount"
read -r -p "Do you want to see the null files? [Y/N] " input
case $input in
[yY][eE][sS]|[yY])
echo -e "$string\n";;
[nN][oO]|[nN])
exit;;
*)
echo "Invalid input!"
exit 1;;
esac
else
echo "None of the files exists!"
fi
IFS=$BKPIFS
else
echo "No file to process! Exiting..."
sleep 2
exit 1
fi