What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Universal configuration monitoring and system of record for IT.

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for:



May 19, 2015 - A Look At The New Commandlinefu
I've put together a short writeup on what kind of newness you can expect from the next iteration of clfu. Check it out here.
March 2, 2015 - New Management
I'm Jon, I'll be maintaining and improving clfu. Thanks to David for building such a great resource!

Top Tags





Commands tagged date from sorted by
Terminal - Commands tagged date - 100 results
echo "obase=2;$((($(date +%s)-$(date +%s -d YYYY-MM-DD))/86400))" | bc
2015-10-19 15:40:32
User: flatcap
Functions: echo

Print out your age in days in binary.

Today's my binary birthday, I'm 2^14 days old :-)


This command does bash arithmatic $(( )) on two dates:

Today: $(date +%s)

Date of birth: $(date +%s -d YYYY-MM-DD)

The dates are expressed as the number of seconds since the Unix epoch (Jan 1970),

so we devide the difference by 86400 (seconds per day).


Finally we pipe "obase=2; DAYS-OLD" into bc to convert to binary.

(obase == output base)

D="$(date "+%F %T.%N")"; [COMMAND]; find . -newermt "$D"
2015-10-15 21:09:54
User: flatcap
Functions: find

Often you run a command, but afterwards you're not quite sure what it did.

By adding this prefix/suffix around [COMMAND], you can list any files that were modified.


Take a nanosecond timestamp: YYYY-MM-DD HH:MM:SS.NNNNNNNNN

date "+%F %T.%N"


Find any files that have been modified since that timestamp:

find . -newermt "$D"


This command currently only searches below the current directory.

If you want to look elsewhere change the find parameter, e.g.

find /var/log . -newermt "$D"
ssh(){ L="\$HOME/logs/$(date +%F_%H:%M)-$USER";/usr/bin/ssh -t "$@" "mkdir -p \"${L%/*}\";screen -xRRS $USER script -f \"$L\"";}
2015-10-14 13:14:29
User: flatcap
Functions: ssh

A wrapper around ssh to automatically provide logging and session handling.

This function runs ssh, which runs screen, which runs script.


The logs and the screen session are stored on the server.

This means you can leave a session running and re-attach to it later, or from another machine.




* Log sessions on a remote server

* Transparent - nothing extra to type

* No installation - nothing to copy to the server beforehand



* Function wrapper delegating to ssh

- so nothing to remember

- uses .ssh/config as expected

- passes your command line option to ssh

* Self-contained: no scripts to install on the server

* Uses screen(1), so is:

- detachable

- re-attachable

- shareable

* Records session using script(1)

* Configurable log file location, which may contain variables or whitespace

L="$HOME" # local variable

L="\$HOME" # server variable

L="some space"



* Log dir/file may not contain '~' (which would require eval on the server)



The sessions are named by the local user connecting to the server.

Therefore if you detach and re-run the same command you will reconnect to your original session.

If you want to connect/share another's session simply run:

USER=bob ssh root@server


The command above is stripped down to an absolute minimum.

A fully expanded and annotated version is available as a Gist (git pastebin):



If you want to add timing info to script, change the command to:

ssh(){ L="\$HOME/logs/$(date +%F_%H:%M)-$USER";/usr/bin/ssh -t "$@" "mkdir -p \"${L%/*}\";screen -xRRS $USER script --timing=\"$L-timing\" -f \"$L\"";}
echo FileName | perl -nlE'sleep 1 while time-(stat)[10]<10' && echo DONE
2015-05-09 14:58:41
User: pung96
Functions: echo perl

perl version of "Wait for file to stop changing"

When "FileName" has not been changed for last 10 seconds, then print "DONE"

"10" in "(stat)[10]" means ctime.

One have other options like atime, mtime and others. http://perldoc.perl.org/functions/stat.html

while [ $(( $(date +%s) - $(stat -c %Y FILENAME) )) -lt 10 ]; do sleep 1; done; echo DONE
2015-05-09 12:30:13
User: flatcap
Functions: date echo sleep stat

This loop will finish if a file hasn't changed in the last 10 seconds.


It checks the file's modification timestamp against the clock.

If 10 seconds have elapsed without any change to the file, then the loop ends.


This script will give a false positive if there's a 10 second delay between updates,

e.g. due to network congestion


How does it work?

'date +%s' gives the current time in seconds

'stat -c %Y' gives the file's last modification time in seconds

'$(( ))' is bash's way of doing maths

'[ X -lt 10 ]' tests the result is Less Than 10

otherwise sleep for 1 second and repeat


Note: Clever as this script is, inotify is smarter.

debugfs -R "stat <$(stat --printf=%i filename)>" /dev/sdaX | grep crtime
2015-04-09 01:23:56
User: pggx999
Functions: debugfs grep

Return the creation date of a file on ext2, 3, 4 filesystems, because stat command won't show it.

Useful on ubuntu, debian, and else

