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 using less from sorted by
Terminal - Commands using less - 45 results
man foobar | less +/searched_string
2013-11-06 18:50:11
User: mcescalante
Functions: less man
1

This will open the manpage for "foobar", and display all instances of "searched_string". You can traverse through them by pressing "n"

less -XF
less -p pattern file.txt
pygmentize -l pytb myapp.log | less -SR
grep --color=always -nr 'setLevel' --include=*py | less -SRqg
git diff -U10|dwdiff --diff-input -c|less -R
git diff -U10 |wdiff --diff-input -a -n -w $'\e[1;91m' -x $'\e[0m' -y $'\e[1;94m' -z $'\e[0m' |less -R
2012-08-15 01:42:16
Functions: diff less
2

I've been using colordiff for years. wdiff is the new fav, except its colors. Word delimited diffs are more interleaved, easing the chore of associating big blocks of changes.

show_code() { pygmentize $1 | less -N }
2012-05-12 09:18:33
User: Flow
Functions: less
0

Uses pygments ( http://pygments.org/ ) to highlight the source code and a less pipe to scroll and show line numbers of the code.

compgen -c | sort -u > commands && less commands
less `kpsewhich scrartcl.cls`
2012-04-15 11:10:41
User: Stefan
Functions: less
Tags: latex tex
0

kpsewhich is a tool for path and file lookup. It is a front-end of the kpathsea library. For one or more given package or file names it returns the complete path from within the TeX installation, that one which the compiler would actually use. Via backticks we can use it as argument to less, more, or any editor. For example:

gedit `kpsewhich hyperref.sty`
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;}
2012-04-01 23:57:09
User: argv
Functions: echo export less nl sed tr
0

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.

pygmentize -l sh ~/.bashrc | less -R
2012-01-27 03:27:37
User: captaincomic
Functions: less sh
0

On Debian/Ubuntu the pygments script is called pygmentize and can be found in the python-pygments package.

For an overview of all available lexers, formatters, styles and filters use

pygmentize -L

Here is an example using more options

pygmentize -f 256 -l sh -F whitespace:spaces=True,tabs=True -O style=borland ~/.bashrc | less -R
cmdfu(){ curl "http://www.commandlinefu.com/commands/matching/$@/$(echo -n $@ | openssl base64)/plaintext" --silent | sed "s/\(^#.*\)/\x1b[32m\1\x1b[0m/g" | less -R }
less file.ext
2011-09-13 10:29:27
User: totti
Functions: less
Tags: less text search
-4

Use the following key binding to search

----------------------------------------------------------------

ng

: Jump to line number n. Default is the start of the file.

nG

: Jump to line number n. Default is the end of the file.

/pattern

: Search for pattern. Regular expressions can be used. [/ = slash] Press / and then Enter to repeat the previous search pattern. Press ESC and then u to undo search highlighting.

n

: Go to next match (after a successful search).

N

: Go to previous match.

mletter

: Mark the current position with letter.

'letter

