-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnap.sh
56 lines (45 loc) · 1.61 KB
/
snap.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
#!/bin/bash
# Output file for the snapshot
output_file="filesystem_snapshot_$(date +%Y%m%d_%H%M%S).txt"
# Function to process files
process_file() {
local file="$1"
# Skip files ending with .sum
if [[ "$file" == *.sum ]]; then
return
fi
# Add file separator and path to output
echo -e "\n=== File: $file ===" >> "$output_file"
# Check if file is binary
if [[ -f "$file" ]] && ! [[ -x "$file" ]] && file "$file" | grep -q "text"; then
# For text files, add content
echo -e "\n--- Content ---" >> "$output_file"
cat "$file" >> "$output_file"
else
# For binary files, just note that it's binary
echo -e "\n[Binary file]" >> "$output_file"
fi
}
# Main function to traverse directory
traverse_directory() {
local dir="$1"
# Find all files and directories, excluding .git
find "$dir" -type f -o -type d | while read -r item; do
# Skip .git directories and their contents
if [[ "$item" == */.git/* ]] || [[ "$item" == */.git ]]; then
continue
fi
if [[ -f "$item" ]]; then
process_file "$item"
elif [[ -d "$item" ]]; then
echo -e "\n=== Directory: $item ===" >> "$output_file"
fi
done
}
# Initialize output file with header
echo "File System Snapshot - Generated on $(date)" > "$output_file"
echo "Current working directory: $(pwd)" >> "$output_file"
echo -e "\n----------------------------------------" >> "$output_file"
# Start traversal from current directory
traverse_directory "."
echo "Snapshot has been generated in: $output_file"