Skip to content

Commit 7c39bce

Browse files
authored
Create pathlog.sh
1 parent 2bb8322 commit 7c39bce

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

pathlog.sh

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#!/bin/bash
2+
3+
# Create detailed log and file audit for a specified path
4+
# Usage: $0 [sudo] [SCAN_PATH] [--logto /custom/path/or/filename]
5+
6+
# Default hash algorithm
7+
HASH_ALGORITHM="md5sum" # Change to 'sha256sum' for SHA-256
8+
9+
# Function to log error messages
10+
log_error() {
11+
echo "Error: $1"
12+
exit 1
13+
}
14+
15+
# Function to calculate hash for a file
16+
calculate_hash() {
17+
local file=$1
18+
$HASH_ALGORITHM "$file" 2>/dev/null | awk '{print $1}'
19+
}
20+
21+
# Function to parse arguments and set options
22+
parse_args() {
23+
while [[ "$#" -gt 0 ]]; do
24+
case "$1" in
25+
--logto)
26+
shift
27+
CUSTOM_PATH_LOG="$1"
28+
if [[ -z "$CUSTOM_PATH_LOG" ]]; then
29+
log_error "Missing value for --logto flag."
30+
fi
31+
;;
32+
*)
33+
if [[ -z "$SCAN_PATH" ]]; then
34+
SCAN_PATH="$1"
35+
else
36+
log_error "Unexpected argument: $1"
37+
fi
38+
;;
39+
esac
40+
shift
41+
done
42+
}
43+
44+
# Initialize variables
45+
SCAN_PATH=""
46+
CUSTOM_PATH_LOG=""
47+
parse_args "$@"
48+
49+
# Set default scan path if not provided
50+
SCAN_PATH="${SCAN_PATH:-/}"
51+
52+
# Check if the path exists and is a directory
53+
[ -d "$SCAN_PATH" ] || log_error "Path '$SCAN_PATH' does not exist or is not a directory."
54+
55+
# Determine the user's home directory
56+
if [ -n "$SUDO_USER" ]; then
57+
USER_HOME=$(eval echo "~$SUDO_USER")
58+
else
59+
USER_HOME="$HOME"
60+
fi
61+
62+
# Prepare the default log directory and filename
63+
LOG_DIR="$USER_HOME/szmelc/logs/pathlog"
64+
mkdir -p "$LOG_DIR" || log_error "Failed to create log directory '$LOG_DIR'."
65+
66+
DATETIME=$(date '+%Y-%m-%d_%H-%M-%S')
67+
SANITIZED_PATH=$(echo "$SCAN_PATH" | sed 's/[\/:]/_/g')
68+
OUTPUT_FILE="$LOG_DIR/${SANITIZED_PATH}-${DATETIME}.txt"
69+
70+
# Create or overwrite the output file
71+
echo "Creating log file at $OUTPUT_FILE..."
72+
: > "$OUTPUT_FILE"
73+
74+
# Add metadata to the log
75+
echo "# Pathlog $DATETIME" >> "$OUTPUT_FILE"
76+
echo "# Scanned Path: $SCAN_PATH" >> "$OUTPUT_FILE"
77+
echo "# Hash Algorithm: $HASH_ALGORITHM" >> "$OUTPUT_FILE"
78+
echo >> "$OUTPUT_FILE"
79+
80+
# Append tree output to the log if available
81+
if command -v tree &>/dev/null; then
82+
echo "# Directory structure:" >> "$OUTPUT_FILE"
83+
tree "$SCAN_PATH" >> "$OUTPUT_FILE" 2>/dev/null
84+
else
85+
echo "Warning: 'tree' command not found. Skipping directory structure." >> "$OUTPUT_FILE"
86+
fi
87+
88+
echo >> "$OUTPUT_FILE"
89+
90+
# Process files recursively and log path/hash
91+
echo "Processing files in '$SCAN_PATH'..."
92+
find "$SCAN_PATH" -type f -print | while read -r FILE; do
93+
HASH=$(calculate_hash "$FILE")
94+
if [ -n "$HASH" ]; then
95+
echo "$FILE | $HASH" >> "$OUTPUT_FILE"
96+
else
97+
echo "Failed to hash: $FILE" >> "$OUTPUT_FILE"
98+
fi
99+
done
100+
101+
# Handle custom log path if specified
102+
if [ -n "$CUSTOM_PATH_LOG" ]; then
103+
if [ -d "$CUSTOM_PATH_LOG" ]; then
104+
CUSTOM_PATH_LOG="${CUSTOM_PATH_LOG%/}/${SANITIZED_PATH}-${DATETIME}.txt"
105+
fi
106+
107+
echo "Moving log file to custom location: $CUSTOM_PATH_LOG..."
108+
cp "$OUTPUT_FILE" "$CUSTOM_PATH_LOG" || log_error "Failed to move log file to '$CUSTOM_PATH_LOG'."
109+
OUTPUT_FILE="$CUSTOM_PATH_LOG"
110+
fi
111+
112+
echo "Log saved to $OUTPUT_FILE"

0 commit comments

Comments
 (0)