: Return to position letter. [' = single quote]

'^ or g

: Go to start of file.

'$ or G

: Go to end of file.

s

: Save current content (got from another program like grep) in a file.

= or Ctrl+g

: File information.

F

: continually read information from file and follow its end. Useful for logs watching. Use Ctrl+c to exit this mode.

-option

: Toggle command-line option -option.

h

: Help.

less file.lst | head -n 50000 > output.txt
2011-09-05 05:26:04
User: Richie086
Functions: head less
-3

Useful for situations where you have word lists or dictionaries that range from hundreds of megabytes to several gigabytes in size. Replace file.lst with your wordlist, replace 50000 with however many lines you want the resulting list to be in total. The result will be redirected to output.txt in the current working directory. It may be helpful to run wc -l file.lst to find out how many lines the word list is first, then divide that in half to figure out what value to put for the head -n part of the command.

cat file.csv | perl -pe 'if($. == 1) {@h = split(/;/); $i = 1 ; map { $_ = $i; $i++ } @h; print join(" ;", @h) , "\n"} ; s/(^|;);/$1 ;/g' | column -ts\; | less -S
mount |tail -1 | less -p "/dev/[^ ]*"
manswitch () { man $1 | less -p "^ +$2"; }
2011-08-19 16:44:48
User: dbh
Functions: less man
Tags: grep switch
14

e.g.

manswitch grep -o

This will take you to the relevant part of the man page, so you can see the description of the switch underneath.

dwdiff -c a.tex b.tex | less -R
function ds { echo -n "search : "; read ST; EST=`php -r "echo rawurlencode('$ST');"`; B64=`echo -n $ST| openssl enc -base64`; curl -s "http://www.commandlinefu.com/commands/matching/$EST/$B64/plaintext" | less -p "$ST"; } ; bind '"\C-k"':"\"ds\C-m\""
2011-02-20 23:46:16
User: cparker
Functions: echo less read
Tags: bash less curl PHP
0

This is a simple bash function and a key binding that uses commandlinefu's simple and easy search API. It prompts for a search term, then it uses curl to search commandline fu, and highlights the search results with less.

Command in description (Your command is too long - please keep it to less than 255 characters)
2011-02-03 08:25:42
User: __
Functions: command less
0
yt2mp3(){ for j in `seq 1 301`;do i=`curl -s gdata.youtube.com/feeds/api/users/$1/uploads\?start-index=$j\&max-results=1|grep -o "watch[^&]*"`;ffmpeg -i `wget youtube.com/$i -qO-|grep -o 'url_map"[^,]*'|sed -n '1{s_.*|__;s_\\\__g;p}'` -vn -ab 128k "`youtube-dl -e ${i#*=}`.mp3";done;}

squeezed the monster (and nifty ☺) command from 7776 from 531 characters to 284 characters, but I don't see a way to get it down to 255. This is definitely a kludge!

Command in description (Your command is too long - please keep it to less than 255 characters)
2011-02-01 17:36:32
User: m1cawber
Functions: command less
0
yt-mp3chanrip() { for count in 1 51 101 151 201 251 301; do for i in $(curl -s http://gdata.youtube.com/feeds/api/users/"$1"/uploads\?start-index="$count"\&max-results=50 | grep -Eo "watch\?v=[^[:space:]\"\'\\]{11}" | uniq); do ffmpeg -i $(wget http://youtube.com/"$i" -qO- | sed -n "/fmt_url_map/{s/[\'\"\|]/\n/g;p}" | sed -n '/^fmt_url_map/,/videoplayback/p' | sed -e :a -e '$q;N;5,$D;ba' | tr -d '\n' | sed -e 's/\(.*\),\(.\)\{1,3\}/\1/') -vn -ab 128k "$(youtube-dl -e http://youtube.com/"$i").mp3"; done; done; unset count i; }

create the function and run with

yt-mp3chanrip YoutubeUsername

Great for channels like ukfDrumAndBass that only post music. No more need for third party browser plugins or websites that only convert one vid one at a time. It'll convert and save to CWD up to 300 of a user's videos to mp3s, one at a time. To increase, just increment the $count pattern. This is a concoction from commands #7718 and #7752, so it uses ffmpeg wget, curl, sed, and youtube-dl -- youtube-dl is only used to get the title of the video which it uses to name the mp3 file. You can use a different naming method if you want and the function should still work.

In MySQL client, \P less -S
2011-01-19 09:44:43
User: maxmanders
Functions: less
-1

Instead of dumping the entire query to the screen, it will use 'less -S' to handle pagination. This means you can move back and forward through the result set, and instead of wrapping rows at screen boundaries, the table will extend off the screen, and can be scrolled left and right with arrow keys.

zcat file.gz | less +/search_pattern
2011-01-14 21:29:40
User: gwchamb
Functions: less zcat
1

This command enables the user to append a search pattern on the command line when using less as the PAGER. This is especially convenient (as the example shows) in compressed files and when searching man pages (substituting the zcat command with man, however).

hexdump -e '8/1 "%02X ""\t"" "' -e '8/1 "%c""\n"' /dev/sda1 | less /mystring