Commands by argv (21)


  • 1
    case `uname` in FreeBSD)a=$#; case $a in 2) case $1 in 0) jot $(($2+1)) 0 $2 ;; *) jot $2 $1 $2 ;;esac;esac;esac; # usage: seq 1 4; seq 0 4
    argv · 2012-07-18 22:07:39 4
  • 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.


    0
    dng(){ local a;a=$(sed '/'"$1"'/!d' /etc/hosts |sed '=;'"${2-1,$}"'!d'|sed '/ /!d');echo $a|tr '\040' '\n'|nl -bp'[0-9]$'|less -E;export dn=$(echo $a|sed 's,.* ,,');export ip=$(echo $a|sed 's, .*,,');echo \$dn=$dn;echo \$ip=$ip;}
    argv · 2012-04-01 23:57:09 3
  • 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? Show Sample Output


    0
    _p(){ ps ax |grep $1 |sed '/grep.'"$1"'/d' |while read a;do printf ${a%% *}' ';printf "${a#* }" >&2;printf '\n';done;}
    argv · 2012-04-01 19:46:19 5
  • 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? Show Sample Output


    0
    _p(){ ps ax |grep $1 |sed '/grep.'"$1"'/d' |while read a;do printf ${a%% *}' ';printf "${a#* }" >&2;printf '\n';done;}
    argv · 2012-04-01 19:45:17 3
  • 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.


    0
    lso(){ jot -w '%04d' 7778 0000 7777 |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 };};}
    argv · 2012-01-08 05:48:24 4
  • 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


    -4
    case $# in 0) echo usage: $0 pattern ;; *)case $1 in */*)sed ' s,'"$1"',\ ,g';; *) sed ' s/'"$1"'/\ /g' ;;esac;esac;
    argv · 2011-12-30 23:54:12 3
  • for small output only example usage: jobs -l |col1 72


    -3
    col1(){ case $# in 0)echo col1 col-length;;*) sed 's/\(.\{'"$1"'\}\)\(.*\)/\1/' esac;}
    argv · 2011-12-30 23:35:29 3

  • -3
    tmpfs(){ cd /;for i in $@;do tar czvf /tmp/$i $i;mount -t tmpfs tmpfs /$i;tar xvzf /tmp/$i;cd ~ ;}# usage: tmpfs etc var
    argv · 2011-12-30 23:27:56 3
  • _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; }


    -5
    _ff(){ cd /mnt;echo /mnt/*/* |sed 's/ \/mnt\//\&/g' |sed '/'"$1"'/!d'; cd -;}
    argv · 2011-12-30 23:25:31 5
  • does the -i option open a tmp file? this method does not.


    3
    sedi(){ case $# in [01]|[3-9])echo usage: sedi sed-cmds file ;;2)sed -a ''"$1"';H;$!d;g;' $2 |sed -a '/^$/d;w '"$2"'' ;;esac;}
    argv · 2011-07-27 02:36:53 3
  • 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)


    5
    mem(){ { case $1 in st*|[vgid]*) printf "%s " "$@";; *) dd if=$3 2>&1|sed '$!d;/^0/d;s/ .*//;s/^/'"$1"' '"$2"' 1 0 /; r '"$3"'' 2>/dev/null;;esac;printf "\r\nquit\r\n";}|nc -n 127.0.0.1 11211; }
    argv · 2011-06-17 06:39:07 6
  • 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.


    1
    se(){ while read a;do [ "$a" != "${a#*$@*}" ]&&echo $a;done ;} # usage: se pattern # use in place of sed /pat/!d where RE are overkill
    argv · 2011-04-06 03:37:40 5
  • 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.


    14
    cls(){ printf "\33[2J";} or, if no printf, cat >cls;<ctrl-v><ctrl+[>[2J<enter><ctrl+d> cls(){ cat cls;}
    argv · 2011-04-06 01:51:45 17
  • 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.


    1
    tcp(){ tcpdump -nUs0 -w- -iinterface $1|tcpdump -n${2-A}r- ;} usage: tcp '[primitives]' [X|XX]
    argv · 2011-03-07 03:40:11 28
  • some other options: &delay=1000 &mode=links much more with piggybank as scraper works well with your favourite curses or non-curses http clients


    -1
    svn co http://simile.mit.edu/repository/crowbar/trunk&& cd ./trunk/xulapp/ xulrunner --install-app && Xvfb :1 && DISPLAY=:1 xulrunner application.ini 2>/dev/null 1>/dev/null && wget -O- "127.0.0.1:10000/&url=http://www.facebook.com"
    argv · 2010-10-16 05:12:11 3
  • 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.


    5
    liveh(){ tcpdump -lnAs512 ${1-} tcp |sed ' s/.*GET /GET /;s/.*Host: /Host: /;s/.*POST /POST /;/[GPH][EOo][TSs]/!d;w '"${2-liveh.txt}"' ' >/dev/null ;} # usage: liveh [-i interface] [output-file] && firefox &
    argv · 2010-10-11 01:01:11 3
  • usage examples ls largedir |rd lynx -dump largewebsite.com |rd rd < largelogfile


    2
    rd(){ while read a ;do printf "$a\n";sleep ${1-1};done ;} # usage: rd < file ; or ... | rd
    argv · 2010-10-03 04:16:03 3
  • if you use disk-based swap then it can defeat the purpose of this function.


    4
    ram() { for i in /tmp /altroot;do mount -t tmpfs tmpfs $i;done&& for i in /var /root /etc $HOME; do find -d $i |cpio -pdmv /tmp&& mount -t tmpfs tmpfs $i&& mv -v /tmp$i/* $i&& rm -vrf /tmp$i ; done ;} usage: (in rc sequence) ram
    argv · 2010-08-31 08:25:55 4
  • 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)


    2
    jb() { if [ -z $1 ];then printf 'usage:\njb <"date and/or time"> <"commandline"> &\nsee parsedate(3) strftime(3)\n';else t1=$(date +%s); t2=$(date -d "$1" +%s) ;sleep $(expr $t2 - $t1);$2 ;fi ;}
    argv · 2010-08-26 23:50:42 3

  • 5
    st() { LDFLAGS=-static CFLAGS=-static CXXFLAGS=-static NOSHARED=yes ./configure $@ ;} usage: st [configure operands]
    argv · 2010-08-20 08:18:56 3

  • 7
    cls(){ printf "\033c";} or, if no printf, cat > c ;<ctrl+v> <ctrl+[>c <enter><ctrl-d> c(){ cat c;} #usage: c
    argv · 2010-08-02 07:27:22 18

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. 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.

Share Your Commands


Check These Out

force unsupported i386 commands to work on amd64
The above was done using the i386 flashplayer plugin, and was installed on a AMD64 machine running an AMD64 kernel and AMD64 programs. the resulting plugin install ultimately didn't work for swiftfox (but worked for iceweasel) without also covering it with a nspluginwrapper which took a bit of fenangaling to get to work (lots of apt-getting) but it is a nice feature to be able to trick installers that think you need i386 into running on a amd64, or at least attempting to run on amd64. Enjoy

Gets the english pronunciation of a phrase
Usage examples: say hello say "hello world" say hello+world

Generat a Random MAC address
Generate a random MAC address with capital letters

Extract tarball from internet without local saving

Check whether laptop is running on battery or cable
The original proc file doesn't exist on my system.

Install pip with Proxy
Installs pip packages defining a proxy

Track X Window events in chosen window
After executing this, click on a window you want to track X Window events in. Explaination: "xev will track events in the window with the following -id, which we get by greping window information obtained by xwininfo"

dont execute command just add it to history as a comment, handy if your command is not "complete" yet

list files recursively by size

Get the IP address
gives u each configured IP in a seperate line.


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: