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 mpb from sorted by
Terminal - Commands by mpb - 35 results
ffmpeg -i input.ogv -qscale 0 output.wmv # convert .ogv to .wmv
2014-06-24 16:23:18
User: mpb
0

I needed to convert a screen capture when using Gnome's "recordmydesktop" and convert it to a .wmv for playback in Windows.

C:\> shutdown /f /r /t 0
2014-04-02 22:35:00
User: mpb
Functions: shutdown
-1

Today, I needed to reboot a Windoze machine on another continent which had no shutdown or restart options via "Start" in the remote desktop (the only options available were: "Logoff, Disconnect, or Lock").

Fortunately, I found how to shutdown and restart from the command line.

function hg () { history | grep $* ; } # define a function combining history and grep to save typing :-)
2014-04-02 15:17:31
User: mpb
Functions: grep
4

By defining a function "hg" as shown here, it saves me typing "history | grep" every time I need to search my shell history because now I only have to type "hg".

A nifty time saver :-)

You can also add the "hg" function definition to your .bashrc so it is defined each time you login.

firefox about:robots
2013-07-07 14:12:36
User: mpb
6

Some information about robots. :-)

url=http://mirrors.mageia.org/api/mageia.4.i586.list; wget -q ${url} -O - | grep rsync:
2013-05-20 16:19:05
User: mpb
Functions: grep wget
1

Need to find a Mageia Linux mirror server providing Mageia 4 via rsync?

Modify the "url=" string for the version you want. This shows i586 which is the 32bit version.

If you want the 64bit version it is:

url=http://mirrors.mageia.org/api/mageia.4.x86_64.list; wget -q ${url} -O - | grep rsync:

rpm -qa --queryformat '%{size} %{name}-%{version}-%{release}\n' | sort -k 1,1 -rn | nl | head -16
2013-03-19 21:10:54
User: mpb
Functions: head nl rpm sort
1

Interesting to see which packages are larger than the kernel package.

Useful to understand which RPMs might be candidates to remove if drive space is restricted.

ls -qahlSr # list all files in size order - largest last
2013-03-13 09:52:07
User: mpb
Functions: ls size
1

I find it useful, when cleaning up deleting unwanted files to make more space, to list in size order so I can delete the largest first.

Note that using "q" shows files with non-printing characters in name.

In this sample output (above), I found two copies of the same iso file both of which are immediate "delete candidates" for me.

ls -qaltr # list directory in chronological order, most recent files at end of list
2013-02-25 14:14:44
User: mpb
Functions: at ls
7

I find it very handy to be able to quickly see the most recently modified/created files in a directory.

Note that the "q" option will reveal any files with non-printable characters in their filename.

uname -m # display machine "hardware name"
2013-01-04 11:46:43
User: mpb
Functions: uname
-4

Display the machine "hardware name" 32 or 64 bit.

"x86_64" is shown on 64 bit machines

"i686" is typically shown on 32 bit machines (although, you might also see "i386" or "i586" on older Linuxen).

On other "unix-like" systems, other hardware names will be displayed.

For example, on AIX, "uname -m" gives the "machine sequence number".

For whatever reason, IBM decided that "uname -M" would give the machine type and model.

