Hide

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.

Hide

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:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

Commands tagged date from sorted by
Terminal - Commands tagged date - 88 results
echo "($(date +%s)-$(date +%s -d "march 1"))/86400"|bc
2010-07-22 19:44:50
User: nickwe
Functions: echo
Tags: echo bc date
2

Exactly the same number of characters, exactly the same results, but with bc

TZ=PST8PDT+72 date '+%Y_%m_%d'
2010-07-02 00:29:27
Functions: date
Tags: date nongnu
4

This command prints the Date (Not time) from 3 days ago (72 hours ago).

This works on systems without GNU date (MacOSX , Solaris, FreeBSD).

sudo find . -maxdepth 1 -cnewer olderFilesNameToMove -and ! -cnewer newerFileNameToMove -exec mv -v {} /newDirectory/ \;
2010-06-30 20:40:30
User: javamaniac
Functions: find mv sudo
2

In a folder with many files and folders, you want to move all files where the date is >= the file olderFilesNameToMove and

watch -tn1 'date -u +%T -d @$(expr $(date -d HH:MM +%s) - $(date +%s)) | toilet -f bigmono12'
2010-06-26 11:56:11
User: prayer
Functions: date expr watch
Tags: date time
-2

Change HH:MM with your target time.

This is for a Debian/Ubuntu GNU system. You need bash (package bash), date (package coreutils) and toilet (package toilet). Install with:

# apt-get install bash coreutils toilet toilet-fonts

touch file-$(date +%Y%m%d)
grep -i "$(date +%b\ %d\ %H)" syslog
2010-05-23 16:18:15
User: rubenmoran
Functions: grep
Tags: log date
6

Uses date to grep de logfile for today and uses it to get the last hour logs. Can be used to get last minute logs or today's logs.

utime(){ python -c "import time; print(time.strftime('%a %b %d %H:%M:%S %Y', time.localtime($1)))"; }
utime(){ awk -v d=$1 'BEGIN{print strftime("%a %b %d %H:%M:%S %Y", d)}'; }
utime(){ date -d "1970-01-01 GMT $1 seconds"; }
utime { date -d @$1; }
2010-05-12 12:21:15
User: deltaray
Functions: date
4

More recent versions of the date command finally have the ability to decode the unix epoch time into a human readable date. This function makes it simple to utilize this feature quickly.

alias dateh='date --help|sed -n "/^ *%%/,/^ *%Z/p"|while read l;do F=${l/% */}; date +%$F:"|'"'"'${F//%n/ }'"'"'|${l#* }";done|sed "s/\ *|\ */|/g" |column -s "|" -t'
21

If you have used bash for any scripting, you've used the date command alot. It's perfect for using as a way to create filename's dynamically within aliases,functions, and commands like below.. This is actually an update to my first alias, since a few commenters (below) had good observations on what was wrong with my first command.

# creating a date-based ssh-key for askapache.github.com

ssh-keygen -f ~/.ssh/`date +git-$USER@$HOSTNAME-%m-%d-%g` -C 'webmaster@askapache.com' # /home/gpl/.ssh/git-gplnet@askapache.github.com-04-22-10

# create a tar+gzip backup of the current directory

tar -czf $(date +$HOME/.backups/%m-%d-%g-%R-`sed -u 's/\//#/g' <<< $PWD`.tgz) . # tar -czf /home/gpl/.backups/04-22-10-01:13-#home#gpl#.rr#src.tgz .

I personally find myself having to reference

date --help

quite a bit as a result. So this nice alias saves me a lot of time. This is one bdash mofo. Works in sh and bash (posix), but will likely need to be changed for other shells due to the parameter substitution going on.. Just extend the sed command, I prefer sed to pretty much everything anyways.. but it's always preferable to put in the extra effort to go for as much builtin use as you can. Otherwise it's not a top one-liner, it's a lazyboy recliner.

Here's the old version:

alias dateh='date --help|sed "/^ *%%/,/^ *%Z/!d;s/ \+/ /g"|while read l;do date "+ %${l/% */}_${l/% */}_${l#* }";done|column -s_ -t'

This trick from my [ http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html bash_profile ]

date +%A | cut -c $(( $(date +%A | wc -c) - 1 ))
2010-04-07 00:23:15
User: DaveQB
Functions: cut date wc
Tags: bash echo cut date wc
0

