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.


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

Psst. Open beta.

Wow, didn't really expect you to read this far down. The latest iteration of the site is in open beta. It's a gentle open beta-- not in prime-time just yet. It's being hosted over at UpGuard (link) and you are more than welcome to give it a shot. Couple things:

  • » The open beta is running a copy of the database that will not carry over to the final version. Don't post anything you don't mind losing.
  • » If you wish to use your user account, you will probably need to reset your password.
Your feedback is appreciated via the form on the beta page. Thanks! -Jon & CLFU Team

All commands from sorted by
Terminal - All commands - 12,352 results
dnstop -l 3 enp1s0f0
./mpv_identify.sh * | egrep '^video_aspect=1\.[67]'
2015-09-29 05:47:07
User: Tatsh
Functions: egrep
0

Checks if the aspect ratio is approximately 1.6 (16:10) or ~1.77777778 (16:9).

Requires mpv_identify from mpv project https://github.com/mpv-player/mpv/blob/master/TOOLS/mpv_identify.sh (this replaces mplayer -identify, or midentify)

pkill imwheel && imwheel &
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.

pkill plasma-desktop && plasma-desktop
2015-09-28 23:36:24
User: lordtoran
0

This can help with serious GUI lockups in KDE. It will only restart Plasma + widgets, not your session, so all your programs will stay running.

sudo sh -c "rm /usr/share/mime/packages/kde.xml && update-mime-database /usr/share/mime"
wallpaper -w 800 -x 16384 -a 1.7 -b 1.85 *
2015-09-28 23:18:39
User: lordtoran
0

List images between 800 and 16384 pixels wide that can be displayed sized and trimmed on a widescreen display.

Requires the "wallpaper" utility.

