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 linuxrawkstar from sorted by
Terminal - Commands by linuxrawkstar - 35 results
cp -a /etc /destination
ls -ltp | sed '1 d' | head -n1
2011-10-17 16:21:15
Functions: head ls sed
-2

wrap it in a function if you like...

lastfile () { ls -ltp | sed '1 d' | head -n1 }
while :; do sensors|grep ^Core|while read x; do printf '% .23s\n' "$x"; done; sleep 1 && clear; done;
2011-04-20 06:41:57
Functions: grep printf read sleep
1

Watch the temperatures of your CPU cores in real time at the command line. Press CONTROL+C to end.

GORY DETAILS: Your computer needs to support sensors (many laptops, for example, do not). You'll need to install the lm-sensors package if it isn't already installed. And it helps to run the `sensors-detect` command to set up your sensor kernel modules first. At the very end of the sensors-detect interactive shell prompt, answer YES to add the new lines to the list of kernel modules loaded at boot.

!!command
2011-03-04 22:34:22
Tags: vim
10

in command mode, navigate your cursor to the line where you want the command output to appear, and hit "!!". No need to enter edit mode or even type a ":" (colon).

pgrep -c 'httpd|apache2'
2011-02-24 18:03:00
Tags: httpd
-1

this version is going to work on redhat/centos/suse AND ubuntu/debian systems

sudo arp-scan -l
2011-02-21 21:58:27
Functions: sudo
Tags: Arp arp-scan
8

A much quicker and (not dirtier) option. use the man page for help. On linux/ubuntu you will need to `sudo apt-get -y install arp-scan`.

for file in $( vmrun list | grep 'vmx$' | sort ); do printf "% 40s %s M\n" $(echo "$( echo -n ${file}:\ ; grep memsize $file )" | sed -e 's/.*\///' -e 's/"//g' -e 's/memsize.=//'); done;
2010-11-19 06:14:11
Functions: echo file grep printf sed sort
Tags: vmware
-1

So your boss wants to know how much memory has been assigned to each virtual machine running on your server... here's how to nab that information from the command line while logged in to that server

echo $ascii | perl -ne 'printf "%x", ord for split //'
find ./ -name '*.sw[op]' -delete
iptraf
sfdisk /dev/sdb <(sfdisk -d /dev/sda| perl -pi -e 's/sda/sdb/g')
2009-12-22 22:45:41
Functions: perl
-3

*as long as the drives are exactly the same* then this command copies the partition table on /dev/sda to /dev/sdb

mutt your@email_address.com -s "Message Subject Here" -a attachment.jpg </dev/null
2009-12-10 22:49:38
Tags: mutt
4

Send it the easy way, with MIME and everything handled for you by an app that knows best.

pwgen -Bs 10 1
2009-12-01 14:33:51
14

-B flag = don't include characters that can be confused for other characters (this helps when you give someone their password for the first time so they don't cause a lockout with, for example, denyhosts or fail2ban)

-s flag = make a "secure", or hard-to-crack password

-y flag = include special characters (not used in the example because so many people hate it -- however I recommend it)

"1 10" = output 1 password, make it 10 characters in length

For even more secure passwords please use the -y flag to include special characters like so:

pwgen -Bsy 10 1

output>> }&^Y?.>7Wu

flip -u $FILE
2009-10-26 17:33:26
Tags: flip
0

conversely, you can type

flip -m

to convert the file back to MS line endings.

a=($(ls *html)) && a=${a[$(expr ${#a[@]} - 1)]} && rm $a
2009-10-12 16:40:06
Functions: expr ls rm
-3

plays with bash arrays. instead of storing the list of files in a temp file, this stores the list in ram, retrieves the last element in the array (the last html file), then removes it.

VBoxManage internalcommands converttoraw winxp.vdi winxp.raw && qemu-img convert -O vmdk winxp.raw winxp.vmdk && rm winxp.raw
2009-10-12 16:23:37
Functions: rm
9

Converts a .vdi file to a .vmdk file for use in a vmware virtual machine. The benefit: using this method actually works. There are others out there that claim to give you a working .vmdk by simply using the qemu-img command alone. Doing that only results in pain for you because the .vmdk file will be created with no errors, but it won't boot either.

Be advised that these conversions are very disk-intensive by nature; you are probably dealing with disk images several gigabytes in size.

Once finished, the process of using the new .vmdk file is left as an exercise to the reader.

perl -pi -e 's/([[:lower:]]+)/uc $1/gsex' file
2009-10-08 14:18:50
Functions: perl
Tags: perl
-2

same, except it works on any OS with Perl installed. DOS, Windose, whatever

ls -A
beepwhenup () { echo 'Enter host you want to ping:'; read PHOST; if [[ "$PHOST" == "" ]]; then exit; fi; while true; do ping -c1 -W2 $PHOST 2>&1 >/dev/null; if [[ "$?" == "0" ]]; then for j in $(seq 1 4); do beep; done; ping -c1 $PHOST; break; fi; done; }
2009-09-24 18:11:10
Functions: echo host ping read seq
Tags: ping beep
2

After this, just type:

beepwhenup

You need to install "beep" before this would make the beep sound.

Save it in your .profile if you want to use it later

WARNING: this command won't exit until it is successful. You won't be able to CONTROL+C out of it.

uuid
2009-08-14 14:02:37
-1

http://en.wikipedia.org/wiki/Universally_Unique_Identifier

http://www.ossp.org/pkg/lib/uuid/

You'll first need to install the uuid package. Available immediately from apt in Ubuntu, Debian, or other myriad Debian variants. You could always compile it from source as well.

curl http://www.commandlinefu.com/commands/by/<your username>/rss|gzip ->commandlinefu-contribs-backup-$(date +%Y-%m-%d-%H.%M.%S).rss.gz
2009-08-10 12:43:33
Functions: date gzip
10

Use `zless` to read the content of your *rss.gz file:

zless commandlinefu-contribs-backup-2009-08-10-07.40.39.rss.gz
VBoxManage list vms
2009-08-07 12:43:04
2

A similar command that lists only the currently running VMs is thus:

VBoxManage list runningvms

...the above showing a list of VMs by name and UUID in the same format as the "$ VBoxManage list vms" command

VBoxHeadless -s <name|uuid>
2009-08-07 12:36:32
5

you can launch a VirtualBox VM from the command line using VBoxManage, but that invokes it in a gui environment.

If you want to just fire off your VM in the background, use VBoxHeadless as shown. To get the names and UUIDs of your VirtualBox VMs, type:

VBoxManage list