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 sudo from sorted by
Terminal - Commands using sudo - 406 results
sudo snmptrap -m ALL -v 2c -c public trapserver "" UCD-DEMO-MIB::ucdDemoPublic SNMPv2-MIB::sysLocation.0 s "Just here"
sudo cat /usr/share/icons/*/*/* > /dev/dsp
2009-08-06 03:12:27
User: 12345678
Functions: cat sudo
0

Plays the sound of the file, should sound like *some* kind of music, most files sound like static but some are really cool.

variations:

sudo cat /dev/sda > /dev/dsp sudo cat /dev/sda5 | aplay

Check out http://bbs.archlinux.org/viewtopic.php?id=70937 for more variations!

semi-dupe--like http://www.commandlinefu.com/commands/view/985/generate-white-noise but with different syntax and program.

curl -s http://isc.sans.org/sources.html|grep "ipinfo.html"|awk -F"ip=" {'print $2'}|awk -F"\"" {'print $1'}|xargs -n1 sudo iptables -A OUTPUT -j DROP -d > 2&>1
tmpfile=$(mktemp) && echo -e 'startup_message off\nscreen -t top htop\nsplit\nfocus\nscreen -t nethogs nethogs wlan0\nsplit\nfocus\nscreen -t iotop iotop' > $tmpfile && sudo screen -c $tmpfile
2009-08-03 10:14:02
User: Patola
Functions: echo screen sudo top
18

This command starts screen with 'htop', 'nethogs' and 'iotop' in split-screen. You have to have these three commands (of course) and specify the interface for nethogs - mine is wlan0, I could have acquired the interface from the default route extending the command but this way is simpler.

htop is a wonderful top replacement with many interactive commands and configuration options. nethogs is a program which tells which processes are using the most bandwidth. iotop tells which processes are using the most I/O.

The command creates a temporary "screenrc" file which it uses for doing the triple-monitoring. You can see several examples of screenrc files here: http://www.softpanorama.org/Utilities/Screen/screenrc_examples.shtml

sudo apt-get -o Acquire::http::Dl-Limit=25 install <package>
2009-07-31 19:43:45
User: dunnix
Functions: install sudo
Tags: apt-get
4

apt-get is pretty aggressive when it downloads, potentially hogging the bandwidth of your network. The 25 is in KB, change this to your needs.

sudo wget -c "http://nmap.org/dist/nmap-5.00.tar.bz2" && bzip2 -cd nmap-5.00.tar.bz2 | tar xvf - && cd nmap-5.00 && ./configure && make && sudo make install
2009-07-26 11:36:53
User: hemanth
Functions: bzip2 cd make sudo tar wget
-6

Just copy and paste the code in your terminal.

Note : sudo apt-get for debian versions , change as per your requirement .

Source : www.h3manth.com

