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 - 231 results
IP=$(nslookup `hostname` | grep -i address | awk -F" " '{print $2}' | awk -F# '{print $1}' | tail -n 1 ); R=3$((RANDOM%6 + 1)); PS1="\n\[\033[1;37m\]\u@\[\033[1;$R""m\]\h^$IP:\[\033[1;37m\]\w\$\[\033[0m\] "
2010-10-20 07:29:14
User: rubo77
Functions: awk grep nslookup tail
0

this adds a random color to your prompt and the external ip.

useful if you are using multiple mashines with the same hostname.

s=`head -$i fileName | tail -1`
tail -f /var/log/messages | while read line; do accu="$line"; while read -t 1 more; do accu=`echo -e "$accu\n$more"`; done; notify-send "Syslog" "$accu"; done
2010-10-10 16:28:08
User: hfs
Functions: read tail
1

The given example collects output of the tail command: Whenever a line is emitted, further lines are collected, until no more output comes for one second. This group of lines is then sent as notification to the user.

You can test the example with

logger "First group"; sleep 1; logger "Second"; logger "group"
curl --silent http://www.dudalibre.com/gnulinuxcounter?lang=en | grep users | head -2 | tail -1 | sed 's/.*<strong>//g' | sed 's/<\/strong>.*//g'
tail -f `ls -alst /var/log/maillog* | awk '{print $10} NR>0{exit};0'` | grep "criteria"
endnl () { [[ -f "$1" && -s "$1" && -z $(tail -c 1 "$1") ]]; }
2010-08-25 12:06:10
User: quintic
Functions: tail
Tags: tail
1

tail -c 1 "$1" returns the last byte in the file.

Command substitution deletes any trailing newlines, so if the file ended in a newline $(tail -c 1 "$1") is now empty, and the -z test succeeds.

However, $a will also be empty for an empty file, so we add -s "$1" to check that the file has a size greater than zero.

Finally, -f "$1" checks that the file is a regular file -- not a directory or a socket, etc.

while (true); do clear; uname -n; echo ""; df -h /; echo ""; tail -5 /var/log/auth.log; echo ""; vmstat 1 5; sleep 15; done
2010-08-23 04:37:58
User: roknir
Functions: df echo sleep tail uname vmstat
1

You can use this one-liner for a quick and dirty (more customizable) alternative to the watch command. The keys to making this work: everything exists in an infinite loop; the loop starts with a clear; the loop ends with a sleep. Enter whatever you'd like to keep an eye on in the middle.

tail -f /var/log/system.log | colorizer
git log --pretty=format:%H | tail -1
2010-08-17 13:47:42
Functions: tail
Tags: git commit
1

git log --format=%H | tail -1 doesn't work anymore

rsync -av --link-dest=$(ls -1d /backup/*/ | tail -1) /data/ /backup/$(date +%Y%m%d%H%M)/
2010-08-05 19:36:24
User: dooblem
Functions: date ls rsync tail
Tags: backup rsync
1

'data' is the directory to backup, 'backup' is directory to store snapshots.

Backup files on a regular basis using hard links. Very efficient, quick. Backup data is directly available.

Same as explained here :

http://blog.interlinked.org/tutorials/rsync_time_machine.html

in one line.

Using du to check the size of your backups, the first backup counts for all the space, and other backups only files that have changed.

sudo ls -l $(eval echo "/proc/{$(echo $(pgrep java)|sed 's/ /,/')}/fd/")|grep log|sed 's/[^/]* //g'|xargs -r tail -f
2010-07-30 18:20:00
User: vutcovici
Functions: echo eval grep ls sed sudo tail xargs
-1

Tail all logs that are opened by all java processes. This is helpful when you are on a new environment and you do not know where the logs are located. Instead of java you can put any process name. This command does work only for Linux.

The list of all log files opened by java process:

sudo ls -l $(eval echo "/proc/{$(echo $(pgrep java)|sed 's/ /,/')}/fd/")|grep log|sed 's/[^/]* //g'
git log --format=%H | tail -1
2010-07-20 08:12:05
User: l0b0
Functions: tail
Tags: git commit
0

Can anyone make a shorter one?

This doesn't work:

git log --reverse -1 --format=%H
url=http://www.youtube.com/watch?v=V5bYDhZBFLA; youtube-dl -b $url; mplayer $(ls ${url##*=}*| tail -n1) -ss 00:57 -endpos 10 -vo gif89a:fps=5:output=output.gif -vf scale=400:300 -nosound
2010-07-18 02:11:39
User: zed
Functions: ls tail
12

requires "youtube-dl" -- sure you can do this with wget and some more obscurity but why waste your time when this great tool is available?

the guts consist of mplayer converting a video to a gif -- study this command and read the man page for more information

mplayer video.flv -ss 00:23 -endpos 6 -vo gif89a:fps=5:output=output.gif -vf scale=400:300 -nosound

generates a 6 second gif starting at 23 seconds of play time at 5 fps and a scale of 400x300

start time (-ss)/end time (-endpos) formats: 00:00:00.000

end time should be relative to start time, not absolute. i.e. -endpos 5 == seconds after 0:42 = 0:47 end point

play with fps and scale for lower gif sizes

the subshell is a solution for the -b flag on youtube-dl which downloads the best quality video, sometimes, which can be various video formats $(ls ${url##*=}*| tail -n1)

while (( 1==1 )); do du -c . >> output.log; sleep 2; done; tail -f output.log
2010-07-12 17:23:45
User: aceiro
Functions: du sleep tail
-5

this command shows the space used in postgres directory.

tail -n +4 | head -n 1
head -n X | tail -n 1
2010-07-08 22:06:39
User: infinull
Functions: head tail
-1

using tail first won't do it because tail counts from the bottom of the file. You could do it this way but I don't suggest it

tail -n 4 | head -n 1
2010-07-08 19:50:06
User: puddy
Functions: head tail
-7

tail -n X | head -n 1

prints a specific line, where X is the line number

IFS=`echo -en "\n\b"`; for i in $(curl http://feeds.digg.com/digg/container/technology/popular.rss | grep '<title>' | sed -e 's#<[^>]*>##g' | tail -n10); do echo $i; echo $i | sed 's/^/Did you hear about /g' | say; sleep 30; done
2010-06-07 22:16:19
User: echosedawk
Functions: echo grep sed sleep tail
Tags: bash sed curl osx
-2

Instead of having someone else read you the Digg headlines, Have OSX do it. Requires Curl+Sed+Say. This could probably be easily modified to use espeak for Linux.

tune2fs -l $(df -P / | tail -n1 | cut -d' ' -f1 ) | grep 'Filesystem created:'
diff <(tail -10 file1) <(tail -10 file2)
tail -fs 1 somefile
2010-05-18 23:42:35
User: ppaschka
Functions: tail
9

The -s option allows you to specify the update interval

tail -n2000 /var/www/domains/*/*/logs/access_log | awk '{print $1}' | sort | uniq -c | sort -n | awk '{ if ($1 > 20)print $1,$2}'
mplayer http://minnesota.publicradio.org/tools/play/streams/the_current.pls < /dev/null | grep --line-buffered "StreamTitle='.*S" -o | grep --line-buffered "'.*'" -o > mus & tail -n0 -f mus | while read line; do notify-send "Music Change" "$line";done
2010-05-09 17:51:40
User: spiffwalker
Functions: grep read tail
7

Plays the mp3 stream of The Current as a background job. When you are done run:

fg %1

then to exit

Quite possible with Growl for mac I'd guess, although have not tried.

Libnotify needed for notification, stream will still work otherwise

tail -f error_log | nc -l 1234
2010-05-07 18:14:04
User: zlemini
Functions: tail
3

Netcat is used to serve a log-file over a network on port 1234.

Point a browser to the specified server/port combo to view log-file updates in real-time.

/etc/init.d/cron restart && tail -100 /var/log/syslog
2010-05-05 09:56:30
User: root
Functions: tail
-7

Debian-specific but very useful as cron files are prone to very subtle gotchas