echo "BTC rate is" $(wget https://api.bitcoinaverage.com/ticker/global/EUR/ -q -O - | jq ".last") "?"
2015-09-28 23:03:59
User: lordtoran
Functions: echo wget
Tags: wget bitcoin jq btc
0

Returns the global weighted BTC rate in EUR. Requires the "jq" JSON parser.

ps h -o %a 21679
2015-09-27 11:00:07
User: BeniBela
Functions: ps
Tags: Linux ps
3

Show the command line for a PID with ps

tr '\0' ' ' </proc/21679/cmdline ; echo
xargs -0a /proc/27288/cmdline echo
2015-09-25 17:35:11
User: dennisw
Functions: xargs
Tags: Linux /proc
7

If you cat the file, all the parts of the command line are bunched up. If you use tr to convert the nulls to spaces, you're still left without a newline unless you add another step. This command does everything for you.

xidel --quiet 'http://www.ebay.com/sch/Beds-Bed-Frames-/175758/i.html?_from=R40&_sac=1&_sop=15&_nkw=bed' -e 'css(".lvresult")[not(matches(., "Pickup only|BUILDING PLANS \(ONLY\)|PARTS LIST"))]/css(".lvtitle")/(.||": "[email protected])' -f 'css("a.gspr.next")'
2015-09-24 12:11:13
User: BeniBela
0

The ebay URL is the search query copied from the browser (here searching for bed stuff). The regex is for crap that should not be shown. Important is "Pickup only", which filters away all the things that cannot be shipped (a filter that cannot be done on Ebay's webpage).

It will output the title and url for all matching items, for all pages of the search result.

It also works for the German Ebay.

curl -q -s http://www.hasthelhcdestroyedtheearth.com/ | sed -En '/span/s/.*>(.*)<.*/\1/p'
2015-09-23 23:26:30
User: Tatsh
Functions: sed
0

Use -q as first argument (as described in `man curl`) to ignore curlrc to ensure the output is always the same regardless of user's configuration.

while true; do (echo -n $(date +"%F %T"):\ ; xwininfo -id $(xprop -root|grep "ACTIVE_WINDOW("|cut -d\ -f 5) | grep "Window id" | cut -d\" -f 2 ) >> logfile; sleep 60; done
2015-09-23 23:00:14
User: BeniBela
Functions: cut date echo grep sleep
1

This logs the titles of the active windows, thus you can monitor what you have done during which times. (it is not hard to also log the executable name, but then it is gets too long)

xidel --quiet http://www.hasthelhcdestroyedtheearth.com/ -e //span
2015-09-23 12:31:55
User: BeniBela
1

This says if the LHC has destroyed the world. Run it in a loop to monitor the state of Earth. Might not work reliable, if the world has actually been destroyed.

curl -s http://www.hasthelhcdestroyedtheearth.com/ | sed -En '/span/s/.*>(.*)<.*/\1/p'
2015-09-23 12:30:31
User: BeniBela
Functions: sed
1

This says if the LHC has destroyed the world. Run it in a loop to monitor the state of Earth. Might not work reliable, if the world has actually been destroyed.

find . -iname '*png' -exec pngcrush -ow -brute {} {}.crush \;
2015-09-22 11:10:16
User: miniker84
Functions: find
Tags: GNU find
1

Find all pngs in directory structure and pngcrush them, none destructive. You can just remove the "{}.crush" part if you want destructive.

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.

while true; do nc -z localhost 3333 >|/dev/null || (ssh -NfL 3333:REMOTE_HOST:5432 [email protected]_HOST); sleep 15; done
2015-09-21 02:25:49
User: rxw
Functions: sleep ssh
-1

Check if SSH tunnel is open and open it, if it isn't.

NB: In this example, 3333 would be your local port, 5432 the remote port (which is, afaik, usually used by PostgreSQL) and of course you should replace REMOTE_HOST with any valid IP or hostname. The example above let's you work on remote PostgreSQL databases from your local shell, like this:

psql -E -h localhost -p 3333
F=bigdata.xz; lsof -o0 -o -Fo $F | awk -Ft -v s=$(stat -c %s $F) '/^o/{printf("%d%%\n", 100*$2/s)}'
2015-09-19 22:22:43
User: flatcap
Functions: awk stat
7

Imagine you've started a long-running process that involves piping data,

but you forgot to add the progress-bar option to a command.

e.g.

xz -dc bigdata.xz | complicated-processing-program > summary

.

This command uses lsof to see how much data xz has read from the file.

lsof -o0 -o -Fo FILENAME

Display offsets (-o), in decimal (-o0), in parseable form (-Fo)

This will output something like:

.

p12607

f3

o0t45187072

.

Process id (p), File Descriptor (f), Offset (o)

.

We stat the file to get its size

stat -c %s FILENAME

.

Then we plug the values into awk.

Split the line at the letter t: -Ft

Define a variable for the file's size: -s=$(stat...)

Only work on the offset line: /^o/

.

Note this command was tested using the Linux version of lsof.

Because it uses lsof's batch option (-F) it may be portable.

.

Thanks to @unhammer for the brilliant idea.

f=bigdata.xz; calc "round($(lsof -o0 -o "$f"|awk '{o=substr($7,3)}END{print o}')/$(stat -c %s "$f")*100)"
2015-09-19 18:27:12
User: unhammer
2

Say you're started "xzcat bigdata.xz | complicated-processing-program >summary" an hour ago, and you of course forgot to enable progress output (you could've just put "awk 'NR%1000==0{print NR>"/dev/stderr"}{print}'" in the pipeline but it's too late for that now). But you really want some idea of how far along your program is. Then you can run the above command to see how many % along xzcat is in reading the file.

Note that this is for the GNU/Linux version of lsof; the one found on e.g. Darwin has slightly different output so the awk part may need some tweaks.

curl --write-out %{http_code} --connect-timeout 10 --max-time 20 -s -I -L http://google.com --output /dev/null
2015-09-19 17:29:32
User: Guyverix
0

Basic check to see if a website (or even an API) is responding as expected. If this is put in a case statement, there would be enough flexibility to monitor just about anything that gives an HTTP response code, as well as the network path to the site looking at curl exit codes. As this is for a possible monitor, timeouts have been added to make sure that iteration overruns do not occur. ( hopefully, curl sometimes "sticks" longer than expected )

while cat energy_now; do sleep 1; done |awk -v F=$(cat energy_full) -v C=60 'NR==1{P=B=$1;p=100/F} {d=$1-P; if(d!=0&&d*D<=0){D=d;n=1;A[0]=B=P}; if(n>0){r=g=($1-B)/n;if(n>C){r=($1-A[n%C])/C}}; A[n++%C]=P=$1; printf "%3d %+09.5f %+09.5f\n", p*$1, p*g, p*r}'
2015-09-19 15:45:40
User: sqweek
Functions: awk cat printf sleep
-1

Needs to be run in a battery sysfs dir, eg. /sys/class/power_supply/BAT0 on my system.

Displays the battery's current charge and the rate per-second at which energy is {dis,}charging. All values are displayed as percentages of "full" charge.

The first column is the current charge. The second is the rate of change averaged over the entire lifetime of the command (or since the AC cable was {un,}plugged), and the third column is the rate of change averaged over the last minute (controlled by the C=60 variable passed to awk).

The sample output captures a scenario where I ran 'yes' in another terminal to max out a CPU. My battery was at 76% charge and you can see the energy drain starts to rise above 0.01% per-second as the cpu starts working and the fan kicks in etc. While idle it was more like 0.005% per-second.

I tried to use this to estimate the remaining battery life/time until fully charged, but found it to be pretty useless... As my battery gets more charged it starts to charge slower, which meant the estimate was always wrong. Not sure if that's common for batteries or not.

[ $(date +"%H") -lt 7 ] && echo you should probably be sleeping...
weather() { curl -s "http://www.wunderground.com/q/zmw:$1.1.99999" | grep "og:title" | cut -d\" -f4 | sed 's/&deg;/ degrees F/'; }