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.

Universal configuration monitoring and system of record for IT.

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:



May 19, 2015 - A Look At The New Commandlinefu
I've put together a short writeup on what kind of newness you can expect from the next iteration of clfu. Check it out here.
March 2, 2015 - New Management
I'm Jon, I'll be maintaining and improving clfu. Thanks to David for building such a great resource!

Top Tags





Commands by zlemini from sorted by
Terminal - Commands by zlemini - 40 results
sudo lsof -rc command >> /tmp/command.txt
2011-08-03 20:19:53
User: zlemini
Functions: command sudo

Run this before you run a command in order to see what the command does as it starts.

The -c flag is useful here as the PID is unknown before startup.

All config files, libraries, logs, ports, etc used by the command as it starts up, (and shuts down) will be captured at 1s intervals and written to a file.

Useful for debugging etc.

lsof -nPi | txt2html > ~/lsof.html
2011-07-28 14:01:21
User: zlemini
Tags: perl cpan lsof

The output of lsof is piped to txt2html which converts it to html.

# Perl module HTML::TextToHTML needed

sudo lsof -p1234 | grep -E "(3r|4w).*REG"
2011-01-11 19:45:24
User: zlemini
Functions: grep sudo

If you spot a dubious looking cp command running you can use this command to view what is being copied and to where.

1234 is the PID of the cp command being passed to the lsof utility.

3r.*REG will display the file/directory that is being read/copied.

4w.*REG will display the destination it is being written to.

awk '{ $5=""; print }' file
2010-10-22 09:48:49
User: zlemini
Functions: awk
Tags: cut

Here's an awk alternative, for those lacking the version of cut with the --complement argument.

lsof -i :555-7000
2010-10-07 20:45:54
User: zlemini

View details of both TCP and UDP network activity within a specified port range.

sudo lsof -u someuser -a +D /etc
2010-06-11 06:37:27
User: zlemini
Functions: sudo

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

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

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

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

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

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

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

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

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

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'