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:
usage: dng BRE [selection]
default selection is the last match
DNS is ok, but although domainnames may be easier to remember than IP numbers, it still requires typing them out. This can be error-prone. Even more so than typing IPv4 numbers, depending on the domainname, its length and complexity.
proc lister
usage: p
proc killer
usage: p patt [signal]
uses only ps, grep, sed, printf and kill
no need for pgrep/pkill (not part of early UNIX)
_p(){
ps ax \
|grep $1 \
|sed '
/grep.'"$1"'/d' \
|while read a;do
printf ${a%% *}' ';
printf "${a#* }" >&2;
printf '\n';
done;
}
p(){
case $# in
0)
ps ax |grep .|less -iE;
;;
1)
_p $1;
;;
[23])
_p $1 2>/dev/null \
|sed '/'"$2"'/!d;
s,.*,kill -'"${3-15}"' &,'|sh -v
;;
esac;
}
alas, can't get this under 255 chars.
flatcap?
proc lister
usage: p
proc killer
usage: p patt [signal]
uses only ps, grep, sed, printf and kill
no need for pgrep/pkill (not part of early UNIX)
_p(){
ps ax \
|grep $1 \
|sed '
/grep.'"$1"'/d' \
|while read a;do
printf ${a%% *}' ';
printf "${a#* }" >&2;
printf '\n';
done;
}
p(){
case $# in
0)
ps ax |grep .|less -iE;
;;
1)
_p $1;
;;
[23])
_p $1 2>/dev/null \
|sed '/'"$2"'/!d;
s,.*,kill -'"${3-15}"' &,'|sh -v
;;
esac;
}
alas, can't get this under 255 chars.
flatcap?
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.
allows for multi-char sentinals, while tr(1) only operates on single chars
like tr(1), all versions of awk accept octals
example usage:
tr -d '\n' < file-with-unstructured-text-with-repeating-multichar-sentinal|tr1 multichar-sentinal | sed ...
_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;
}
does the -i option open a tmp file?
this method does not.
usage: mem memcache-command [arguments]
where memcache-command might be:
set
add
get[s]
append
prepend
replace
delete
incr
decr
cas
stats
verbosity
version
notes:
exptime argument is set to 0 (no expire)
flags argument is set to 1 (arbitrary)
POSIX requires this "string truncating" functionality.
might as well use it, at least for very small tasks where invoking sed and using RE is overkill.
this leaves the cursor at the bottom of the terminal screen, where your eyes are.
ctrl-l moves it to the top, forcing you to look up.
anticipated question/argument: how is it faster than an external command such as tput?
answer: if printf is a shell builtin and you haven't already run the external program, placing it in memory, then...
executing a program from memory (printf) is faster than executing a program from disk (tput). the second time you run tput, it's probably running from memory. but the first time, it's running from the media where your os is stored. and accessing that media is slower than accessing memory.
and finally, as far as i know you do not need braces for a single command shell function. POSIX allows this. if this one-liner doesn't work in your shell because of no braces, then let's hear what shell you're using.
Sometimes the question comes up: How to get unbuffered tcpdump output into the next program in the pipe? i.e. if your OS forces you to wait for the buffer to fill before the next program sees any of the output
If you use -Uw- then you can't use -A (or -X or -XX) at the same time.
When the question comes up, I've never seen anyone suggest this simple solution: chaining 2 tcpdump instances.
some other options:
&delay=1000
&mode=links
much more with piggybank as scraper
works well with your favourite curses or non-curses http clients
Default output-file is "liveh.txt".
This uses only BRE, in case you're using an older version of sed(1) that doesn't have support for ERE added.
With a modern sed(1), to reduce false positive matches, you might do something like:
liveh(){ tcpdump -lnnAs512 -i ${1-} tcp |sed 's/.*GET /GET /;s/.*Host: /Host: /;s/.*POST /POST /;/GET |Host: |POST /!d;/[\"'"'"]/d;/\.\./d;w '"${2-liveh.txt}"'' >/dev/null ;}
Anyway, it's easy to clean up the output file with sed(1) later.
usage examples
ls largedir |rd
lynx -dump largewebsite.com |rd
rd < largelogfile
if you use disk-based swap then it can defeat the purpose of this function.
EXAMPLES
jb "next sun 12pm" "/bin/sh ~you/1.sh" &
jb "2010-08-29 12:00:00" "~you/1.sh" &
jb "29aug2010 gmt" ". ~you/1.sh" &
jb 12:00p.m. "nohup ./1.sh" &
jb 1min "echo stop!" &
SEE ALSO
parsedate(3) strftime(3)
Alas, typing "reset" and pressing Enter does not always work, in my experience.