A command to find out what the day ends in. Can be edited slightly to find out what "any" output ends in.

NB: I haven't tested with weird and wonderful output.

function ends_in_y() { case $(date +%A) in *y ) true ;; * ) false ;; esac } ; ends_in_y && echo ok
2010-04-06 22:18:52
Functions: date echo false true
-1

The shell has perfectly adequate pattern matching for simple expressions.

function ends_in_y() { if [ `date +%A | sed -e 's/\(^.*\)\(.$\)/\2/'` == "y" ]; then echo 1; else echo 0; fi }
2010-04-06 20:14:34
User: allrightname
Functions: echo sed
-3

For those days when you need to know if something is happening because the day ends in "y".

sed -i 's/20[0-1][0-9]\{7\}/'`date +%Y%m%d%I`'/g' *.db
2010-03-24 07:28:58
User: alf
Functions: sed
6

Will edit *.db files in the same directory with todays date. Useful for doing a mass update to domains on a nameserver, adding spf records, etc.

Looks for a string starting with 200 or 201 followed by 7 numbers, and replaces with todays date. This won't overwrite Ip's but i would still do some double checking after running this.

Make sure your server's date is correct, otherwise insert your own serial number.

rndc reload

should usually follow this command.

date -j -v1d -v-0m -v-1d +'%m %d %Y'
2010-03-04 17:47:51
User: drewk
Functions: date
-4

This produces a parseable output of the last day of the month in future or past. Change the '-v-0m' to be a month plus or minus from the current system time.

date -s "`curl -sI www.example.com | sed -n 's/^Date: //p'`"
2010-01-08 21:05:41
User: putnamhill
Functions: date
Tags: curl date
0

If you don't have netcat, you can use curl.

date -s "$(echo -e "HEAD / HTTP/1.0\n" | nc www.example.com 80 | sed -ne 's/^Date: \(.*\)$/\1/p')"
2010-01-08 20:05:00
User: tomhol
Functions: date
Tags: date
0

Good when firewalled and only in need of a reasonable accurate time.

Use a fast responding web server.

xvkbd -xsendevent -text $(date +%Y%m%d)
2009-12-23 12:01:07
User: sputnick
Functions: date
13

That works in all softs, CLI or GUI... I don't want to waste time to all the time typing the same stuff . So, I have that command in my window manager shortcuts ( meta+l ). All the window managers have editable shortcuts AFAIK. If not, or you don't want to use it that way, you can easily use the xbindkeys soft.

I you're using kde4, you can run :

systemsettings

then open "inputs actions" and create a new shortcut.

For Gnome take a look there : http://www.cyberciti.biz/faq/howto-create-keyboard-shortcuts-in-gnome/

A more advanced one, with strings and newlines :

xvkbd -xsendevent -text "---8<-----\nToday date is: $(date +%Y%m%d)\n---8<-----"

For complicated or long paste, you can feed xvkbd with a file :

xvkbd -xsendevent -file <file>

You can simulate ^C ( control+c ) too or others combinations of keys :

xvkbd -text "\C\Ac"

There's no man page nor help ( On my Archlinux distro ), but you can see online doc there : http://homepage3.nifty.com/tsato/xvkbd/

curl -o id.gif `date +http://d.yimg.com/a/p/uc/%Y%m%d/largeimagecrwiz%y%m%d.gif`
2009-12-04 20:16:32
User: putnamhill
Tags: curl date
0

Requires the date command. This also works with some other comics. Here's a bash script that displays daily Garfield, Id, and Andy Capp:

http://putnamhill.net/cgi-bin/yahoo-comics.sh?ga,crwiz,crcap

ruby -rdate -e 'p DateTime.now.cweek'
date +%V
ruby -e 'require "date"; puts DateTime.now.cweek'
perl -e 'use Date::Calc qw(Today Week_Number); $weekn = Week_Number(Today); print "$weekn\n"'
backup() { for i in "$@"; do cp -va $i $i.$(date +%Y%m%d-%H%M%S); done }
2009-11-10 20:59:45
User: polaco
Functions: cp date
Tags: backup copy date
4

This script creates date based backups of the files. It copies the files to the same place the original ones are but with an additional extension that is the timestamp of the copy on the following format: YearMonthDay-HourMinuteSecond