(ref: http://www.ibm.com/developerworks/aix/library/au-aix-systemid.html )

On Sun Solaris, "uname -m" can be used to determine the chip type and "isainfo -v" will reveal

if the kernel is 64 or 32 bit.

(ref: http://www.ibiblio.org/pub/packages/solaris/sparc/html/32.and.64.bit.packages.html )

A more reliable way to determine "64-bit ness" across different Unix type systems is to compile the following simple C program:

cat <<eeooff > bits.c

/*

* program bits.c

* purpose Display "32" or "64" according to machine type

* written January 2013

* reference http://www.unix.org/whitepapers/64bit.html

*/

/* hmm, curious that angle-brackets removed by commandlinefu.com data input processing? */

#include "/usr/include/stdio.h"

long lv = 0xFFFFFFFF;

main ( ) {

printf("%2d\n",(lv < 0)?32:64);

}

eeooff

Compile and run thusly: cc -o bits bits.c; ./bits

gcal -K -q GB_EN December/2012-January/2013 # Holidays for Dec/2012 and Jan/2013 with week numbers
2012-11-07 18:01:31
User: mpb
0

Display the holidays in December and January for UK/England (2012/2013).

Most Linux distros have "gcal" in their package manager system.

If not, it is available here: http://www.gnu.org/software/gcal

pwgen 10 # generate a table of 10 character random passwords
!}sort -nut. -k 1,1 -k 2,2 -k 3,3 -k 4,4
2012-08-12 16:16:24
User: mpb
0

The vi key sequence "!}" will feed the block of lines from the current position to the next blank line to the command provided: in this case "sort -nut. -k 1,1 -k 2,2 -k 3,3 -k 4,4".

The sort is ascending, numeric (-n), removing duplicates (-u), using "." as key delimiter (-t .").

The "-nut." is a memorable mnemonic :-).

The same command (less the "!}") can, of course, be used from command line to sort a file of IP addresses in a text file.

In the command line version, I found it also useful to remove blank lines and comment lines thusly:

< IPaddresses.txt sed -e "/^#/d" -e "/^$/d" | sort -nut. -k 1,1 -k 2,2 -k 3,3 -k 4,4 # sort IP addresses
/^~
2012-08-02 21:10:23
User: mpb
-5

When searching in vi, the search string gets highlighted but the highlighting can become a nuisance.

By searching for the very unlikely pattern "^~" the highlighting is effectively switched off.

export HISTIGNORE=' cd "`*: PROMPT_COMMAND=?*?'
2011-10-18 19:58:39
User: mpb
Functions: cd export
1

I was surprised to find that with RedHat bash, I could not find any comment lines (begining with #) in my bash shell history. Surprised because in Mageia Linux this works. It turns out that RedHat's bash will keep comment lines if in my .bashrc, I define:

export HISTIGNORE=' cd "`*: PROMPT_COMMAND=?*?'

Why have comment lines in shell history? It's a handy and convenient way to make proto-commands (to be completed later) and for storing brief text data that is searchable in shell history.

< /etc/passwd sed -n "/^bin:/,/^lp:/p"
2011-10-18 13:33:12
User: mpb
Functions: sed
0

[continued]...with "bin:" and line starting with "lp:".

This specific example with /etc/passwd shows the power of sed to extract data from text files.

Here we see an extract from /etc/passwd beginning with the line starting with "bin:" and ending with the line starting with "lp:".

Note also, placing the STDIN redirection at the start of the command makes it easy to recall and modify the command parameters line in shell history.

urpmi.addmedia --update google-talkplugin http://dl.google.com/linux/talkplugin/rpm/stable/$(uname -m | sed -e "s/i.86/i386/")
2011-04-30 23:01:36
User: mpb
Functions: sed uname
0

This command adds a urpmi media source called "google-talkplugin" to the urpmi configuration on Mandriva or Mageia. Needs to be run as root. We specify the option "--update" so that when Google provides a newer version of Google Talk plugin in their download system then running a system update (eg: "urpmi --auto-update") will result in our copy of Google Talk plugin getting updated (along with any other Mandriva/Mageia pending updates).

To install Google Talk plugin from this source, use:

urpmi google-talkplugin # install plugin used for voice and video Google chat via gmail

urpmi.removemedia -a && urpmi.addmedia --distrib --mirrorlist
2011-03-27 15:29:42
User: mpb
1

On Mageia (and Mandriva) Linux, this command will clear your existing urpmi configuration and configure it to use only network sources. It can be useful after you have installed from CD/DVD and

don't want to continually be prompted to insert CD/DVD each time you install a package.

acknowledgement: blino

rpm -q --whatprovides $filename
2011-02-09 23:28:15
User: mpb
Functions: rpm
1

For Linux distributions using rpm (eg Mandriva), this command will find the rpm package name that provides a file.

sort_csn () { echo "${1}" | sed -e "s/,/\n/g"| sort -nu | awk '{printf("%s,",$0)} END {printf("\n")}' | sed -e "s/,$//"; }
2011-01-26 15:18:08
User: mpb
Functions: awk echo sed sort
0

"sort_csn" is a function to sort a comma separated list of numbers.

Define the the function with this:

sort_csn () { echo "${1}" | sed -e "s/,/\n/g"| sort -nu | awk '{printf("%s,",$0)} END {printf("\n")}' | sed -e "s/,$//"; }

Use the function like this:

sort_csn 443,22,80,8200,1533,21,1723,1352,25

21,22,25,80,443,1352,1533,1723,8200

One example where this is useful is when port scanning with nmap and getting a list of open ports in random order. If you use Nessus, you may need to create a scan policy for that set of specific ports and it is clearer to read with the port numbers in ascending order (left to right).

Caveat: no spaces in the comma separated list (just number1,number2,number3,etc).

A variation of this to sort a comma separated list of strings:

sort_css () { echo "${1}" | sed -e "s/,/\n/g"| sort -u | awk '{printf("%s,",$0)} END {printf("\n")}' | sed -e "s/,$//"; }

usage:

sort_css apples,pears,grapes,melons,oranges

apples,grapes,melons,oranges,pears

# cd $partition; dd if=/dev/zero of=ShredUnusedBlocks bs=512M; shred -vzu ShredUnusedBlocks
2009-06-21 14:17:22
User: mpb
Functions: cd dd shred
8

This command securely erases all the unused blocks on a partition.

The unused blocks are the "free space" on the partition.

Some of these blocks will contain data from previously deleted files.

You might want to use this if you are given access to an old computer and you do not know its provenance.

The command could be used while booted from a LiveCD to clear freespace space on old HD.

On modern Linux LiveCDs, the "ntfs-3g" system provides ReadWrite access to NTFS partitions thus enabling this method to also be used on Wind'ohs drives.

NB depending on the size of the partition, this command could take a while to complete.

shred -vzu /tmp/junk-file-to-be-shredded
<<<"k=1024; m=k*k; g=k*m; g" bc
2009-06-17 10:35:10
User: mpb
8

There are two ways to use "here documents" with bash to fill stdin:

The following example shows use with the "bc" command.

a) Using a delimiter at the end of data:

less-than less-than eeooff bc

> k=1024

> m=k*k

> g=k*m

> g

> eeooff

1073741824

b) using the "inline" verion with three less-than symbols:

