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 using tail from sorted by
Terminal - Commands using tail - 224 results
ls -trF | grep -v \/ | tail -n 1
2011-09-14 20:05:37
User: mrpollo
Functions: grep ls tail
Tags: find stat mtime
-1

Sort by time and Reverse to get Ascending order, then display a marker next to the a file, negate directory and select only 1 result

fn=$(find . -type f -printf "%T@\t%p\n"|sort -n|tail -1|cut -f2); echo $(date -r "$fn") "$fn"
sudo netstat|head -n2|tail -n1 && sudo netstat -a|grep udp && echo && sudo netstat|head -n2|tail -n1 && sudo netstat -a|grep tcp
search="whatyouwant";data=$(grep "$search" * -R --exclude-dir=.svn -B2 -A2);for((i=$(echo "$data" | wc -l);$i>0;i=$(($i-6)) )); do clear;echo "$data"| tail -n $i | head -n 5; read;done
2011-08-29 18:14:16
User: Juluan
Functions: echo grep head tail wc
-2

Not perfect but working (at least on the project i wrote it ;) )

Specify what you want search in var search, then it grep the folder and show one result at a time.

Press enter and then it will show the next result.

It can work bad on result in the firsts lines, and it can be improved to allow to come back.

But in my case (a large project, i was checking if a value wasn't used withouth is corresponding const and the value is "1000" so there was a lot of result ...) it was perfect ;)

mount |tail -1 | less -p "/dev/[^ ]*"
cd $(ls -ltr|grep ^d|head -1|sed 's:.*\ ::g'|tail -1)
2011-08-10 03:39:35
Functions: cd grep head ls sed tail
-1

Replace the head -1 with head -n that is the n-th item you want to go to.

Replace the head with tail, go to the last dir you listed.

You also can change the parameters of ls.

netstat -an |grep ":80" |awk '{print $5}' | sed s/::ffff://g | cut -d: -f1 |sort |uniq -c |sort -n | tail -1000 | grep -v "0.0.0.0"
NAME=`wget --quiet URL -O - | grep util-vserver | tail -n 1 | sed 's|</a>.*||;s/.*>//'`; wget URL$UTILVSERVER;
ps aux | awk {'sum+=$3;print sum'} | tail -n 1
tail -f /var/log/squid/access.loc | ccze -CA
tail -n0 -f /var/log/messages | while read line; do notify-send "System Message" "$line"; done
2011-07-11 22:33:24
User: hukketto
Functions: read tail
Tags: notify-send
1

It willl popup a message for each new entry in /var/log/messages

found on the notify-send howto page on ubuntuforums.org.

Posted here only because it is one of the favourites of mine.

REALUSERID=`TTYTEST=$(ps | awk '{print $2}' |tail -1); ps -ef |grep "$TTYTEST$" |awk '{print $1}'`;echo $REALUSERID
2011-07-09 16:27:21
User: khyron320
Functions: awk echo grep ps tail
Tags: sudo
0

Find the USERid of a SUDOed user who has either left their terminal logged in or for scripting purposes to track who ran what commands.

This only applys to users that do sudo su - USERNAME. not sudo su USERNAME

Sudo su without the dash allows use of (echo $SUDO_USER)

tail -f /var/log/squid/access.log | perl -p -e 's/^([0-9]*)/"[".localtime($1)."]"/e'
tail -f /var/log/logfile|perl -e 'while (<>) {$l++;if (time > $e) {$e=time;print "$l\n";$l=0}}'
2011-06-21 10:28:26
User: madsen
Functions: perl tail time
Tags: perl tail
2

Using tail to follow and standard perl to count and print the lps when lines are written to the logfile.

head -n1 sample.txt | tail -n1
2011-06-14 17:45:04
User: gtcom
Functions: head tail
Tags: tail HEAD
-1

You can actually do the same thing with a combination of head and tail. For example, in a file of four lines, if you just want the middle two lines:

head -n3 sample.txt | tail -n2

Line 1 --\

Line 2 } These three lines are selected by head -n3,

Line 3 --/ this feeds the following filtered list to tail:

Line 4

Line 1

Line 2 \___ These two lines are filtered by tail -n2,

Line 3 / This results in:

Line 2

Line 3

being printed to screen (or wherever you redirect it).

history | tail -(n+1) | head -(n) | sed 's/^[0-9 ]\{7\}//' >> ~/script.sh
2011-06-08 13:40:58
Functions: head sed tail
1

Uses history to get the last n+1 commands (since this command will appear as the most recent), then strips out the line number and this command using sed, and appends the commands to a file.

tail /var/log/auth.log -n 100
ls -atr /home/reports/*.csv -o --time-sty=+%s | tail -1 | awk '{print systime()-$5}'
FILE=`ls -ltr /var/lib/pgsql/backups/daily/ | tail -n1 | awk '{print $NF}'`; TIME=`stat -c %Y /var/lib/pgsql/backups/daily/$FILE`; NOW=`date +%s`; echo $((NOW-TIME))
find . -printf '%d\n' | sort -n | tail -1
find /var/log -iregex '.*[^\.][^0-9]+$' -not -iregex '.*gz$' 2> /dev/null | xargs tail -n0 -f | ccze -A
tail -f /var/log/messages | espeak
2011-04-08 10:24:49
User: dmytrish
Functions: tail
1

you can listen to your computer, but don't be carried away

tail -f somefile |tee >(grep --line-buffered '1' > one.txt) |tee >(grep --line-buffered '2' > two.txt)
2011-04-03 10:05:28
User: binaryten
Functions: grep tail tee
4

when using named pipes only one reader is given the output by default. Also, most commands piped to by grep use a buffer which save output until tail -f finishes, which is not convenient. Here, using a combination of tee, sub-processes and the --line-buffered switch in grep we can workaround the problem.

history | awk '{print $2}' | awk 'BEGIN {FS="|"}{print $1}' | sort | uniq -c | sort -n | tail | sort -nr
curl http://www.discogs.com/search?q=724349691704 2> /dev/null | grep \/release\/ | head -2 | tail -1 | sed -e 's/^<div>.*>\(.*\)<\/a><\/div>/\1/'