-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisk-space-analysis.sh
63 lines (55 loc) · 2.17 KB
/
disk-space-analysis.sh
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
#!/usr/bin/env bash
# This is a simple program made by me which prints the size of a given directory or file.
# It has error handling and all that fancy stuff, so I decided to add it into this repo.
# Color variables (error (red), info (green), texto (gray), fim (reset))
ERROR_="\033[38;5;161m"
INFO_="\033[38;5;47m"
TEXTO_="\033[38;5;247m"
FIM_="\033[0m"
# Defining a flag argument "-d/--directory"
case "$1" in
-d|--directory) ANSWER_="$2" ;;
"") ;;
*) echo -e "\n${ERROR_}[ERROR]: Opção \"$1\" não encontrado! Saindo...${FIM_}" ; exit 1 ;;
esac
# Checking if the first command argument is empty
if [[ -z "$1" ]]; then
echo -e "${INFO_} + Qual o disco que deseja analisar?${FIM_}"
read -erp "$(echo -e $INFO_" >"$FIM_) " ANSWER_
# Read includes xterm ansi colors and tab completion support
fi
# The "-e" option in the bash test condition ( "[ ]" ) which
# checks if the file exists, regardless of whether it is a directory, file, symlink, node etc..
if [ ! -e "$ANSWER_" ]; then
echo -e "\n${ERROR_}[ERROR]: O arquivo \"$ANSWER_\" não encontrado! Saindo...${FIM_}\n"
exit 1
fi
# Main checking function. Identifying if the input is a file or directory
# For file it'll use "stat" and for directory "du"
CHECK_SIZE(){
if [[ ! -d "$ANSWER_" ]]; then
local FILE_SIZE=$(
stat -c '%s MB NN NNN+ Group/Ownership:FF %G / %U' "$ANSWER_" | sed 's/NN/\n/' |\
sed ''/NNN/s//`printf "\033[38;5;247m"`/'' |\
sed ''/FF/s//`printf "\033[0m"`/''
)
echo -e "\n${TEXTO_}\
${INFO_}>>> Information:${FIM_}${FIM_}\n \
${TEXTO_}+ File:${FIM_} $ANSWER_\n \
${TEXTO_}+ File size:${FIM_} $FILE_SIZE"
else
local FILE_SIZE=$(du -sh "$ANSWER_" | awk '{print $1}')
echo -e "\n\
${INFO_}>>> Information:${FIM_}\n \
${TEXTO_}+ Directory:${FIM_} $ANSWER_\n \
${TEXTO_}+ File size:${FIM_} $FILE_SIZE\n\
$(stat -c ' NN+ Group/Ownership:FF %G / %U' "$ANSWER_" |\
sed ''/NN/s//`printf "\033[38;5;247m"`/'' |\
sed ''/FF/s//`printf "\033[0m"`/''\
)"
# The above sed command replaces the string "NN" and "FF" with bash xterm ansi colors. (giving "+ Group/Ownership:" string a gray color)
fi
}
CHECK_SIZE
# Unsetting varibles which were not set as local
unset ERROR_ INFO_ TEXTO_ FIM_ ANSWER_