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/
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.
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
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:
Ever need to get some text that is a specific number of characters long? Use this function to easily generate it! Doesn't look pretty, but sure does work for testing purposes!
Test'd & work'd fine on: HP-UX, SUSE, with bash
# "Work'd" 's are wellcome!
APu
xmllint2 comes with GNU libxml2 library.
(based on: http://vim.wikia.com/wiki/Format_your_xml_document_using_xmllint)
this requires the use of a throwaway file.
it outputs a shell function.
assuming the throwaway file is f.tmp
usage: >f.tmp;lso f.tmp > f.tmp; . f.tmp;rm f.tmp;lso -l ...
notes:
credit epons.org for the idea. however his version did not account for the sticky bit and other special cases.
many of the 4096 permutations of file permissions make no practical sense. but chmod will still create them.
one can achieve the same sort of octal output with stat(1), if that utility is available.
here's another version to account for systems with seq(1) instead of jot(1):
lso(){
case $# in
1)
{ case $(uname) in
FreeBSD)
jot -w '%04d' 7778 0000 7777 ;;
*)
seq -w 0000 7777 ;;
esac; } \
|sed '
/[89]/d
s,.*,printf '"'"'& '"'"';chmod & '"$1"';ls -l '"$1"'|sed s/-/./,' \
|sh \
|{
echo "lso(){";
echo "ls \$@ \\";
echo " |sed '";
sed '
s, ,@,2;
s,@.*,,;
s,\(.* \)\(.*\),s/\2/\1/,;
s, ,,';
echo \';
echo };
};
;;
*)
echo "usage: lso tmp-file";
;;
esac;
}
this won't print out types[1]. but its purpose is not to examine types. its focus is on mode and its purpose is to make mode easier to read (assuming one finds octal easier to read).
1. one could of course argue "everything is a file", but not always a "regular" one. e.g., a "directory" is really just a file comprising a list.
You can use ordinary printf to convert "%23%21%2fbin%2fbash" into "#!/bin/bash" with no external utilities, by using a little known printf feature -- the "%b" specifier converts shell escapes. Replace % with \x and printf will understand the urlencoded string.
BASH's printf has an extension to set a variable directly, too. So you get to convert urlencoded strings from garble to plaintext in one step with no externals and no backticks.
full command:
for fn in xkcd*.png xkcd*.jpg; do; echo $fn; read xw xh <<<$(identify -format '%w %h' $fn); nn="$(echo $fn | sed 's/xkcd-\([0-9]\+\)-.*/\1/')"; wget -q -O xkcd-${nn}.json http://xkcd.com/$nn/info.0.json; tt="$(sed 's/.*"title": "\([^"]*\)", .*/\1/' xkcd-${nn}.json)"; at="$(sed 's/.*alt": "\(.*\)", .*/\1/' xkcd-${nn}.json)"; convert -background white -fill black -font /usr/share/fonts/truetype/freefont/FreeSansBold.ttf -pointsize 26 -size ${xw}x -gravity Center caption:"$tt" tt.png; convert -background '#FFF9BD' -border 1x1 -bordercolor black -fill black -font /usr/share/fonts/truetype/freefont/FreeSans.ttf -pointsize 16 -size $(($xw - 2))x -gravity Center caption:"$at" at.png; th=$(identify -format '%h' tt.png); ah=$(identify -format '%h' at.png); convert -size ${xw}x$(($xh+$th+$ah+5)) "xc:white" tt.png -geometry +0+0 -composite $fn -geometry +0+$th -composite at.png -geometry +0+$(($th+$xh+5)) -composite ${fn%\.*}_cmp.png; echo -e "$fn $nn $xw $xh $th $ah \n$tt \n$at\n"; done
this assumes that all comics are saved as xkcd-[number]-[title].{png|jpg}.
it will then download the title and alt-text, create pictures from them, and put everything together in a new png-file.
it's not perfect, but it worked for nearly all my comics.
it uses the xkcd-json-interface.
though it's poorly written, it doesn't completely break on http://xkcd.com/859/
Scan a file and print out a list of ASCII characters that are not used in the file which can then be safely used to delimit fields. Useful when needing to convert CSV files using "," to a single character delimiter. Piping it into less at the end (which could be redundant) stops the command characters being interpreted by the terminal.
To quickly add some remark, comment, stamp text, ... on top of (each of) the pages of the input pdf file.
This is especially useful to get crazy stuff like space characters copied to your pasteboard correctly.
Source: https://github.com/mathiasbynens/dotfiles/blob/master/.functions
Check your IP address using www.whatismyip.org from the command line
alternative to tr char '\012'
works with sed's that don't accept "\n"
allows for multi-char sentinals, while tr(1) only operates on single chars
_ff(){
cd /mnt;
echo /mnt/*/* |sed '
s/ \/mnt\//\&/g;
'|sed '/'"$1"'/!d';
cd -;
}
ff(){
case $# in
0)
echo "usage: ff glob [sed-cmds] [--|var-name]"
;;
1)
_ff $1 |sed =
;;
[2-9])
case $2 in
--) _ff $1 |less -SN
;;
*) _ff $1 |sed -n ''"$2"''|tr '\n' '\040' |sed 's/.*/export '"$3"'=\"&/;s/=\" /=\"/;s/ $/\"/' > $HOME/.ff;
case $# in
3)
. $HOME/.ff
;;
esac;
sed '
s/export .*=\"/\$'"$3"' = \"/;' $HOME/.ff;\
;;
esac
;;
esac;
}
v(){
local a=$HOME;
sed '
s/export /less -n \$/;
s/=.*//;
' $a/.ff > $a/.v ;
. $a/.v ;
}
Another approach using ls(1)
lsl(){
_lsl ()
{
ls -l $3 /mnt/*/$1* 2>/dev/null;
};
case $# in
0)
echo "usage: lsl pat [ls-options|result-no]";
echo "usage: lsle pat [sed-cmds]"
;;
1)
_lsl $1 |sed =
;;
2)
case $2 in
-*) _lsl $1 $@;;
*)
_lsl $1 |sed 's/.* //;
'"$2"'!d;
'"$2"'q' > $HOME/.lsl ;
export v=$(sed 1q $HOME/.lsl);
echo \$v = $v
;;
esac
;;
esac;
}
exp(){
echo "%s/\$/ /";
echo "%j";
echo "s/^/export v=\"";
echo "s/\$/\"";
echo "s/ \"\$/\"";
echo ".";
echo "wq";
}
lsle(){
lsl $1 -1 |sed $2 > .lsl&&
exp |ed -s .lsl >&-&&
. .lsl&&
echo \$v = $v;
}
It's useful mostly for your custom scripts, which running on specific host and tired on ssh'ing every time when you need one simple command (i use it for update remote apt repository, when new package have to be downloaded from another host).
Don't forget to set up authorization by keys, for maximum comfort.
Found on https://bitcointalk.org/index.php?topic=55520.0
Example: you have a package.txt you want to install on a system. Instead of this:
cat package.txt
package1
package2
package3
You want it to cat out on one line so you can print "yum install package1 package2 package3"