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
pssh -h RemoteHosts.txt -P -I < ~/LocalScript.sh
2014-07-14 16:02:12
User: zlemini
1

Runs a local script over ssh assuming ssh keys are in place.

-P argument prints results to stdout.

# Uses - https://code.google.com/p/parallel-ssh/

for i in $(getent group|cut -d: -f1); do sudo usermod -a -G "$i" $USER; done
for i in $(sudo lsof -Pni4 | awk -F: '/LISTEN/ { print substr($2,1,3) }'); do nc -z $(hostname) $i; done
2014-02-06 22:02:53
User: zlemini
Functions: awk sudo
0

Check if your open ports as listed by lsof are actually accepting connections using netcat.

prepend () { array=("$@"); len=${#array[@]}; file=${array[$len-1]}; text=${array[@]:0:$len-1}; printf '%s\n' 0a "$text" . w | ed -s "$file"; }
2013-12-09 21:59:26
User: zlemini
Functions: ed printf
Tags: sed replace
3

Syntax:

$ prepend content to add [filename]

Uses ed, so no temp files created.

cut -d " " -f1,4 access_log | sort | uniq -c | sort -rn | head
2013-09-20 21:29:32
User: zlemini
Functions: cut sort uniq
1

Analyze an Apache access log for the time period with most activity and display the hit count, requesting IP and the timestamp. May help detect a brute force dos attack.

curl -s http://www.census.gov/popclock/data/population/world | awk -F'[:,]' '{print $7}'
sxh () { for i in "${@:2}"; do ssh "$i" "$1"; done ; }
2013-04-12 11:34:02
User: zlemini
Functions: ssh
2

Oneliner to run commands on multiple servers over ssh.

- First parameter "$1" is the command you want to execute remotely.

( It can be multiple commands e.g. "hostname;uptime")

- Second parameter "${@:2}" represents the remote host/s you want to run the command/s on.

prompt (){ if [ "$1" = "on" ]; then . ~/.bash_profile; else if [ "$1" = "off" ];then PS1="$ ";fi;fi; }
2013-02-17 13:41:18
User: zlemini
1

Bash function to change your default prompt to something simpler and restore it to normal afterwards.

diff -BI '^#' file{1,2}
python -c "help('modules')"
echo /home/foo/dir1/bar | awk -F/ -v OFS=/ '{$3 = "dir2"}1'
2012-10-26 16:17:20
User: zlemini
Functions: awk echo
1

Awk replaces the value of a specific field while retaining the field separator "/" .

grep -- -- file
xmlstarlet ed -s "/breakfast_menu/food" -t elem -n 'foo' -v "bar" simple.xml
2012-06-03 18:35:53
User: zlemini
Functions: ed
5

This inserts an element as last child under /breakfast_menu/food in simple.xml.

xml used - http://www.w3schools.com/xml/simple.xml

perl -e 'printf "%04o\n", (stat shift)[2] & 0777;' file
2012-03-22 15:05:04
User: zlemini
Functions: perl stat
1

This prints file access rights in octal - useful when "stat" is unavailable.

pscp -h hosts.txt -l username /etc/hosts /tmp/hosts
2012-01-24 12:19:30
User: zlemini
13

You can push files to up to 32 servers at once assuming ssh keys are in place.

Great tool, it is part of the pssh suite.

ssh -t remotebox "tail -f /var/log/remote.log"
2011-12-12 20:24:23
User: zlemini
Functions: ssh
12

This is also handy for taking a look at resource usage of a remote box.

ssh -t remotebox top
find / -type f -size +500M -size -1G
awk '{gsub("foo","bar",$5)}1' file
2011-11-09 18:24:23
User: zlemini
Functions: awk
12

Awk replaces every instance of foo with bar in the 5th column only.

awk '{$1=$3=""}1' file
2011-10-25 22:15:06
User: zlemini
Functions: awk
28

Print all columns except the 1st and 3rd.

wget -b http://dl.google.com/android/android-sdk_r14-linux.tgz
sudo lsof -rc command >> /tmp/command.txt
2011-08-03 20:19:53
User: zlemini
Functions: command sudo
6

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
2

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
2

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
9

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
5

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