ls -drt /var/log/* | tail -n5 | xargs sudo tail -n0 -f
2009-07-22 14:44:41
User: kanaka
Functions: ls sudo tail xargs
Tags: bash tail log watch
5

This command finds the 5 (-n5) most frequently updated logs in /var/log, and then does a multifile tail follow of those log files.

Alternately, you can do this to follow a specific list of log files:

sudo tail -n0 -f /var/log/{messages,secure,cron,cups/error_log}

sudo rmmod pcspkr
2009-07-17 18:21:19
User: zombiedeity
Functions: rmmod sudo
8

To ensure that it will never come back, you can edit /etc/modprobe.d/blacklist

Add "blacklist pcspkr" sans quotes

ran=$(head /dev/urandom | md5sum); MAC=00:07:${ran:0:2}:${ran:3:2}:${ran:5:2}:${ran:7:2}; sudo ifconfig wlan0 down hw ether $MAC; sudo ifconfig wlan0 up; echo ifconfig wlan0:0
2009-07-16 16:21:44
User: workingsmart
Functions: echo head ifconfig sudo
7

Next time you are leaching off of someone else's wifi use this command before you start your bittorrent ...for legitimate files only of course.

It creates a hexidecimal string using md5sum from the first few lines of /dev/urandom and splices it into the proper MAC address format. Then it changes your MAC and resets your wireless (wlan0:0).

sudo nmap -sS 192.168.0.10 -D 192.168.0.2
2009-07-14 17:37:34
User: sedcommand
Functions: sudo
7

Scan for open ports on the target device/computer (192.168.0.10) while setting up a decoy address (192.168.0.2). This will show the decoy ip address instead of your ip in targets security logs. Decoy address needs to be alive. Check the targets security log at /var/log/secure to make sure it worked.

/originalInstall/gem list | tr -d '(),' | xargs -L 1 sudo ./gemInst.sh
2009-07-09 21:46:06
User: snakerdlk
Functions: sudo tr xargs
Tags: bash Linux
0

gemInst.sh:

#!/bin/bash

for i in $@; do

if [ "$1" != "$i" ]

then

echo /newInstall/gem install $1 -v=\"$i\"

/newInstall/gem install $1 -v="$i"

if [ "$?" != "0" ]

then

echo -e "\n\nGEM INSTALL ERROR: $1\n\n"

echo "$1" > gemInst.err

fi

fi

done

sudo port installed | grep -v 'active\|The' | xargs sudo port uninstall
sudo ipfw pipe 1 config bw 50KByte/s;sudo ipfw add 1 pipe 1 src-port 80
2009-07-02 23:17:49
User: miccaman
Functions: sudo
Tags: Os X pipe
1

sudo ipfw pipe 1 config bw 50KByte/s

Set the bandwidth (bw) limit to any number you want. For example you could have a 15kb pipe for X application and then a 100kb pipe for another application and attach things to those pipes. If a port isn’t attached to a pipe, it runs at full speed. Change the number (in this case 1) to a different number for a different pipe.

The next step is to attach your port.

sudo ipfw add 1 pipe 1 src-port 80

In this case anything on port 80 (http) will be set to a limit of 50Kbyte/s. If you want to attach a second port to this pipe, repeat the command but change the port number at the end.

src : http://www.mactricksandtips.com/2008/12/throttling-bandwidth-on-a-mac.html

sudo /Applications/Utilities/Adobe\ Utilities.localized/Adobe\ Updater5/Adobe\ Updater.app/Contents/MacOS/Adobe\ Updater
2009-06-25 08:07:47
User: miccaman
Functions: sudo
Tags: Os X
-9

adobe updater keeps popping up. (normal update fails, probably due to insufficient rights)

(src: http://aricsblog.blogspot.com/2007/12/adobe-updater-crashes-on-mac-os-x-and.html)

sudo sed 's/\o0/\n/g' "/proc/$(pidof -x firefox)/environ" ;# replace firefox
sudo pmset -a hibernatemode 1
2009-06-20 22:52:10
Functions: sudo
-1

sudo pmset -a hibernatemode 1 sets hiberate on.

sudo pmset -a hibernatemode 0 sets hibernate off.

from the pmset man page:

0001 (bit 0) enables hibernation; causes OS X to write memory state to

hibernation image at sleep time. On wake (without bit 1 set) OS X will

resume from the hibernation image. Bit 0 set (without bit 1 set) causes

OS X to write memory state and immediately hibernate at sleep time.

I often change my MacBook's sleep state. So I created a function for bash to make it a little easier.

Usage:

hibernate (on | off)

"hibernate on" will set your laptop to hibernate if the lid is closed.

"hibernate off" will set your laptop to sleep if the lid is closed.

### note : "proper" indentation isn't preserved on the website

function hibernate()

{

case "${1}" in

on)

sudo pmset -a hibernatemode 1

echo Hibernate mode on.

;;

off)

sudo pmset -a hibernatemode 0

echo Hiberate mode off.

;;

*)

echo "I'm sorry Dave, but I can't do that."

;;

esac

}

To make things easier, add the proper line in your /etc/sudoers file so that your user may invoke pmset without a password. Here's how mine looks:

bwayne luna = NOPASSWD: /usr/bin/pmset

Don't forget that you must edit sudoers with `sudo visudo` from Terminal.app, and not by another text editor.

Sorry this is so Mac OS specific.

dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs sudo apt-get -y purge
2009-06-19 10:11:00
User: plasticdoc
Functions: sed sudo xargs
6

will purge:

only installed apps: /^ii/!d

avoiding current kernel stuff: /'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d

using app names: s/^[^ ]* [^ ]* \([^ ]*\).*/\1/

avoiding stuff without a version number: /[0-9]/!d

sudo ifconfig eth0 hw ether 00:01:02:03:04:05
2009-06-04 09:02:35
User: momitov
Functions: ifconfig sudo
6

eth0 = the name of the interface

00:01:02:03:04:05 = the new mac adresse

the same thing for wireless card $ sudo iwconfig eth1 hw ether 00:01:02:03:04:05

sudo dd if=/dev/zero of=/swapfile bs=1024 count=1024000;sudo mkswap /swapfile; sudo swapon /swapfile
2009-05-27 21:10:50
User: dcabanis
Functions: dd mkswap sudo swapon
18

Create a temporary file that acts as swap space. In this example it's a 1GB file at the root of the file system. This additional capacity is added to the existing swap space.

sudo dmidecode -t 5,16
sudo pmset schedule sleep "08/31/2009 00:00:00"
2009-05-14 09:31:32
User: hobzcalvin
Functions: sleep sudo
4

Schedule your Mac to sleep at any future time.

Also wake, poweron, shutdown, wakeorpoweron. Or repeating with

sudo pmset repeat wakeorpoweron MTWRFSU 7:00:00

Query with

pmset -g sched

Lots more at http://www.macenterprise.org/articles/powermanagementandschedulingviathecommandline

function ec() { ec_var="`pwd`" && cd /etc/ && sudo bzr commit -m "$@" && cd $ec_var; }
2009-05-14 04:34:26
Functions: cd sudo
0

ec commits changes to etckeeper

must have etckeeper installed with bzr to use this

place inside bashrc

can be used from any directory to commit changes

sudo shred -zn10 /dev/sda
2009-04-30 13:02:43
User: dcabanis
Functions: shred sudo
-2

Shred can be used to shred a given partition or an complete disk. This should insure that not data is left on your disk

sudo vol_id -u /dev/sda1
2009-04-30 01:40:58
User: agentk
Functions: sudo
3

Shows the UUID of a filesystem or partition that can be used in kernel root options and in fstab.

Run it without the -u option to generate more information. eg:

~/ sudo vol_id /dev/sda2

ID_FS_USAGE=other

ID_FS_TYPE=swap

ID_FS_VERSION=2

ID_FS_UUID=27fca13d-97b7-4d28-882c-6d03353f0a82

ID_FS_UUID_ENC=27fca13d-97b7-4d28-882c-6d03353f0a82

ID_FS_LABEL=

ID_FS_LABEL_ENC=

sudo pmset -a hibernatemode 1