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 mhs from sorted by
Terminal - Commands by mhs - 20 results
sdparm -s SCT=36000 --save /dev/sdb
2013-04-19 13:26:18
User: mhs
Tags: sdparm
2

So I had this 2TB Seagate external disk/USB enclosure which by default would spin-down its internal drive (it enters a standby mode) after four minutes of inactivity.. Spinning-up the inactive drive was an annoying delay when accessing files and also it severely interfered with NFS.. SCT stands for "Standby Condition Timer".

To completely disable SCT:

sdparm --clear STANDBY -6 /dev/sdb

To return to original (default) SCT settings:

sdparm -D -p 0x1a -6 /dev/sdb

To verify the settings (before and after):

sdparm -a /dev/sdb

No need for vendor-provided MSWIN tools, etc.

prlimit --cpu=10 sort -u hugefile
2013-02-27 15:59:11
User: mhs
Functions: sort
Tags: cpu util-linux
4

Similar to `cpulimit`, although `prlimit` can be found shipped with recent util-linux.

Example: limit CPU consumption to 10% for a math problem which ordinarily takes up 100% CPU:

Before:

bc -l <(echo "1234123412341234^12341234")

See the difference `prlimit` makes:

prlimit --cpu=10 bc -l <(echo "1234123412341234^12341234")

To actually monitor the CPU usage, use `top`, `sar`, etc.. or:

pidstat -C 'bc' -hur -p ALL 1
soffice --headless -convert-to odt:"writer8" somefile.docx
2012-12-27 15:08:38
User: mhs
14

In this example, the docx gets converted to Open Document .odt format.

For other formats, you'll need to specify the correct filter (Hint: see "Comments" link below for a nice list).

adb pull /dev/graphics/fb0 /dev/stdout | ffmpeg -vframes 1 -vcodec rawvideo -f rawvideo -pix_fmt rgb32 -s 480x800 -i pipe:0 -f image2 -vcodec png screenshot.png
2012-12-26 13:21:18
User: mhs
8

Works with *rooted* Android devices. 400x800 are the screen dimensions of a typical handheld smartphone.

adb pull /data/data/com.android.providers.contacts/databases/contacts2.db ; sqlite3 -batch <<EOF contacts2.db <CR> .header on <CR> .mode tabs <CR> select * from data; <CR> EOF
2012-12-26 11:35:23
User: mhs
Functions: tabs
2

Crude, but works. Note for security, /data/ will be inaccessible unless your device has been *rooted*.

On the other hand, if a device has been rooted, its data is now wide open to anyone with a USB cable and the above "one-liner".

`adb` is one of the platform tools in the android SDK.

To get SMS messages:

adb pull /data/data/com.android.providers.telephony/databases/mmssms.db ; sqlite3 -batch <<EOF contacts2.db <CR> .header on <CR> .mode tabs <CR> select * from sms; <CR> EOF
http_proxy=http://127.0.0.1:3128 wget --http-user='domain\account' --http-password='###' -p -r -l 8 --no-remove-listing -P . 'http://sp.corp.com/teams/Team/Shared%20Documents/Forms/AllItems.aspx?RootFolder=%2fteams%2fTeam%2fShared%20Documents%2fFolder'
2012-12-26 09:03:55
User: mhs
Functions: wget
1