less-than less-than less-than "k=1024; m=k*k; g=k*m; g" bc

1073741824

One nice advantage of using the triple less-than version is that the command can easily be recalled

from command line history and re-executed.

PS: in this "description", I had to use the name "less-than" to represent the less-than symbol because the commandlinefu input text box seems to eat up the real less-than symbols. Odd.

urpmi --auto-update --force # apply all pending updates (Mandriva Linux)
2009-03-28 14:59:26
User: mpb
0

Update a Mandriva Linux system with any pending updates.

This command needs to be run with root privilege.

Using the "--force" option answers "yes" to any interactive prompts

thus allowing the updates to be left unattended to completion.

NB if there is an update for glibc and/or a new kernel then the system

would need to be rebooted for these to take effect.

A prerequisite for running "urpmi --auto-update" is to have correctly defined

urpmi media sources (which can be done by visiting http://easyurpmi.zarb.org/).

If there are no new updates the the message "Packages are up to date" is shown.

pwgen
2009-03-28 11:43:21
User: mpb
6

See: "man pwgen" for full details.

Some Linux distros may not have pwgen included in the base distribution

so you maye have to install it (eg in Mandriva Linux: "urpmi pwgen").

!}sort
2009-03-28 00:18:39
User: mpb
12

The vi key sequence !}command will send the file contents from the cursor

to the next blank line as STDOUT to the command specified

and replace that sequence of file lines with the output of the command.

For example: sorting a block of data - !}sort

The sequence !{command will do the same but "upwards" (from the current position towards the start of the file.