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

All commands from sorted by
Terminal - All commands - 11,620 results
cat `ls -r /sys/class/net/*/address` | sort -u
script -t 2> timing.txt -a session.txt ; Run some commands here; exit; scriptreplay timing.txt session.txt
type -a python
f -f -e mplayer movie
2011-12-09 02:58:41
User: clvv
-2

This is one use case of `f`. You need to install `f`(https://github.com/clvv/f) for this to work. With f, you can access any file by only typing some of its name regardless of where you are. f is inspired by autojump and z.

ifconfig -a | awk '/Bcast/{print $2}' | cut -c 5-19
tar -pczf archive_name.tar.gz /path/to/dir/or/file
2009-07-17 19:53:02
User: ryuslash
Functions: tar
Tags: bash tar gzip
-2

Create a single tar.gz archive

I know it's a very basic one, but it's one I keep forgetting.

alias emacs='vim'
sqlite3 -line database.db
2010-10-09 16:10:19
User: pykler
Tags: CSV html sql sqlite
-2

Similar output to using MySQL with the \G at the end of a Query. Displays one column per line. Other modes include:

-column

Query results will be displayed in a table like form, using whitespace characters to separate the columns and align the output.

-html Query results will be output as simple HTML tables.

-line Query results will be displayed with one value per line, rows separated by a blank line. Designed to be easily parsed by scripts or other programs

-list Query results will be displayed with the separator (|, by default) character between each field value. The default.

From inside the command line this can be also changed using the mode command:

.mode MODE ?TABLE? Set output mode where MODE is one of:

csv Comma-separated values

column Left-aligned columns. (See .width)

html HTML code

insert SQL insert statements for TABLE

line One value per line

list Values delimited by .separator string

tabs Tab-separated values

tcl TCL list elements

for i in 192.168.1.{61..71};do ping -c 1 $i &> /dev/null && echo $i;fi;done
2009-08-26 06:04:24
User: AlecSchueler
Functions: echo ping
Tags: Network ssh bash
-2

If you need to ssh into a computer on the local network but you're unsure of the ip to use, then ping them and see if you get a response. If you do, print out the address you got it from. Adjust the range to suit your network.

file /System/Library/Extensions/*.kext/Contents/MacOS/* |grep -i x86_64 |nl | tail -1 | cut -f1 -f3; file /System/Library/Extensions/*.kext/Contents/MacOS/* |grep -i "mach-o object i386" |nl | tail -1 | cut -f1 -f3
2009-09-11 16:43:27
Functions: cut file grep nl tail
-2

Not as far off as you thought, now is it?

-mac fanboy

IP=`ifconfig eth0 | grep "inet addr:" | ips |cut -d ":" -f 2 | cut -d " " -f 1`;SUBNET=`ifconfig eth0 | grep "inet addr:" | ips |cut -d ":" -f 3 | cut -d " " -f 1`;RANGE=`ipcalc $IP/$SUBNET | grep "Network:" | cut -d ' ' -f 4`;echo $RANGE
ls -lah --color=always | most
2010-01-04 22:21:13
User: Code_Bleu
Functions: ls
-2

Even though --color is an option for 'ls' it will not display in color when doing 'ls -lah --color=always | less' to have color output when doing a directory listing and piping it out to page through results, replace less with most.

To install most if not installed, run:

sudo apt-get install most

find . -type f -exec sed -i 's/gw10./gw17./g' {} \;
cowsay -l | sed '1d;s/ /\n/g' | while read f; do cowsay -f $f $f;done
find . \( -name "*.as[pc]x" -o -name "*.htm*" \) -exec grep -Hi "jquery-1" {} +
zc () { for exp in $argv; do print "$exp = $(( exp ))"; done; }
find . -type f -name '*.gz'|awk '{print "zcat", $1, "| bzip2 -c >", $0.".tmp", "&& rename", "s/.gz.tmp/.bz2/", "*.gz.tmp", "&& rm", $0}'|bash
2013-04-11 10:17:57
User: Ztyx
Functions: awk find
-2

This solution is similar to [1] except that it does not have any dependency on GNU Parallel. Also, it tries to minimize the impact on the running system (using ionice and nice).

[1] http://www.commandlinefu.com/commands/view/7009/recompress-all-.gz-files-in-current-directory-using-bzip2-running-1-job-per-cpu-core-in-parallel

cat /service/solr/log/main/current | tai64nlocal | grep "\(`date '+%F %H'`\|`date '+%F %H %M' | awk '{print $1" 0"$2-1":"$3}'`\)" | grep QTime | awk '{print $NF}' | awk -F\= '{ s += $2} END {print s/NR}'
ifconfig | grep -B 1 "inet addr:" | head -1 | cut -d" " -f1
2010-01-04 23:02:57
User: jasonwalsh
Functions: cut grep head ifconfig
-2

Get the line containing "inet addr:" and the line before that, get down to only the first line, and then get the first word on that line, which should be the interface.

ifconfig eth0 | grep "inet:" | cut -d ":" -f2 | cut -d " " -f1
plink -v -ssh -N -D 8580 -l USERNAME -pw PASSWARD 192.168.2.12
grep -r "sampleString" . |uniq | cut -d: -f1 | xargs sed -i "/sampleString/d"
2009-02-09 20:21:30
User: sk1418
Functions: cut grep sed xargs
-2

checking files in current and sub directories, finding out the files containing "sampleString" and removing the containing lines from the file.

* Beware that The command will update the original file [no backup].

The command can be extended if play with 'find' command together,

e.g. it is possible to execute on certain type of files: *.xml, *.txt... (find -name "*.xml" | grep....)

if anybody knows a better solution on that, please drop a comment. thx.

ls /some/directory | sed -rn -e 's/input_file_regex/mv -v & output_file_name/p' | sh
2009-03-25 09:20:15
User: polar
Functions: ls sed
Tags: bash sed
-2

Allows for quick mass renaming, assuming the user has some familiarity with regular expressions. Basically, it replaces the original_file_name in the output of ls with

"mv -v original_file_name new_file_name"

and passes the output to sh.

feh --bg-center `ls -U1 |sort -R |head -1`