Geody Labs


# Main Index: Debian Linux Magic Spells Cheat Sheet (one liners, how to, tips and tricks)

# Time

Set date and time:
date MMDDhhmmYYYY.ss

Set date from NTP (SNTP) server:
apt-get install ntpdate
ntpdate SERVER  # set the clock
ntpdate -q SERVER  # query only for the time, don't set the clock

SNTP servers:
pool.ntp.org # NTP Pool Project
ntp.ubuntu.com # Ubuntu
ntp1.inrim.it # INRiM, Torino, Italy
ntp.univ-lyon1.fr # University of Lyon, Lyon, France
vega.cbk.poznan.pl # Astrogeodynamic Observatory, Borowiec, Poland
bitsy.mit.edu # MIT, Cambridge, Massachusetts, USA
bonehed.lcs.mit.edu # MIT, Cambridge, Massachusetts, USA
clock.isc.org # Internet Systems Consortium, Palo Alto, California, USA

Start a NTP server:
apt-get install ntp-refclock (or another ntp* package)
jed /etc/ntp.conf
/etc/init.d/ntp-server restart

Date tokens:
%% : %
%a : Weekday's abbreviated name (for example, Mon)
%A : Weekday's full name (for example, Monday)
%b : Month's abbreviated name (for example, Jan; same as %h)
%B : Month's full name (for example, January)
%C : Century (for example, 21)
%d : Day of month (for example, 01)
%e : day of month, space padded; same as %_d
%F : Full date formatted as full year - month - day (same as %Y-%m-%d )
%g : last two digits of year of ISO week number (see %G)
%G : year of ISO week number (see %V); normally useful only with %V
%H : Hour in 24 hours format with padded 0 (00-23)
%I : Hour in 12 hours format with padded 0 (01-12)
%j : Day of year with padded 0 (001-366)
%k : Hour in 24 hours format with padded space ( 0-23)
%l : Hour in 12 hours format with padded space ( 1-12)
%m : Month with padded 0 (01-12)
%M : Minute with padded 0 (00-59)
%n : Newline
%N : Nanoseconds (000000000-999999999)
%p : AM or PM, uppercase
%P : AM or PM, lowercase
%R : 24-hour hour and minute (same as %H:%M )
%s : Seconds since Linux Epoch (1970-01-01 00:00:00 UTC)
%S : Second with padded 0 (00-60)
%t : Tab
%T : Time (same as %H:%M:%S )
%u : ISO Day of week (1-7: 1 is Monday, 7 is Sunday)
%U : Week number of year, with Sunday as first day of week and padded 0 (00-53)
%V : ISO Week number of the year, with Monday as first day of week and padded 0 (01-53)
%w : Day of week (0-6: 0 is Sunday, 6 is Saturday)
%W : ISO Week number of year, with Monday as first day of week and padded 0 (00-53)
%y : Last two digits of year (00-99)
%Y : Full Year
%z : Timezone as hhmm (e.g., -0200)
%:z : Timezone as hh:mm (e.g., -02:00)
%Z : Timezone's abbreviated name (e.g., CET)

Show current date and time:
date
date '+%a %d %b %Y %H:%M:%S %Z (UTC%:z)' # show day of the week, day of the month, month (short format), year (full digits), hour, minutes, seconds, timezone. Example: Wed 16 Sep 2020 21:18:15 CEST (UTC+02:00)
echo "$(date '+%a %d %b %Y %H:%M:%S %Z (UTC%:z)')"; # echo day of the week, day of the month, month (short format), year (full digits), hour, minutes, seconds, timezone

Show information about your local time:
timedatectl status

Show System Time Zone
cat /etc/timezone

Linux Epoch (1 January 1970 00:00:00)
date +"%s" # Timestamp: seconds from Epoch to current time
date -d "2000-01-01" +"%s" # Seconds from Epoch to the specified date (from 1 January 1970 to 1 January 2000 in this example)
date -d "2000-01-01 12:00:00" +"%s" # Seconds from Epoch to the specified date and time (from 1 January 1970 00:00:00 to 1 January 2000 12:00:00 in this example)
date -d @1474303375 # Converts seconds from Epoch to date

Dates:
date +"%F" -d now # Return current day in the specified format
date +"%F" -d yesterday # Return yesterday's day in the specified format
date +"%F" -d tomorrow # Return tomorrow's day in the specified format
date +"%F" -d last-week # Return last week's date on current day in the specified format
date +"%F" -d next-week # Return next week's date on current day in the specified format
date +"%F" -d last-month # Return last month's date on current day in the specified format
date +"%F" -d next-month # Return next month's date on current day in the specified format
date +"%F" -d last-year # Return last year's date on current day in the specified format
date +"%F" -d next-year # Return next year's date on current day in the specified format
date +"%F" -d last-WEEKDAY # Return the date of the last given weekday (for example last-monday) in the specified format
date +"%F" -d next-WEEKDAY # Return the date of the next given weekday (for example next-monday) in the specified format

