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 tagged bash from sorted by
Terminal - Commands tagged bash - 706 results
for i in `find -name '*_test.rb'` ; do mv $i ${i%%_test.rb}_spec.rb ; done
2012-10-09 14:08:38
User: olopopo
Functions: mv
0

Renames all files ending in "_test.rb" to "_spec.rb"

yes | !!
2012-10-05 10:09:39
User: freck
Functions: yes
Tags: bash
1

I doubt this works with other than bash, but then again, I havent tried.

The 'yes' utility is very simple, it outputs a hell of a lot of 'y's to standard input.

The '!!' command means 'the last command'. So this one-lines inputs a lot of y's into the last command, aggressively agreeing to everything. For instance, when doing apt-get.

. ~/.bashrc
source ~/.bashrc
2012-10-01 08:30:19
User: knoppix5
4

Any changes to BASH shell made in .bashrc will be active in the current terminal window from the moment you execute this command, ie. aliases, prompt settings etc. No need to restart terminal.

(In BASH 'source' simile to 'eval' lets you generally execute any bunch of commands stacked in a text file).

brd = "! f() { for k in $(git branch $@ | sed 's/^..//; s/ .*//'); do echo "$(git log -1 --pretty='%Cgreen%ci %Cblue(%cr)%Creset ' $k) $k" ; done | sort -r; }; f"
2012-09-28 10:20:51
0

Add this line to your ~/.gitconfig for a git alias "git brd" (i.e., brd = (br)anch+(d)ate) which sorts branches by date. Allows you to pass in limited "git branch" options such as "-r" (remote) or "-a" (all). (Note: forum added "$" prefix to command; obviously in gitconfig there is no "$" prefix.)

git for-each-ref --sort='-authordate' --format='%(refname)%09%(authordate)' refs/heads | sed -e 's-refs/heads/--'
source <(egrep '^#define EX_.*' /usr/include/sysexits.h | sed -e 's/#define/declare -r/g' | sed 's/\//#/g' | sed -e 's/\s\{1,\}/ /g' | sed -e 's/ \([0-9]\)/\=\1/'g )
2012-09-21 13:30:37
User: fishdaemon
Functions: egrep sed
-1

Use meaningful exit codes

change "source" to "cat" to view output instead of assigning

typeset -f
2012-09-17 13:48:39
User: kLeZ
Tags: bash function
2

typeset command gives to stdout all the functions defined in a bash session, -f and -F switches are for: all functions names with body (-f) and all functions names only (-F).

function cdf () { [ -f $1 ] && { cd $(dirname $1); } || { cd $1 ; }; pwd; };
2012-09-08 10:50:58
User: Josso
Functions: cd dirname
Tags: bash ksh
0

Had trouble with the other function, because of missing semicolons. (According to my bash on OS X)

(cat ~/.bash_history;U='curl -s www.commandlinefu.com';$U/users/signin -c/tmp/.c -d'username=<USER>&password=<PASS>&submit=1'|$U/commands/favourites/json -b/tmp/.c|grep -Po 'nd":.*?[^\\]",'|sed -re 's/.*":"(.*)",/\1/g')>~/.h;HISTFILE=~/.h bash --login
2012-08-17 12:31:51
User: xenomuta
Functions: bash cat grep sed
5

This makes your commandlinefu.com's favorites appear as most recent commands in your history.

:!bash
2012-08-13 17:13:53
User: CLIxBOBxBOM
Tags: bash vim vi
2

Helps when I'm editing a script and want to double check some commands without having to exit out of vi multiple times or having to use another terminal session.

alias irc="screen -D -R -S chatclient irssi"
2012-08-12 13:24:43
User: expelledboy
Functions: alias
2

This command attempts to attach to existing irssi session, if one exists, otherwise creates one.

I use "irc" because I use different irc clients depending on what system I am working on. Consistency is queen.

for folder in $( find $( pwd ) -maxdepth 1 -type d | grep -v .svn ); do svn propset svn:ignore -F ignorelist ${folder}; done
2012-08-03 13:50:45
User: Highwayman
Functions: find grep pwd
Tags: bash
0

To use this comment you'll have to create a file entitled 'ignorelist' where you put the file name or pattern of the files you want to ignore. I used it for my maven project which generates the child project files in each folder so I can import them into eclipse. By adding these project files to the ignore list ensure they won't appear each time I run 'svn status'.

egrep '.*(("STATUS)|("HEAD)).*' http_access.2012.07.18.log | awk '{sum+=$11; ++n} END {print "Tot="sum"("n")";print "Avg="sum/n}'
2012-07-27 12:18:29
User: fanchok
Functions: awk egrep
0

Depending on your Apache access log configuration you may have to change the sum+=$11 to previous or next awk token.

Beware, usually in access log last token is time of response in microseconds, penultimate token is size of response in bytes. You may use this command line to calculate sum and average of responses sizes.

You can also refine the egrep regexp to match specific HTTP requests.

eselect bashcomp enable --global $(eselect bashcomp list | sed -e 's/ //g'| cut -d']' -f2 | sed -e 's/\*//'| xargs)
set -x && PS4='+\t '
umount -a -t cifs
up() { [ $(( $1 + 0 )) -gt 0 ] && cd $(eval "printf '../'%.0s {1..$1}"); }
2012-06-15 17:10:45
User: Mozai
Functions: cd eval
Tags: bash cd
6

`up 3` will climb the directory tree by three steps. `up asdf` will do nothing, and returns exit code 1 as an error should.

find ./ -type f -exec sh -c 'echo "{}" "$(dirname "{}")/$(basename "{}" | tr "[A-Z]" "[a-z]")"' \;
2012-06-14 07:13:42
User: jelloir
Functions: find sh
Tags: bash find mv rename tr
0

Handles spaces in file names and directories. Optionally change directories as well by pipe to tr from dirname.

stty -ixon
2012-05-28 19:04:19
User: ricardofunke
Functions: stty
1

This command disable sending of start/stop characters.

It's useful when you want to use incremental reverse history search forward shortcut (Ctrl+s).

To enable again, type:

stty -ixoff
bind -P
2012-05-28 18:51:59
User: ricardofunke
38

This command shows the various shortcuts that can be use in bash, including Ctrl+L, Ctrl+R, etc...

You can translate "\C-y" to Ctrl+y, for example.

desiredDay=6; year=2012; month=5; n=0; while [ $(date -d "$year-$((month+1))-1 - $n day" "+%u") -ne $desiredDay ]; do n=$((n+1)); done; date -d "$year-$((month+1))-1 - $n day" "+%x"
2012-05-17 12:02:30
Functions: date
0

Choosing your year and month. You only need the gnu date command and bash. desiredDay of the week is (1..7); 1 is Monday.

If you want desiredDay of week (0..6); 0 is Sunday

desiredDay=6; year=2012; month=5; n=0; while [ $(date -d "$year-$((month+1))-1 - $n day" "+%w") -ne $desiredDay ]; do n=$((n+1)); done; date -d "$year-$((month+1))-1 - $n day" "+%x"
export PROMPT_COMMAND=$PROMPT_COMMAND'; export PWD_UPCASE="${PWD^^}"'; export PS1='C:${PWD_UPCASE//\\//\\\\}>'
2012-04-30 20:09:49
User: chungy
Functions: export
0

(This may be specific to bash only.) This transforms the current working directory to all uppercase characters and replaces forward slashes with backslashes, prepending the string with "C:" and appending a single ">". It serves no practical purpose, but may serve as a great practical joke if you can insert it to some victim's .bashrc :-)

compgen -c | sort -u > commands && less commands