Um pequeno script de linha de comando, então!
#!/bin/bash
# Checked with https://www.shellcheck.net/
set -o nounset
user= # user to clean
do_it= # nonempty to actually run remove commands
cmdline_user= # user given on cmdline (if any)
# ---
# Option processing
# ---
processOptions() {
# We use bash regular expressions, which are "grep" regular expressions; see "man grep"
# or "man 7 regex"
# In that case, the string on the right MUST NOT be enclosed in single or double quotes,
# otherwise it becomes a literal string
local param= # Current parameter
local use_user= # Set to nonempty if a user argument is expected in the next PARAM
local unknown= # Accumulates unknown PARAMS
local print_help= # Set to nonempty if help requested
for param in "$@"; do
# Process arguments that are separate from their option
if [[ -n $use_user ]]; then
cmdline_user=$param
use_user=""
continue
fi
# Process option
if [[ $param == '--do' ]]; then
# set global variable
do_it=YES
continue
fi
if [[ $param =~ --user(=.+)? ]]; then
if [[ $param =~ --user=(.+)? ]]; then
# set global variable
cmdline_user=$(echo "$param" | cut --delimiter="=" --fields=2)
else
# expecting value
use_user=1
fi
continue
fi
if [[ $param == '--help' || $param == '-h' ]]; then
print_help=1
break
fi
# if we are here, we encountered something unknown in PARAM
# if UNKNOWN is already set, add a comma for separation
if [[ -n $unknown ]]; then
unknown="$unknown,"
fi
unknown="${unknown}${param}"
done
if [[ -n $unknown ]]; then
echo "Unknown parameters '$unknown'" >&2
print_help=1
fi
if [[ -n $print_help ]]; then
echo "--user=... to explicitly give user to clean" >&2
echo "--do to actually perform operations instead of just printing them" >&2
exit 1
fi
}
processOptions "$@"
# The user to handle is either myself or it comes from the command line
if [[ -n $cmdline_user ]]; then
user=$cmdline_user
else
user=$(whoami)
fi
# Does the user actually exist? If so get the home dir
if ! record=$(getent passwd "$user"); then
echo "Could not get passwd entry of user '$user' -- exiting" >&2
exit 1
fi
zehome=$(echo "$record" | cut -f6 -d:)
if [[ ! -d "$zehome" ]]; then
echo "Home directory of user '$user' is '$zehome' but that directory does not exist -- exiting" >&2
exit 1
fi
# ********
# Path to actually clean out, configure as needed
# ********
clean[0]="$zehome/.cache/thumbnails"
clean[1]="$zehome/.kde/share/apps/okular/docdata"
# Run operations. Depending on "do_it", it's done or not!
for dir in "${clean[@]}"; do
if [[ ! -d "$dir" ]]; then
echo "(Directory '$dir' does not exist or may not be a directory -- skipping that directory)" >&2
else
if [[ -z $do_it ]]; then
echo "Would run: /bin/rm -r \"$dir\""
else
echo "Running: /bin/rm -r \"$dir\""
/bin/rm -r "$dir"
fi
fi
done