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.

Universal configuration monitoring and system of record for IT.
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

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!
Hide

Top Tags

Hide

Functions

Commands using sudo from sorted by
Terminal - Commands using sudo - 441 results
sudo mount -o remount,rw / && sudo cp /etc/hosts /etc/hosts.old && wget http://winhelp2002.mvps.org/hosts.txt && cp /etc/hosts ~/ && cat hosts.txt >> hosts && sudo cp hosts /etc/hosts
2016-06-06 15:01:19
User: bugmenot
Functions: cat cp mount sudo wget
1

Will append lines to the hosts file to do some basic ad blocking.

ASN=32934; for s in $(whois -H -h riswhois.ripe.net -- -F -K -i $ASN | grep -v "^$" | grep -v "^%" | awk '{ print $2 }' ); do echo " blocking $s"; sudo iptables -A INPUT -s $s -j REJECT &> /dev/null || sudo ip6tables -A INPUT -s $s -j REJECT; done
sudo system-image-cli --switch ubuntu-touch/rc-proposed/ubuntu --build 0
2016-02-07 18:24:28
User: bugmenot
Functions: sudo
Tags: ubuntu-phone
0

Get the latest and hopefully greatest to test out on the Ubuntu Phone - sometimes broken but always interesting. https://developer.ubuntu.com/en/start/ubuntu-for-devices/image-channels/

sudo mount -o remount,rw /
2016-02-07 17:57:19
User: bugmenot
Functions: mount sudo
0

Warning this will allow you to write to the system image on the phone, not recommended. But sometimes useful.

sudo strace -e trace=network -p `pidof nginx | sed -e 's/ /,/g'`
2016-01-28 18:48:16
Functions: sed strace sudo
1

Nginx (and other webservers like Apache) can be awkward to trace. They run as root, then switch to another user once they're ready to serve web pages. They also have a "master" process and multiple worker processes.

The given command finds the process IDs of all Nginx processes, joins them together with a comma, then traces all of them at once with "sudo strace." System trace output can be overwhelming, so we only capture "networking" output.

TIP: to kill this complex strace, do "sudo killall strace".

Compare with a similar command: http://www.commandlinefu.com/commands/view/11918/easily-strace-all-your-apache-processes

sudo -E rpm -Uvh "http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm"
2015-10-29 14:06:48
User: zlemini
Functions: rpm sudo
1

In this case the current user has proxy variable set which allows access to the rpm on the internet but needs root privs to install it.

Running sudo -E preserves the current user proxy var and allows the rpm install to be executed with sudo.

sudo aa-complain /etc/apparmor.d/usr.bin.chromium-browser
2015-09-30 18:56:28
User: lordtoran
Functions: sudo
0

If AppArmor is installed with all profiles enforced, Chromium may refuse to start, printing a "failed to launch zygote process" error on stderr.

This command switches the Chromium profile from enforce to complain mode, effectively un-sandboxing the browser, but depending on the system setup it may be a smaller deal than a nonfunctional application.

sudo sh -c "aptitude update; aptitude -DrVWZ full-upgrade; aptitude autoclean; exit"
2015-09-28 23:48:05
User: lordtoran
Functions: sh sudo
2

This has been my "sysupgrade" alias since ca. 2006, first used on Debian Sid, then Sabayon, and it still does its duty on Mint nowadays without breaking stuff.

sudo sh -c "rm /usr/share/mime/packages/kde.xml && update-mime-database /usr/share/mime"
echo apt-get\ {update,-y\ upgrade}\ \&\& true | sudo bash
2015-09-22 00:48:26
User: alecthegeek
Functions: echo sudo true
1

it's nice to be able to use the command `ls program.{h,c,cpp}`. This expands to `ls program.h program.c program.cpp`. Note: This is a text expansion, not a shell wildcard type expansion that looks at matching file names to calculate the expansion. More details at http://www.linuxjournal.com/content/bash-brace-expansion

I often run multiple commands (like apt-get) one after the other with different subcommands. Just for fun this wraps the whole thing into a single line that uses brace expansion.

sudo lsof -nP | awk '/deleted/ { sum+=$8 } END { print sum }'
2015-09-19 00:45:23
Functions: awk sudo sum
0

A potential source of a full filesystem are large files left open but have been deleted. On Linux, a file may be deleted (removed/unlinked) while a process has it open. When this happens, the file is essentially invisible to other processes, but it still takes on physical space on the drive. Tools like du will not see it.