If you have to deal with MS Sharepoint which is (rarely, let's hope) used in e.g. certain corporate environments).

This uses Cntlm.

For single files, just use cURL -- its NTLM authentication works quite well.

# /etc/cntlm.conf:

# Username account

# Domain domain

# Password ############

# Proxy 10.20.30.40 (IP of the sharepoint site)

# NoProxy *

# Listen 3128

socat unix-connect:/tmp/socket stdio,echo=0,raw
2012-12-19 07:58:09
User: mhs
1

Create a serial console with "socket (named pipe)" of "/tmp/socket", "from:server, to:virtual machine" in vmware player, etc.. gui. Run the above command after you have booted the guest OS (which should also be configured for serial console).

for i in $(multipath -ll | grep "3PARdata,VV"|awk '{print $1}') ; do parted -a optimal /dev/mapper/$i --script -- mklabel gpt mkpart primary 1 -1 set 1 lvm on ; done
2012-09-12 15:30:36
User: mhs
Functions: awk grep set
1

`multipath -ll` requires Device Mapper multipath.conf configuration. And of course, replace "3PARdata,VV" with your disk array's SCSI vendor,LUN name.

- GPT partition table allows you to create >2TB partitions

for i in /sys/block/sd* ; do wwn=`/lib/udev/scsi_id -g -s /block/${i##*/}` ; [ "$wwn" != "" ] && echo -e ${i##*/}'\t'$wwn ;done
2012-09-12 14:14:53
User: mhs
Functions: echo
1

Use `scsi_id` to positively identify which LUNs are which (i.e. compare with the list of LUNs you created on your disk array)

(shown: RHEL5 usage)

Debian usage:

# for i in /dev/sd* ; do wwn=`/lib/udev/scsi_id -g --device $i` ; [ "$wwn" != "" ] && echo -e ${i##*/}'\t'$wwn ;done
blkid -c /dev/null
2012-09-12 13:34:41
User: mhs
2

`blkid` is an interface to libuuid - it can read Device Mapper, EVMS, LVM, MD, and regular block devices.

-c /dev/null - Do not use cached output from /etc/blkid.tab or /etc/blkid/blkid.tab (RHEL)

-i - Display I/O Limits (aka I/O topology) information (not available in RHEL)

-p - Low-level superblock probing mode (not available in RHEL)

lsof -n -P|grep FlashXX|awk '{ print "/proc/" $2 "/fd/" substr($4, 1, length($4)-1) }'|while read f;do newname=$(exiftool -FileModifyDate -FileType -t -d %Y%m%d%H%M%S $f|cut -f2|tr '\n' '.'|sed 's/\.$//');echo "$f -> $newname";cp $f ~/Vids/$newname;done
2012-02-25 01:49:45
User: mhs
Functions: awk cp cut echo grep read sed tr
8

Certain Flash video players (e.g. Youtube) write their video streams to disk in /tmp/ , but the files are unlinked. i.e. the player creates the file and then immediately deletes the filename (unlinking files in this way makes it hard to find them, and/or ensures their cleanup if the browser or plugin should crash etc.) But as long as the flash plugin's process runs, a file descriptor remains in its /proc/ hierarchy, from which we (and the player) still have access to the file. The method above worked nicely for me when I had 50 tabs open with Youtube videos and didn't want to have to re-download them all with some tool.

exec -a "/sbin/getty 38400 tty7" your_cmd -erase_all_files
2012-02-01 10:54:03
User: mhs
Functions: exec
16
exec -a $NAME $COMMAND $ARGS

`your_cmd -erase_all_files` is the real process, but harmless-looking getty appears in the process table.

Never actually had a need to do this, but interesting nonetheless... Tested in bash, dash.

-a $NAME

"pass NAME as the zeroth argument to COMMAND", i.e. customise the name of the process (as commonly seen with `ps`)

lshw -short
echo "${0%/*}"
2011-04-17 12:09:56
User: mhs
Functions: echo
10

Invoked from within a shell script, this will print the directory in which the script resides. Doesn't depend on external tools, /proc/self/*, etc.. (`echo` is a shell builtin.) To see the *current working* directory of a script, use `pwd`.

mailutil appenddelete '{src.mailsrv1.com:993/imap/norsh/notls/ssl/novalidate-cert/user="username"}INBOX' '{dest.mailsrv2.com:143/imap/norsh/notls/user="username"}INBOX'
2011-02-08 09:37:05
User: mhs
Tags: mailutil IMAP
2

This one-liner was useful in helping someone I know to get off of MS Exchange. `mailutil` proved to be a much better alternative than `fetchmail` or `getmail` in this case.

It quickly moved all mails to the destination server (a simple Dovecot/Maildir setup), with no need to convert back and forth between mbox/maildir on the user's own system.

for i in ~/.adobe ~/.macromedia ; do ( rm $i/ -rf ; ln -s /dev/null $i ) ; done
2010-12-29 13:23:48
User: mhs
Functions: ln rm
18

Brute force way to block all LSO cookies on a Linux system with the non-free Flash browser plugin. Works just fine for my needs. Enjoy.

xfreerdp --plugin rdpsnd -g 1280x720 -a 24 -z -x m -u $username -p $password 10.20.30.40
2010-12-29 11:21:18
User: mhs
Tags: rdesktop RDP
2

This example uses xfreerdp, which builds upon the development of rdesktop. This example usage will also send you the remote machine's sound.

perl -wlne'/title>([^<]+)/i&&rename$ARGV,"$1.html"' *.html
2010-12-29 05:39:41
User: mhs
Functions: perl
Tags: rename
9

The above one-liner could be run against all HTML files in a directory. It renames the HTML files based on the text contained in their title tag. This helped me in a situation where I had a directory containing thousands of HTML documents with meaningless filenames.

infocmp rxvt-unicode | ssh 10.20.30.40 "mkdir -p .terminfo && cat >/tmp/ti && tic /tmp/ti"
2010-12-29 00:44:49
User: mhs
Functions: ssh
Tags: terminfo
3

I frequently use this trick to send my terminal settings to HPUX and older RHEL systems. This is due to the fact that terminfo support for rxvt-unicode (my preferred terminal app) does not exist on many older Linux and Unices.

file -s /dev/sd*
2010-12-28 09:56:03
User: mhs
Functions: file
Tags: file devfs
24

file(1) can print details about certain devices in the /dev/ directory (block devices in this example). This helped me to know at a glance the location and revision of my bootloader, UUIDs, filesystem status, which partitions were primaries / logicals, etc.. without running several commands.

See also:

file -s /dev/dm-* file -s /dev/cciss/*

etc..