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 tagged bash from sorted by
Terminal - Commands tagged bash - 712 results
curl ifconfig.me
2014-11-02 21:57:52
User: adria
0

http://ifconfig.me is a web site which shows your public iP address.

If you want use your own code, you can use PHP code such as:

function getip()

{

return "".$_SERVER["REMOTE_ADDR"];

}

echo getip();

?>

Then, you'll get your IP by 'curl yourfile.php'.

echo $(wget http://ipecho.net/plain -q -O -)
2014-10-25 20:25:05
User: KonKar
Functions: echo wget
0

Easiest way to get the external IP address.

cat /etc/named.conf | grep -Po '(?<=(named/)).*(?=\.db)'
2014-10-25 20:16:17
User: KonKar
Functions: cat grep
0

Allows you to have a list of the domains on the server.

gcloud components list | grep "^| Not" | sed "s/|\(.*\)|\(.*\)|\(.*\)|/\2/" | xargs echo gcloud components update
2014-10-13 20:52:25
User: wires
Functions: echo grep sed xargs
0

Google Cloud SDK comes with a package manager `gcloud components` but it needs a bit of `sed` to work. Modify the "^| Not" bit to change the package selection. (The gcloud --format option is currently broken)

firefox $(grep -i ^url=* file.url | cut -b 5-)
2014-10-08 05:56:27
User: nachos117
Functions: cut grep
0

This command will use grep to read the shortcut (which in the above examle is file.url), and filter out all but the only important line, which contains the website URL, and some extra characters that will need to be removes (for example, URL=http://example.com). The cut command is then used to get rid of the URL= at the beginning. The output is then piped into Firefox, which should interpret the it as a web URL to be opened. Of course, you can replace Firefox with any other broswer. Tested in bash and sh.

finger $(whoami) | perl -ne '/Name: ([a-zA-Z0-9 ]{1,})/ && print "$1\n"'
finger $(whoami) | egrep -o 'Name: [a-zA-Z0-9 ]{1,}' | cut -d ':' -f 2 | xargs echo
2014-09-24 01:22:07
User: swebber
Functions: cut egrep finger xargs
0

Its possible to user a simple regex to extract de username from the finger command.

The final echo its optional, just for remove the initial space

curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"GUI.ShowNotification","params":{"title":"This is the title of the message","message":"This is the body of the message"},"id":1}' http://i3c.pla.lcl:8080/jsonrpc
2014-08-24 21:49:13
User: PLA
0

Send a text message to an Kodi (XBMC) device. Uses curl to post a JSON request to the Kodi JSON-RPC API.

function say { wget -q -U Mozilla -O google-tts.mp3 "http://translate.google.com/translate_tts?ie=UTF-8&tl=$1&q=$2" open google-tts.mp3 &>/dev/null || mplayer google-tts.mp3 &>/dev/null; rm google-tts.mp3; }
2014-08-01 23:43:16
User: Zath
Functions: rm wget
0

Improved google text-to-speech function. Allows to specify language, plays sound in terminal. Automatically removes downloaded file after successfully processing.

Usage:

say LANGUAGE TEXT

Examples:

say en "This is a test." say pl "To jest test"
apt-get update && apt-get dist-upgrade -y --show-progress && apt-get autoremove -y && apt-get check && apt-get autoclean -y
0

# AllInOne: Update what packages are available, upgrade to new versions, remove unneeded packages

# (some are no longer needed, replaced by the ones from ap upgrade), check for dependencies

# and clean local cached packages (saved on disk but not installed?,some are needed? [this only cleans unneeded unlike ap clean]).

# aliases (copy into ~/.bashrc file):

alias a='alias'

a ap='apt-get'

a r='ap autoremove -y'

a up='ap update'

a u='up && ap upgrade -y --show-progress && r && ap check && ap autoclean'

# && means "and run if the previous succeeded", you can change it to ; to "run even if previous failed".

I'm not sure if ap check should be before or after ap upgrade -y, you can also change the alias names.

# To expand aliases in bash use ctrl alt e or see this ow.ly/zBKHs

# For more useful aliases go to ow.ly/zBMOx

echo {-1..-5}days | xargs -n1 date +"%Y-%m-%d" -d
blkid
fn() { find . -iname "*$1*" -print; }
2014-07-15 05:30:59
User: suprjami
Functions: find
0

A simple bash function to the find command. I use this much more than find itself.

cut -f 2 -d ':' oclHashcat.pot | egrep -oi '[a-z]{1,20}' | sort | uniq > base.pot
perl -M URI::Escape -lne 'print uri_unescape($_)'
env PS4=' ${BASH_SOURCE:-0$}:${LINENO}(${FUNCNAME[0]}) ' sh -x /etc/profile
watch -n10 --no-title "w3m http://www.livescore.com/ |egrep 'live [0-9H]+[^ ]'"
2014-06-12 21:44:26
Functions: watch
Tags: bash livescore
1

World Cup Live Score of the ongoing match.

Alternative to have the live score with the match statistics:

watch -n10 --no-title "w3m http://www.livescore.com/ |awk '/live [0-9H]+[^ ]/,/red cards/'"
F=136; [[ $(($F % 4)) == 0 ]] && for i in $(seq 1 $(($F/4))); do echo -n $(($F-2*($i-1))),$((2*$i-1)),$((2*$i)),$(($F-2*$i+1)),; done | sed 's/,$/\n/' || echo "Make F a multiple of 4."
2014-06-11 01:21:08
User: Kayvlim
Functions: echo sed seq
0

Useful if you don't have at hand the ability to automatically create a booklet, but still want to.

F is the number of pages to print. It *must* be a multiple of 4; append extra blank pages if needed.

In evince, these are the steps to print it, adapted from https://help.gnome.org/users/evince/stable/duplex-npage.html.en :

1) Click File ▸ Print.