date -r 1390196676
function findOlderThan () { find . -mmin -$((($(date "+%s") - $(stat -c %Y $1))/60)) -type f ; }
2014-08-29 17:52:34
User: RobertDeRose
Functions: date find stat
Tags: find date stat

This function will find the modification time in unix_time of the given file, then calculate the number of minutes from now to then and then find all files modified in that range.

YEAR=2015; echo Jul $(ncal 7 $YEAR | awk '/^Fr/{print $NF}')
2014-08-17 11:12:09
User: andreasS
Functions: awk echo
Tags: awk date

Calculate the date of Sysadmin day (last Friday of July) of any given year

YEAR=2015; date -d${YEAR}0801-$(date -d${YEAR}0801+2days +%u)days +%b\ %e
2014-08-17 11:06:25
User: andreasS
Functions: date
Tags: date

Calculate Sysadmin day of any given year using 2 `date`. Code based on http://stackoverflow.com/a/5656859/196133

YEAR=2015; ncal 7 $YEAR | sed -n 's/^Fr.* \([^ ]\+\) *$/Jul \1/p'
2014-08-17 11:04:02
User: andreasS
Functions: sed
Tags: sed date

Calculate the date of Sysadmin day (last Friday of July) of any given year

echo {-1..-5}days | xargs -n1 date +"%Y-%m-%d" -d
alias t__s='date "+%FT%T"'
2014-03-06 04:37:55
User: UncleLouie
Functions: alias

This can be easier to look at in ls output. Not as clean as +%Y%m%dT%H%M%S, but quicker to write.

date -u `ssh user@remotehost date -u '+%m%d%H%M%Y.%S'`
2014-02-10 03:11:14
User: scruss
Functions: date

Useful if localhost is a small machine running BusyBox, which uses a slightly unusual format to set the date. Remotehost can be pretty much any Linux machine, including one running BusyBox. Uses UTC for portability.

touch -r "source_file" "destination_file"
sudo tar -zcvf $(hostname)-etc-back-`date +%d`-`date +%m`-`date +%y`.tar.gz /etc && sudo chown $USER:$USER $(hostname)-etc-back*
2013-08-18 12:49:11
User: thanosme
Functions: chown sudo tar
Tags: backup tar date

Back up /etc directory with a name based on the current date and the hostname of the machine, then chown the file for the current user for use.

git log | grep Date | awk '{print " : "$4" "$3" "$6}' | uniq -c
sudo cp /usr/share/zoneinfo/Europe/Paris /etc/localtime
ls -lT -rt | grep "^-" | awk 'BEGIN {START=2002} (START <= $9){ print $10 ;START=$9 }' | tail -1
2013-02-24 23:39:22
User: Glamdring
Functions: awk grep ls tail
Tags: ls date osx

On the Mac, the 'ls' function can sort based on month/day/time, but seems to lack ability to filter on the Year field (#9 among the long listed fields). The sorted list continuously increases the 'START' year for the most recently accessed set of files. The final month printed will be the highest month that appeared in that START year. The command does its magic on the current directory, and suitably discards all entries that are themselves directories. If you expect files dating prior to 2002, change the START year accordingly.

if date -d 2006-10-10 >> /dev/null 2>&1; then echo 1; else echo 0; fi
2013-01-10 10:35:15
Functions: date echo

On CentOS at least, date returns a boolean for the common date string formats, including YYYY-MM-DD. In the sample output, you can see various invalid dates returning 0 whereas a simple regex check would return 1 for the invalid dates.

-d, --date=STRING display time described by STRING, not `now'

The version of date on OS X does not appear to have this same option.

date +%:z
buf () { oldname=$1; if [ "$oldname" != "" ]; then datepart="$(date +%Y-%m-%d).bak"; firstpart=`echo $oldname | cut -d "." -f 1`; newname=`echo $oldname | sed s/$firstpart/$firstpart.$datepart/`; cp -iv ${oldname} ${newname}; fi }
2012-08-15 08:31:44
User: juliushibert
Functions: cp cut sed

Appends the input file with the date format YYYY-MM-DD.bak. Also runs silently if you remove the -v on the cp at the end of the function.

echo 00:29:36 | sed s/:/*60+/g | bc
datefudge "2012-12-01 12:00" date
desiredDay=6; year=2012; month=5; n=0; while [ $(date -d "$year-$((month+1))-1 - $n day" "+%u") -ne $desiredDay ]; do n=$((n+1)); done; date -d "$year-$((month+1))-1 - $n day" "+%x"
2012-05-17 12:02:30
Functions: date

Choosing your year and month. You only need the gnu date command and bash. desiredDay of the week is (1..7); 1 is Monday.

If you want desiredDay of week (0..6); 0 is Sunday

desiredDay=6; year=2012; month=5; n=0; while [ $(date -d "$year-$((month+1))-1 - $n day" "+%w") -ne $desiredDay ]; do n=$((n+1)); done; date -d "$year-$((month+1))-1 - $n day" "+%x"