sudo apt-get remove --purge $(dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d')
sudo sh -c 'printf "[SeatDefaults]\nallow-guest=false\n" >/usr/share/lightdm/lightdm.conf.d/50-no-guest.conf'; sudo sh -c 'printf "[SeatDefaults]\nallow-guest=false\n" >/usr/share/lightdm/lightdm.conf.d/50-guest-wrapper.conf'
2015-08-31 18:12:21
User: andregyn62
Functions: sh sudo
-2

This command will disable a guest user logon, this user don't have password to login in the system.

sudo apt-get install ufraw
2015-08-20 20:37:00
User: dnlcorrea
Functions: install sudo
-1

Convert RAW files (eg. .CR2) to JPEGs, PNGs and whatnot.

sudo lsof | egrep 'w.+REG' | awk '{print $10}' | sort | uniq -c | sort -n
2015-08-18 14:09:02
User: kennethjor
Functions: awk egrep sort sudo uniq
1

This command run fine on my Ubuntu machine, but on Red Hat I had to change the awk command to `awk '{print $10}'`.

sudo docker rm $(docker ps -a -q); sudo docker rmi $(docker images -q)
2015-05-20 12:34:40
User: lpalgarvio
Functions: ps rm sudo
3

# Delete all containers

docker rm $(docker ps -a -q)

# Delete all images

docker rmi $(docker images -q)

if [[ $(expr $(date +%s) - $(stat -c %X /var/lib/apt/periodic/update-success-stamp)) -gt 86400 ]]; then sudo apt-get update fi
2015-05-12 14:45:11
User: gargolito
Functions: date expr stat sudo
0

I have this in my .bash_aliases and call it before running apt-get install or apt-get upgrade

Example:

alias apt-install='apt-update; apt-get install'

alias apt-upgrade='apt-update; apt-get upgrade'

function apt-update () {

if [[ $(expr $(date +%s) - $(stat -c %X /var/lib/apt/periodic/update-success-stamp)) -gt 86400 ]]; then

sudo apt-get update

else

echo apt is up to date

fi

}

sudo mysql -sNe 'show tables like "PREFIX_%"' DBNAME | xargs sudo mysqldump DBNAME > /tmp/dump.sql
sudo npm cache clean -f | sudo npm install -g n | sudo n stable
sudo mtr -s 1472 -B 0 -oLDRSWNBAWVJMXI <ip address>
2015-04-24 13:46:36
User: bort
Functions: sudo
0

Uses packet size 1472, padded with zeroes, and shows more columns with jitter statistics to the right. This is a greatly improved version of traceroute.

sudo lsof -i -n | grep sshd | grep sshuser | grep :[PORT-RANGE] | grep -v IPv6 | awk -F\: '{print $2}' | grep -v http | awk -F" " '{print $1}'
2015-04-09 15:41:11
User: das_shark
Functions: awk grep sshd sudo
-2

gets network ports

only ones for the sshd service

only logged in a specific user (changed for public posting)

only in a specific localhost:port range

not IPv6

Only the part of the response after the ":" character

Only the part of the response before the 1st space

Output is just the rssh port

ssh [email protected] sudo date -s @`( date -u +"%s" )`
sudo apt-get purge $(dpkg -l linux-{image,headers}-"[0-9]*" | awk '/ii/{print $2}' | grep -ve "$(uname -r | sed -r 's/-[a-z]+//')")
sudo sh -c 'echo 1 > /proc/sys/kernel/dmesg_restrict'
2015-03-13 20:54:45
User: Blacksimon
Functions: sh sudo
1

Linux offers an interesting option to restrict the use of dmesg. It is available via /proc/sys/kernel/dmesg_restrict.

You can check the status with:

cat /proc/sys/kernel/dmesg_restrict

Alternatively you can use sysctl:

sudo sysctl -w kernel.dmesg_restrict=1

To make your change persistent across reboot, edit a fille in /etc/sysctl.d/.

sudo iptables -A INPUT -m limit --limit 2000/sec -j ACCEPT && sudo iptables -A INPUT -j DROP
2015-03-09 20:16:17
User: qdrizh
Functions: iptables sudo
Tags: iptables
1

VPS server hosts suspect DOS attack if PPS is too high. This limits packets at the interface level. Do "sudo apt-get install iptables-persistent" to make persistent, or, if you already have, reconfigure with "sudo dpkg-reconfigure iptables-persistent"