-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrestore_timestamp.sh
More file actions
118 lines (100 loc) · 4.93 KB
/
restore_timestamp.sh
File metadata and controls
118 lines (100 loc) · 4.93 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/bin/bash
# Script to restore file timestamp after manual editing (Linux version)
# Usage: ./restore_timestamp.sh <file> [reference_file]
# This script will restore the mtime of the first file to match the second file (or capture it first)
set -e # Exit on any error
# ANSI color codes for cross-platform compatibility
CYAN='\033[0;36m'
YELLOW='\033[1;33m'
GRAY='\033[0;90m'
NC='\033[0m' # No Color
# Function to show banner
show_banner() {
echo ""
echo -e "${CYAN} ██████╗ ██╗ ██╗ ██████╗ ███████╗████████╗██╗ ██╗██████╗ ██╗████████╗███████╗${NC}"
echo -e "${CYAN} ██╔════╝ ██║ ██║██╔═══██╗██╔════╝╚══██╔══╝██║ ██║██╔══██╗██║╚══██╔══╝██╔════╝${NC}"
echo -e "${CYAN} ██║ ███╗███████║██║ ██║███████╗ ██║ ██║ █╗ ██║██████╔╝██║ ██║ █████╗ ${NC}"
echo -e "${CYAN} ██║ ██║██╔══██║██║ ██║╚════██║ ██║ ██║███╗██║██╔══██╗██║ ██║ ██╔══╝ ${NC}"
echo -e "${CYAN} ╚██████╔╝██║ ██║╚██████╔╝███████║ ██║ ╚███╔███╔╝██║ ██║██║ ██║ ███████╗${NC}"
echo -e "${CYAN} ╚═════╝ ╚═╝ ╚═╝ ╚═════╝ ╚══════╝ ╚═╝ ╚══╝╚══╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚══════╝${NC}"
echo ""
echo -e "${YELLOW} 👻 Edit files without leaving timestamp traces 👻${NC}"
echo -e "${GRAY} https://logisek.com | info@logisek.com${NC}"
echo -e "${GRAY} https://github.com/Logisek/GhostWrite${NC}"
echo ""
}
# Function to show usage
usage() {
echo "Usage: $0 <file> [reference_timestamp_file]"
echo ""
echo "Examples:"
echo " $0 document.docx # Restore using captured timestamp"
echo " $0 document.docx original.docx # Restore using reference file's timestamp"
echo ""
echo "This script restores the file's modification and access time after editing."
echo "Note: Creation time (btime) cannot be reliably set on Linux with standard tools."
exit 1
}
# Check arguments
if [ $# -lt 1 ] || [ $# -gt 2 ]; then
usage
fi
TARGET_FILE="$1"
REFERENCE_FILE="${2:-}"
# Show banner
show_banner
echo "=== File Timestamp Restoration Script (Linux) ==="
# If reference file provided, use its timestamp
if [ -n "$REFERENCE_FILE" ]; then
if [ ! -f "$REFERENCE_FILE" ]; then
echo "Error: Reference file '$REFERENCE_FILE' does not exist"
exit 1
fi
REFERENCE_MTIME=$(stat -c %Y "$REFERENCE_FILE")
echo "Using timestamp from reference file: $REFERENCE_FILE"
echo "Reference modification time: $(date -d @$REFERENCE_MTIME)"
else
# Look for a .timestamp file
TIMESTAMP_FILE="${TARGET_FILE}.timestamp"
if [ -f "$TIMESTAMP_FILE" ]; then
REFERENCE_MTIME=$(cat "$TIMESTAMP_FILE")
echo "Using saved timestamp from: $TIMESTAMP_FILE"
echo "Saved modification time: $(date -d @$REFERENCE_MTIME)"
else
echo "Error: No reference file provided and no timestamp file found ($TIMESTAMP_FILE)"
echo "Please either:"
echo " 1. Provide a reference file: $0 $TARGET_FILE <reference>"
echo " 2. Create timestamp file first: echo \$(stat -c %Y '$TARGET_FILE') > '$TIMESTAMP_FILE'"
exit 1
fi
fi
# Check if target file exists
if [ ! -f "$TARGET_FILE" ]; then
echo "Error: Target file '$TARGET_FILE' does not exist"
exit 1
fi
# Get current timestamp
CURRENT_MTIME=$(stat -c %Y "$TARGET_FILE")
echo "Current modification time: $(date -d @$CURRENT_MTIME)"
echo ""
# Restore the timestamp (Linux format)
# Note: Linux cannot reliably set creation time (btime) with standard tools
echo "Restoring modification and access time..."
touch -d @$REFERENCE_MTIME "$TARGET_FILE"
# Verify
NEW_MTIME=$(stat -c %Y "$TARGET_FILE")
if [ "$NEW_MTIME" -eq "$REFERENCE_MTIME" ]; then
echo "✓ Successfully restored modification and access time: $(date -d @$NEW_MTIME)"
echo "Note: Creation time (btime) cannot be set on Linux with standard tools"
echo "File: $TARGET_FILE"
else
echo "✗ Failed to restore modification time."
echo "Expected: $(date -d @$REFERENCE_MTIME)"
echo "Current: $(date -d @$NEW_MTIME)"
exit 1
fi
# Clean up timestamp file if it exists and we're done
if [ -z "$REFERENCE_FILE" ] && [ -f "$TIMESTAMP_FILE" ]; then
rm "$TIMESTAMP_FILE"
echo "Cleaned up temporary timestamp file: $TIMESTAMP_FILE"
fi