This commit is contained in:
2025-10-06 19:16:59 +03:00
commit 5a7ff209bb
2 changed files with 96 additions and 0 deletions

BIN
lab1/backalor-4-2020.docx Normal file

Binary file not shown.

96
lab2/fdir.sh Normal file
View File

@@ -0,0 +1,96 @@
#!/usr/bin/env bash
show_help() {
cat <<-EOF
Usage: $(basename "$0") [OPTIONS] <ROOT_DIR> <DATE> [COMMAND...]
Finds directories inside ROOT_DIR that were modified before DATE
and executes provided COMMAND inside each of them.
Just lists directories if COMMAND is not provied.
Options:
-h Show help
-v Verbose output
DATE formats:
Accepts any date format understood by the 'date' command.
Examples:
2025-10-05
"2025-10-05 12:00"
"Oct 5 2025"
"yesterday"
"2 weeks ago"
Examples:
$(basename "$0") /var/log 2025-10-05
$(basename "$0") /tmp "2025-10-05 18:00" pwd
EOF
}
VERBOSE=0
while getopts "hv" opt; do
case $opt in
h)
show_help
exit 0
;;
v)
VERBOSE=1
;;
*)
echo "Unknown arguments, use -h for help."
exit 1
;;
esac
done
shift $((OPTIND - 1))
show_error() {
echo $* >&2
echo "Use -h for help..."
}
log() {
if [[ "$VERBOSE" -eq 1 ]]; then
echo $*
fi
}
MIN_ARGS=2
if [ $# -lt $MIN_ARGS ]; then
show_error "Not enough arguments! Expected $MIN_ARGS, got $#"
exit 2
fi
ROOT_DIR="$1"
DATE_STR="$2"
if [[ ! -d "$ROOT_DIR" ]]; then
show_error "Provided ROOT_DIR (\"$ROOT_DIR\") does not exist!"
exit 2
fi
log "Provided ROOT_DIR: $ROOT_DIR"
if ! date -d "$DATE_STR" >/dev/null 2>&1; then
show_error "Invalid date!"
exit 2
fi
DATE=$(date -d "$DATE_STR" +%FT%T)
log "Provided DATE: $DATE_STR"
log "Parsed date: $DATE"
shift 2
COMMAND="$*"
log "Provided COMMAND: $COMMAND"
FIND_CMD=(find "$ROOT_DIR" -type d ! -newermt "$DATE")
if [[ -n "$COMMAND" ]]; then
#FIND_CMD+=(-exec $COMMAND \;)
FIND_CMD+=(-exec bash -c "cd '{}' && $COMMAND" \;)
fi
log "Final command: ${FIND_CMD[*]}"
"${FIND_CMD[@]}"