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 - 222 results
tail -f logfile.log | cut -b 1-80
2009-03-26 18:41:57
User: plasticboy
Functions: cut tail
Tags: tail log
3

This truncates any lines longer than 80 characters. Also useful for looking at different parts of the line, e.g. cut -b 50-100 shows columns 50 through 100.

history | perl -lane '$lsize{$_} = scalar(@F); if($longest<$lsize{$_}) { $longest = $lsize{$_}; print "$_"; };' | tail -n1
INFILE=/path/to/your/backup.img; MOUNTPT=/mnt/foo; PARTITION=1; mount "$INFILE" "$MOUNTPT" -o loop,offset=$[ `/sbin/sfdisk -d "$INFILE" | grep "start=" | head -n $PARTITION | tail -n1 | sed 's/.*start=[ ]*//' | sed 's/,.*//'` * 512 ]
6

Suppose you made a backup of your hard disk with dd:

dd if=/dev/sda of=/mnt/disk/backup.img

This command enables you to mount a partition from inside this image, so you can access your files directly.

Substitute PARTITION=1 with the number of the partition you want to mount (returned from sfdisk -d yourfile.img).

cat $(ls -tr | tail -1) | awk '{ a[$1] += 1; } END { for(i in a) printf("%d, %s\n", a[i], i ); }' | sort -n | tail -25
2009-03-06 17:50:29
User: oremj
Functions: awk cat ls sort tail
7

This command is much quicker than the alternative of "sort | uniq -c | sort -n".

tail -f *[!.1][!.gz]
2009-03-06 16:24:44
User: piscue
Functions: tail
5

with discard wilcards in bash you can "tail" newer logs files to see what happen, any error, info, warn...

du | sort -n | tail -11 | head
2009-03-04 16:06:34
User: phage
Functions: du sort tail
-3

The pipe to head removes the listing of . as the largest directory.

tail -n 0 -f /var/log/messages
2009-03-02 14:21:18
User: atoponce
Functions: tail
3

In this case, I'm keeping an eye on /var/log/messages, but of course any file will do. When I'm following a file, I generally don't want to see anything other than what has been added due to the command or service I've executed. This keeps everything clean and tidy for troubleshooting.

tail -f file1 (file2 .. fileN)
2009-03-02 11:13:42
User: hberth
Functions: tail
Tags: bash Ubuntu
3

Useful to e.g. keep an eye on several logfiles.

tail -n 15 /var/log/yum.log | tac
2009-03-02 08:56:04
User: alcik
Functions: tail
Tags: yum tail
0

It displays, last 15 yum operations (in last operation as first row order) with its dates. Change 15 to any number of operations you need to display or remove "| tac" to see it in reverse order (last operation as last row)

tail -1000 /some/file | vim -
2009-02-25 11:43:27
User: root
Functions: tail vim
17

The hyphen tells vim to open from STDOUT - saves having to create temporary files.

tail -n 20 ~/Library/Logs/FileSyncAgent.log
2009-02-19 05:05:21
User: sacrilicious
Functions: tail
-3

tail would be considered dull, but pair this with being able to push out unix commands over ARD, and life gets easier. (Same can be said for my TimeMachine scrape command, http://xrl.us/begrzb)

syslog -F '$Time $Message' -k Sender /System/Library/CoreServices/backupd -k Time ge -72h | tail -n 30
2009-02-19 04:33:25
User: sacrilicious
Functions: tail
0

While they are few config options and even fewer useful details regarding what actually is being sent by the time machine 'backupd' process, this can at least tell you its doing something, how much it's doing, and exactly how often. Via macosxhints, http://xrl.us/begrwa, which in turn was via comments

tail -f /path/to/timestamped/files/file-*(om[1])
2009-02-16 22:55:16
User: karld
Functions: tail
5

zsh only

If you have this command in your history, you can always re-run it and have it reference the latest file.

The glob matches all timestamped files and then the resulting array is sorted by modification time (m) and then the first element in the sorted array is chosen (the latest)

netstat -pant 2> /dev/null | grep SYN_ | awk '{print $5;}' | cut -d: -f1 | sort | uniq -c | sort -n | tail -20
2009-02-16 08:49:38
3

List top 20 IP from which TCP connection is in SYN_RECV state.

Useful on web servers to detect a syn flood attack.

Replace SYN_ with ESTA to find established connections

cat -n $file | tail -n 100 && head -n number-of-lines-you-want-to-keep > newfile
2009-02-15 01:02:10
User: bbelt16ag
Functions: cat head tail
-1

tells you the number of lines in said file, and then tail the last 100 lines ( or how many are messed up) then u take the total amount of lines and then subract the 100 or so lines u DONT WANT, then do a head -n $new_number and then redirect it to new file.db

tail -f /var/log/messages
2009-02-12 19:30:33
User: nicdev
Functions: tail
-3

Use this command to see logs update in real time

head -1000 < lines.txt | tail -1
alias nsl 'netstat -f inet | grep -v CLOSE_WAIT | cut -c-6,21-94 | tail +2'
view `ls -1 access_log.* | tail -n 1`
2009-02-06 18:38:25
User: jimjmarion
Functions: tail
0

This command opens the latest, most current rotating apache access log for visual analysis and inspection. Run this command from the apache log directory. For error logs, replace access_log with error_log.

tail -f <filename> | grep -C <# of lines to show above and below> <text>
(> errors.log) && tail -f !^
2009-02-03 16:08:19
User: root
Functions: tail
2

This is useful for keeping an eye on an error log while developing. The !^ pulls the first arg from the previous command (which needs to be run in a sub-shell for this shortcut to work).

tail -10000 access_log | awk '{print $1}' | sort | uniq -c | sort -n | tail
2009-01-25 21:01:52
User: root
Functions: awk sort tail uniq
20

This uses awk to grab the IP address from each request and then sorts and summarises the top 10.