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 keymon from sorted by
Terminal - Commands by keymon - 17 results
xclip -selection clipboard -o -t text/html | pandoc -f html -t markdown_github -
2014-08-18 20:47:46
User: keymon
2

I always wanted to be able to copy formatted HTML, like from emails, on trello cards or READMEs... but the formatting is always wrong... But from this two links:

* https://jeremywsherman.com/blog/2012/02/08/pasting-html-into-markdown/

* http://stackoverflow.com/questions/3261379/getting-html-source-or-rich-text-from-the-x-clipboard

For instance, to to copy an formatted email to a trello card, just:

1. Select the email body

2. run: xclip -selection clipboard -o -t text/html | pandoc -f html -t markdown_github - | xclip -i -t text/plain

3. Paste in your trello card

4. Profit!

8-)

docker ps -q | xargs -n 1 docker inspect | jq '.[0].NetworkSettings.Ports +{} | map(select(. != null)[0].HostPort) | map("-L \(.):localhost:\(.)") ' | sed -n 's/.*"\(.*\)".*/\1/p' |xargs boot2docker ssh -N
perl -MFile::Find=find -MFile::Spec::Functions -Tlwe '$found=1; find { wanted => sub { if (/$ARGV[0]\.pm\z/) { print canonpath $_; $found=0; } }, no_chdir => 1 }, @INC; exit $found;' Collectd/Plugins/Graphite
2013-01-11 11:01:46
User: keymon
Functions: exit find perl
-2

Will check if the given module is installed in the @INC. It will print the path and return 0 if found, or 1 otherwise.

Based on script from SharpyWarpy in http://www.linuxquestions.org/questions/linux-general-1/how-to-list-all-installed-perl-modules-216603/