2) Choose the General tab.

Under Range, choose Pages.

Type the numbers of the pages in this order (this is what this one-liner does for you):

n, 1, 2, n-1, n-2, 3, 4, n-3, n-4, 5, 6, n-5, n-6, 7, 8, n-7, n-8, 9, 10, n-9, n-10, 11, 12, n-11...

...until you have typed n-number of pages.

3) Choose the Page Setup tab.

- Assuming a duplex printer:

Under Layout, in the Two-side menu, select Short Edge (Flip).

- If you can only print on one side, you have to print twice, one for the odd pages and one for the even pages.

In the Pages per side option, select 2.

In the Page ordering menu, select Left to right.

4) Click Print.

sed -i 's/^.*?].*?:\s//g' skype-chat-log.txt
2014-05-26 07:48:36
Functions: sed
0

Regular expression search pattern to remove the Datetime and Name when you paste from skype chat into your text editor

From this Gist:

https://gist.github.com/webstandardcss/3967760857d6be470dda

MYURL=http://test.example.com ; awk -F/ '{ print $3 }' <<< $MYURL | awk -F. '{ if ( $(NF-1) == "co" || $(NF-1) == "com" ) printf $(NF-2)"."; print $(NF-1)"."$(NF); }'
2014-05-26 07:31:40
User: snafu
Functions: awk printf
Tags: bash url domain
0

Extracts domain and subdomain from given URl. See examples.

alias pp='python -mjson.tool|pygmentize -l js'
2014-05-22 10:29:44
User: wires
Functions: alias
Tags: bash json
5

Leave out pygmentize or `pip install pygments` first.

psql -X -A -t -c "SELECT version();"
2014-05-01 18:10:20
User: malathion
0

Without using a pipe.

-X ignores the user's .psqlrc configuration file

-A sets un-aligned table output mode

-t prints rows only (no headers or footers)

psql -h <SERVER NAME HERE> -t -c 'SELECT version();' |head -1
rtfm() { help $@ || man $@ || xdg-open "http://www.google.com/search?q=$@"; }
2014-04-25 04:17:03
User: KlfJoat
Functions: man
1

Same as the other rtfm's, but using the more correct xdg-open instead of $BROWSER.

I can't find a way to open info only if the term exists, so it stays out of my version.