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
|
#!/bin/bash
# 從 /etc/passwd 文件中讀取所有用戶名和主目錄路徑
while IFS=: read -r username _ _ _ _ home_dir _; do
if [ -d "$home_dir" ]; then
# 使用 stat 命令獲取主目錄資訊
stat_output=$(stat "$home_dir")
birth_time=$(echo "$stat_output" | grep 'Birth' | awk '{print $2 " " $3}')
if [ -n "$birth_time" ]; then
formatted_time=$(date -d "$birth_time" +'%Y-%m-%d %H:%M:%S')
echo "$(hostname),$username,$formatted_time"
else
birth_time=$(echo "$stat_output" | grep 'Change' | awk '{print $2 " " $3}')
if [-n "$birth_time"]; then
formatted_time=$(date -d "$birth_time" +'%Y-%m-%d %H:%M:%S')
else
echo "$(hostname),$username,-"
fi
fi
else
echo "$(hostname),$username,-"
fi
done < /etc/passwd
|