isdef() { eval test -n \"\${$1+1}\"; }
which any_path/a_command.sh | sed "s|^./|$(pwd)|"
2011-09-22 10:38:56
User: keymon
Functions: sed which
-8

This works in multiple unixes, not only linux, for different paths.

On solaris, if you do not have which, you can use:

ksh whence -p anypath/a_command.sh | sed "s|^./|$(pwd)|" ksh whence -p

who -m | sed 's/.*(\(.*\)).*/\1/'
2011-09-22 10:28:30
User: keymon
Functions: sed who
-2

Will print the host associated with the current stdin.

This is useful to set the DOIT_SERVER for the doit remote execution agent ( http://www.chiark.greenend.org.uk/~sgtatham/doit/ )

export DOIT_HOST=$(who -m | sed 's/.*(\(.*\)).*/\1/')

Note that SSH_CLIENT variable can be lost if you use su or sudo (if set to reset vars)

get_absolute_path() { echo $1 | sed "s|^\([^/].*/.*\)|$(pwd)/\1|;s|^\([^/]*\)$|$(which -- $1)|;s|^$|$1|"; }
2011-09-13 11:06:55
User: keymon
Functions: echo sed
-4

It will return the absolute location of the called a script. If is in $PATH, it will search it using which.

You can combine this function with this other one: http://www.commandlinefu.com/commands/view/9252/readlink-equivalent-using-shell-commands-and-following-all-links, to get a way to know where is the real location of a called script:

# Returns the realpath of a called command. whereis_realpath() { local SCRIPT_PATH=$(whereis $1); myreadlink ${SCRIPT_PATH} | sed "s|^\([^/].*\)\$|$(dirname ${SCRIPT_PATH})/\1|"; }
myreadlink() { [ ! -h "$1" ] && echo "$1" || (local link="$(expr "$(command ls -ld -- "$1")" : '.*-> \(.*\)$')"; cd $(dirname $1); myreadlink "$link"; }
2011-09-13 11:02:27
User: keymon
Functions: cd command dirname echo ls
0

This is a equivalent to the GNU ' readlink' tool, but it supports following all the links, even in different directories.

An interesting alternative is this one, that gets the path of the destination file

myreadlink() { [ ! -h "$1" ] && echo "$1" || (local link="$(expr "$(command ls -ld -- "$1")" : '.*-> \(.*\)$')"; cd $(dirname $1); myreadlink "$link" | sed "s|^\([^/].*\)\$|$(dirname $1)/\1|"); }
while IFS= read -r -u3 -d $'\0' file; do file "$file" | egrep -q 'executable|ELF' && chmod +x "$file"; done 3< <(find . -type f -print0)
2011-08-18 15:37:23
User: keymon
Functions: chmod egrep file find read
0

If you make a mess (like I did) and you removed all the executable permissions of a directory (or you set executable permissions to everything) this can help.

It supports spaces and other special characters in the file paths, but it will work only in bash, GNU find and GNU egrep.

You can complement it with these two commands:

1. add executable permission to directories:

find . type d -print0 | xargs -0 chmod +x

2. and remove to files:

find . type d -print0 | xargs -0 chmod -x

Or, in the same loop:

while IFS= read -r -u3 -d $'\0' file; do case $(file "$file" | cut -f 2- -d :) in :*executable*|*ELF*|*directory*) chmod +x "$file" ;; *) chmod -x "$file" ;; esac || break done 3< <(find . -print0)

Ideas stolen from Greg's wiki: http://mywiki.wooledge.org/BashFAQ/020

for i in *; do file $i | grep -q ELF || continue; readelf -d $i | grep -q RPATH || echo $i; done
2011-08-16 17:37:23
User: keymon
Functions: echo file grep readelf
0

Using gentoo prefix portage I got in a situation where some packages did not contain the needed RPATH variable. This command helped me to find out which ones I should recompile

ipcs -ma | awk '/^m / { if ($9 == 0) { print $2 }}' | xargs -n 1 ipcrm -m
2011-07-26 15:53:59
User: keymon
Functions: awk ipcrm ipcs xargs
0

It can work for message queue, semaphore set or shared memory just changing the parameter.

user=an_user awk "/^$user:\$/,/password =/ { if (\$1 == \"password\") { print \$3; } }" < /etc/security/passwd
2010-11-29 09:46:12
User: keymon
Functions: awk
0

AIX lssec does not print the password attribute by policy

# lssec -c -f /etc/security/passwd -s an_user -a password

3004-697 Attribute "password" is not valid.

To get the password, you have to parse the /etc/security/passwd.

You can reuse this password using chpasswd:

echo "otheruser:D9oKC1v3VUt/I" | chpasswd -c -e -R compat

NFSPATH=/mountpoint TIMEOUT=5; perl -e "alarm $TIMEOUT; exec @ARGV" "test -d $NFSPATH" || (umount -fl $NFSPATH; mount $NFSPATH)
2010-06-04 07:59:00
User: keymon
Functions: mount perl umount
8

Based on the execute with timeout command in this site.

A more complex script:

#!/bin/sh

# This script will check the avaliability of a list of NFS mount point,

# forcing a remount of those that do not respond in 5 seconds.

#

# It basically does this:

# NFSPATH=/mountpoint TIMEOUT=5; perl -e "alarm $TIMEOUT; exec @ARGV" "test -d $NFSPATH" || (umount -fl $NFSPATH; mount $NFSPATH)

#

TIMEOUT=5

SCRIPT_NAME=$(basename $0)

for i in $@; do

echo "Checking $i..."

if ! perl -e "alarm $TIMEOUT; exec @ARGV" "test -d $i" > /dev/null 2>&1; then

echo "$SCRIPT_NAME: $i is failing with retcode $?."1>&2

echo "$SCRIPT_NAME: Submmiting umount -fl $i" 1>&2

umount -fl $i;

echo "$SCRIPT_NAME: Submmiting mount $i" 1>&2

mount $i;

fi

done

NFSPATH=/mountpoint TIMEOUT=5; perl -e "alarm $TIMEOUT; exec @ARGV" "test -d $NFSPATH" || (umount -fl $NFSPATH; mount $NFSPATH)
2010-06-04 07:58:53
User: keymon
Functions: mount perl umount
-1

Based on the execute with timeout command in this site.

A more complex script:

#!/bin/sh

# This script will check the avaliability of a list of NFS mount point,

# forcing a remount of those that do not respond in 5 seconds.

#

# It basically does this:

# NFSPATH=/mountpoint TIMEOUT=5; perl -e "alarm $TIMEOUT; exec @ARGV" "test -d $NFSPATH" || (umount -fl $NFSPATH; mount $NFSPATH)

#

TIMEOUT=5

SCRIPT_NAME=$(basename $0)

for i in $@; do

echo "Checking $i..."

if ! perl -e "alarm $TIMEOUT; exec @ARGV" "test -d $i" > /dev/null 2>&1; then

echo "$SCRIPT_NAME: $i is failing with retcode $?."1>&2

echo "$SCRIPT_NAME: Submmiting umount -fl $i" 1>&2

umount -fl $i;

echo "$SCRIPT_NAME: Submmiting mount $i" 1>&2

mount $i;

fi

done

$COMMAND 2>&1 >/dev/null & WPID=$!; sleep $TIMEOUT && kill $! & KPID=$!; wait $WPID
2010-05-26 11:12:26
User: keymon
Functions: kill sleep wait
3

I like much more the perl solution, but without using perl. It launches a backgroup process that will kill the command if it lasts too much.

A bigger function:

check_with_timeout() {

[ "$DEBUG" ] && set -x

COMMAND=$1

TIMEOUT=$2

RET=0

# Launch command in backgroup

[ ! "$DEBUG" ] && exec 6>&2 # Link file descriptor #6 with stderr.

[ ! "$DEBUG" ] && exec 2> /dev/null # Send stderr to null (avoid the Terminated messages)

$COMMAND 2>&1 >/dev/null &

COMMAND_PID=$!

[ "$DEBUG" ] && echo "Background command pid $COMMAND_PID, parent pid $$"

# Timer that will kill the command if timesout

sleep $TIMEOUT && ps -p $COMMAND_PID -o pid,ppid |grep $$ | awk '{print $1}' | xargs kill &

KILLER_PID=$!

[ "$DEBUG" ] && echo "Killer command pid $KILLER_PID, parent pid $$"

wait $COMMAND_PID

RET=$?

# Kill the killer timer

[ "$DEBUG" ] && ps -e -o pid,ppid |grep $KILLER_PID | awk '{print $1}' | xargs echo "Killing processes: "

ps -e -o pid,ppid |grep -v PID | grep $KILLER_PID | awk '{print $1}' | xargs kill

wait

sleep 1

[ ! "$DEBUG" ] && exec 2>&6 6>&- # Restore stderr and close file descriptor #6.

return $RET

}

odmget -q "attribute=unique_id" CuAt |sed -n 's/.*name = "\(.*\)"/\1/p;s/.*value = "..........\(....\)..SYMMETRIX..EMCfcp.*"/0x\1/p;s/.*value =//p'
for i in $(lsdev -Cc disk | grep EMC | cut -f 1 -d " " ); do if lsattr -a reserve_lock -El $i | grep -q "reserve_lock yes"; then chdev -a reserve_lock=no -l $i; fi; done
2010-05-14 11:48:39
User: keymon
Functions: cut grep lsattr
0

It will change the reserve_lock attribute to all AIX EMC disk attached.