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 by zlemini from sorted by
Terminal - Commands by zlemini - 35 results
sudo lsof -u someuser -a +D /etc
2010-06-11 06:37:27
User: zlemini
Functions: sudo
16

View all files opened by a user in specified directory.

The +D option makes lsof search all sub-directories to complete depth, while ignoring symbolic links.

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.

for i in emerg alert crit error warn ; do awk '$6 ~ /^\['$i'/ {print substr($0, index($0,$6)) }' error_log | sort | uniq -c | sort -n | tail -1; done
2010-04-15 21:47:18
User: zlemini
Functions: awk sort tail uniq
4

This searches the Apache error_log for each of the 5 most significant Apache error levels, if any are found the date is then cut from the output in order to sort then print the most common occurrence of each error.

awk '$9 == 404 {print $7}' access_log | uniq -c | sort -rn | head
2010-04-08 21:40:53
User: zlemini
Functions: awk sort uniq
8

Finds the top ten pages returning an http response code of 404 in an apache log.

{ rm -f file10 && nl > file10; } < file10
2010-04-08 21:08:23
User: zlemini
Functions: nl rm
3

Add permanent line numbers to a file without creating a temp file.

The rm command deletes file10 while the nl command works on the open file descriptor of file10 which it outputs into a new file again named file10.

The new file10 will now be numbered in the same directory with the same file name and content as before, but it will in fact be a new file, using (ls -i) to show its inode number will prove this.

lsof -r 2 -p PID -i -a
2010-03-16 20:37:16
User: zlemini
15

The "-r 2" option puts lsof in repeat mode, with updates every 2 seconds. (Ctrl -c quits)

The "-p" option is used to specify the application PID you want to monitor.

The "-u' option can be used to keep an eye on a users network activity.

"lsof -r 2 -u username -i -a"

diff <(lsof -p 1234) <(sleep 10; lsof -p 1234)
2010-03-15 22:55:32
User: zlemini
Functions: diff sleep
35

This command takes a snapshot of the open files for a PID 1234 then waits 10 seconds and takes another snapshot of the same PID, it then displays the difference between each snapshot to give you an insight into what the application is doing.

lsof -p 1234 | grep -E "\.log$" | awk '{print $NF}'
2010-03-05 11:41:28
User: zlemini
Functions: awk grep
6

Uses lsof to display the full path of ".log" files opened by a specified PID.

grep 'test' somefile | grep -vE '(error|critical|warning)'
2009-10-23 23:21:36
User: zlemini
Functions: grep
16

Use multiple patterns with grep -v. So you can print all lines in a file except those containing the multiple patterns you specify.

PERMA () { echo "$@" >> ~/.bashrc; }
2009-09-28 16:03:24
User: zlemini
Functions: echo
2

Simple function to permanently add an alias to your profile.

Tested on bash and Ksh, bash version above.

Here is the ksh version: PERMA () { print "$@" >> ~/.profile; }

Sample usage:

PERMA alias la='ls -a'