Calculate dates:
date +"%F" -d "now +15 days" # Show the resulting date adding the specified time to the current date in the specified format (date only in this example)
date +"%F" -d "2000-01-01 +15 days" # Show the resulting date adding the specified time to the given date in the specified format (date only in this example)
date +"%F %H:%M:%S" -d "now +10 seconds" # Show the resulting date adding the specified time to the current date in the specified format (date and time in this example)
date +"%F %H:%M:%S" -d "2000-01-01 10:10:00 UTC +30 seconds" # Show the resulting date adding the specified time to the given date in the specified format (date and time in this example)

Difference between dates:
date1='20050901'; date2='20200912'; echo $(( ( $( date +%s -d "$date2" ) - $( date +%s -d "$date1" ) )/86400 )); # Difference between two dates in days
time1='10:08'; time2='11:10'; IFS=: read hour1 min1 <<< "$time1"; IFS=: read hour2 min2 <<< "$time2"; min1=$((10#$hour1*60 + 10#$min1)); min2=$((10#$hour2*60 + 10#$min2)); echo "$((min2 - min1))"; # Difference between two times in minutes
date1='2020-11-10 12:00:10'; date2='2020-11-30 17:56'; echo $((($(date -ud "$date2" +'%s') - $(date -ud "$date1" +'%s'))/60)) # Difference between two dates and times in minutes

Get the day of the week for a specified date:
date -d "2000-01-01" +"%A"

Extract Date and Time from Apache Logs:
head /var/log/apache2/access.log --lines=1 | sed 's#[^[]*[[]\([^]][^]]*\).*#\1#' # Get Date and Time (including Time Zone) of the first entry in the Apache Log access.log
head /var/log/apache2/access.log --lines=1 | sed 's#[^[]*[[]\([^]][^]]*\).*#\1#' | awk '{print $1}' # Get Date and Time (excluding Time Zone) of the first entry in the Apache Log access.log
tail /var/log/apache2/access.log --lines=1 | sed 's#[^[]*[[]\([^]][^]]*\).*#\1#' # Get Date and Time (including Time Zone) of the last entry in the Apache Log access.log
tail /var/log/apache2/access.log --lines=1 | sed 's#[^[]*[[]\([^]][^]]*\).*#\1#' | awk '{print $1}' # Get Date and Time (excluding Time Zone) of the last entry in the Apache Log access.log
date -d "$(echo 'dd/MMM/yyyy:hh:mm:ss' | sed -e 's,/,-,g' -e 's,:, ,')" "+%a %d %b %Y %H:%M:%S" # Convert Apache log date/time to a better human readable date
date -d "$(echo 'dd/MMM/yyyy:hh:mm:ss' | sed -e 's,/,-,g' -e 's,:, ,')" +"%s" # Convert Apache log date/time to Linux timestamp

Calculate how long takes a command to be executed:
time COMMAND COMMAND_PARAMETERS
time cat # works like a stopwatch: counts time until you send an EOF to stop it (which generally means until you press CTRL+D that closes the cat command)

Create filenames containing current date and time:
touch data_`date +%F`.bak # create a file with the name data_YYYY-MM-DD.bak
touch log_`date +%F_%H-%M-%S`.txt # create a file with the name log_YYYY-MM-DD_HH-MM-SS.txt

User and system times:
times

Pause:
sleep S # pause for S seconds

Wait for a key press:
read -rs -n 1 -p "Press any key to continue..." wkey # Wait for any key
read -rs -p "Hit ENTER to continue..." wkey # Wait for ENTER

Show a calendar:
# cal  # show dayweeks in a row, Sunday first (use -m to have Monday first)
# ncal # show dayweeks in a coloumn, Monday first
cal # show a calendar for the current month
cal -y # show a calendar for the current year
cal -y YEAR # show a calendar for the given year
cal MONTH YEAR # show a calendar for the given MONTH and YEAR
ncal -e YEAR # show date of Easter (for Western Churches)
ncal -o YEAR # show date of Orthodox Easter (Greek and Russian Orthodox Churches)

Memorable dates:
The directory /usr/share/calendar/ contains some collections of remarkable dates.
For example:
cat /usr/share/calendar/calendar.history # Important historical dates
cat /usr/share/calendar/calendar.music # Important dates in music history
cat /usr/share/calendar/calendar.lotr # Lord of the Rings dates




Please DONATE to support the development of Free and Open Source Software (PayPal, Credit Card, Bitcoin, Ether)

Page issued on 25-Sep-2022 04:22 GMT
Copyright (c) 2022 Geody - Legal notices: copyright, privacy policy, disclaimer