Skip to content

Instantly share code, notes, and snippets.

@filviu
Created April 23, 2024 08:34
Show Gist options
  • Save filviu/98eca920870c5297137894cbf80efcd7 to your computer and use it in GitHub Desktop.
Save filviu/98eca920870c5297137894cbf80efcd7 to your computer and use it in GitHub Desktop.

On CentOS 7, the last command doesn't support the --since option to filter users who have logged in within the past two weeks. However, you can use some other commands and scripts to achieve this. Here's a Bash script that can help you find users who have logged in within the past two weeks.

Save this script to a file, for example, show_recent_logins.sh, and make it executable with the following command:

chmod +x show_recent_logins.sh

Then, you can run the script to display users who have logged in within the past two weeks.

The script calculates the date two weeks ago, extracts login data using the last command, and filters the results to display users who logged in after that date.

#!/bin/bash
# Calculate the date two weeks ago in the format 'YYYY-MM-DD'
two_weeks_ago=$(date -d "2 weeks ago" "+%Y-%m-%d")
# Get the list of user logins using the 'last' command
login_data=$(last)
# Extract usernames and login timestamps
user_logins=$(echo "$login_data" | awk '{print $1,$5}')
# Loop through the logins and filter users who logged in within the past two weeks
while read -r line; do
username=$(echo "$line" | awk '{print $1}')
login_time=$(echo "$line" | awk '{print $2}')
# Check if the login time is after two weeks ago
if [[ "$login_time" > "$two_weeks_ago" ]]; then
echo "User $username last logged in on $login_time"
fi
done <<< "$user_logins"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment