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 - 715 results
<ALT> .
<ESC> .
removedir(){ read -p "Delete the current directory $PWD ? " human;if [ "$human" = "yes" ]; then [ -z "${PWD##*/}" ] && { echo "$PWD not set" >&2;return 1;}; rm -Rf ../"${PWD##*/}"/ && cd ..; else echo "I'm watching you" | pv -qL 10; fi; }
echo -e "swap=me\n1=2"|sed 's/\(.*\)=\(.*\)/\2=\1/g'
alias PS1="man bash | sed -n '/ASCII bell/,/end a sequence/p'"
2010-01-15 23:39:28
User: haivu
Functions: alias
Tags: bash prompt ps1
3

I use this command (PS1) to show a list bash prompt's special characters. I tested it against A flavor of Red Hat Linux and Mac OS X

exipick -zi | xargs exim -Mrm
for i in `mailq | awk '$6 ~ /^frozen$/ {print $3}'`; do exim -Mrm $i; done
2010-01-13 21:28:45
User: rjamestaylor
Functions: awk
Tags: bash awk exim
-2

Although Exim will purge frozen (undeliverable) messages over time, the command "exim -Mrm #id#" where #id# is a particular message ID will purge a message immediately. Being lazy, I don't want to type the command for each frozen message, so I wrote the one-liner to do it for me.

echo 'mkcd() { mkdir -p "$@" && cd "$_"; }' >> ~/.bashrc
2010-01-13 09:37:56
User: phaidros
Functions: cd echo mkdir
-4

combines mkdir and cd

added quotes around $_, thanx to flatcap!

for file in $(seq -f '%03.f' 1 $TOTAL ); do echo "($file/$TOTAL)"; curl -f -O http://domain.com/Name_$file.ext; done
2010-01-12 15:23:44
User: nordri
Functions: echo file seq
-4

With counter format [001, 002, ..., 999] , nice with pictures or wallpapers collections.

wget http://domain.com/file{1..100}
find /path/to/images -name '*.JPG' -exec bash -c 'mv "$1" "${1/%.JPG/.jpg}"' -- {} \;
2010-01-07 15:41:17
User: sorpigal
Functions: bash find
Tags: bash find mv
7

Recursively rename .JPG to .jpg using standard find and mv. It's generally better to use a standard tool if doing so is not much more difficult.

<alt+50>-
2010-01-07 15:32:47
User: kobayashison
-3

In bash, by pressing ALT+n and then a character x, x will be printed n times

I know is not the same as the original command, but is correlated.

for k in `git branch|sed s/^..//`;do echo -e `git log -1 --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" "$k"`\\t"$k";done|sort
sed -i.bak 's/old/new/g' file
2010-01-06 17:04:05
User: deltaray
Functions: sed
Tags: bash sed
2

sed already has an option for editing files in place and making backup copies of the old file. -i will edit a file in place and if you give it an argument, it will make a backup file using that string as an extension.

read enterKey
2009-12-29 00:18:32
User: bbmarek
Functions: read
Tags: bash read
-4

how to finish command or script without any output

cat | gcc -x c -o a.out - && ./a.out && rm a.out
2009-12-27 04:37:24
User: dgalling
Functions: c++ cat gcc rm
-2

This should work on any unix platform running bash. Just type the program into cat and give it a ^D when you're done, at which time it will compile, run, and remove the program. Obviously, you can run it without the "rm a.out" if you'd like to keep the binary. If you want to keep the source, well, you might as well just write it in vi or emacs first then.

php -i | grep php.ini
2009-12-23 15:52:20
User: jemmille
Functions: grep
Tags: bash grep PHP
5

Quick and easy way to find out which php.ini file is being used. Especially useful if you just need to find the location of the file for editing purposes.

vim -n -es -c 'g/# CommandParse/+2,/^\s\+esac/-1 d p | % d | put p | %<' -c 'g/^\([-+]\+[^)]\+\))/,/^\(\s\+[^- \t#]\|^$\)/-1 p' -c 'q!' $0
2009-12-19 08:32:00
User: syladmin
Functions: vim
0

A really fun vim oneliner for auto documenting your option's parsing in your script.

# print the text embeded in the case that parse options from command line.

# the block is matched with the marker 'CommandParse' in comment, until 'esac'

extract_cmdl_options()

{

# use vim for parsing:

# 1st grep the case block and copy in register @p + unindent in the buffer of the file itself

# 2nd filter lines which start with --opt or +opt and keep comment on hte following lines until an empty line

# 3rd discard changes in the buffer and quit

vim -n -es -c 'g/# CommandParse/+2,/^\s\+esac/-1 d p | % d | put p | %

-c 'g/^\([-+]\+[^)]\+\))/,/^\(\s\+[^- \t#]\|^$\)/-1 p' \

-c 'q!' $0

}

example code:http://snipplr.com/view/25059/display-embeded-comments-for-every-opt-usefull-for-auto-documenting-your-script/

p() { l=$LINES; case $1 in do) shift; IFS=$'\n' _pg=( $("$@") ) && _pgn=0 && p r;; r) echo "${_pg[*]:_pgn:$((l-4))}";; d) (( _pgn+=l-4 )); (( _pgn=_pgn>=${#_pg[@]}?${#_pg[@]}-l+4:_pgn )); p r;; u) (( _pgn=_pgn<=l-4?0:_pgn-$l-4 )); p r;; esac; }
2009-12-18 23:35:53
User: intuited
Functions: echo
Tags: bash pager
0

Manpages, command summaries, and pretty much everything else usually have the information you're most likely to want at the beginning. Seeing just the last 40 or so lines of options from a command that has 100 is not super useful, and having to scroll up each time you want to glance at something is spastic.

Run this and then do something like

p do vi --help

and you'll get the first screen(-mostly-)full of vi's usage info and options list

Then use

p d

to page down, and

p u

to page up.

To see the current page again:

p r

Also useful for situations like

p do aptitude search ~dsmorgasbord p next #p sudo aptitude -r install libwickedawesome-perl-snoochieboochies p next p sudo aptitude -r install libwickedawesome-perl-snoochieboochies snazztasticorama-dev-v0.&#8734;

where you're using readline up-arrow, HOME, END, etc., to quickly recall commented commands.

For the unaware, that option to aptitude search will bring up all of the packages whose descriptions contain the string "smorgasbord". Depending on your distro, there could potentially be hundreds of them.

while [ 1 ]; do banner 'ze missiles, zey are coming! ' | while IFS="\n" read l; do echo "$l"; sleep 0.01; done; done
2009-12-14 07:40:07
User: craigds
Functions: banner echo read sleep
10

Displays a scrolling banner which loops until you hit Ctrl-C to terminate it.

Make sure you finish your banner message with a space so it will loop nicely.

dd if=/dev/sda | tee >(dd of=/dev/sdb) | dd of=/dev/sdc
2009-12-11 17:34:38
User: nerd65536
Functions: dd tee
Tags: bash tee dd pipe root
21

If you have some drive imaging to do, you can boot into any liveCD and use a commodity machine. The drives will be written in parallel.

To improve efficiency, specify a larger block size in dd:

dd if=/dev/sda bs=64k | tee >(dd of=/dev/sdb bs=64k) | dd of=/dev/sdc bs=64k

To image more drives , insert them as additional arguments to tee:

dd if=/dev/sda | tee >(dd of=/dev/sdb) >(dd of=/dev/sdc) >(dd of=/dev/sdd) | dd of=/dev/sde
while read str; do echo "$((++i)) - $str"; done < infile
2009-12-09 15:11:25
User: putnamhill
Functions: echo read
Tags: bash
-1

Hi glaudiston, you can save a few chars by leaving out cat and pipe and still enjoy the added flexibility.

find . -type d -exec sh -c "normalize-audio -b \"{}\"/*.mp3" \;
2009-12-08 03:13:13
Functions: find sh
-2

Execute this in the root of your music library and this recurses through the directories and normalizes each folder containing mp3s as a batch. This assumes those folders hold an album each. The command "normalize-audio" may go by "normalize" on some systems.

while read n; do host $n; done < list
perl -e '$_=`ifconfig eth0`;/\d+.\d+.\d+.\d+ /; print $&,"\n";'
2009-12-05 14:24:48
Functions: perl
0

If you are interested in interfaces other than eth0 you will need to change eth0 to your interface name.

You could use this mammoth to nab the ip4 addresses of all your interfaces

perl -e '@_=`ifconfig -a`; sort(@_); foreach(@_) { /(inet addr\:)(\d+.\d+.\d+.\d+ )/; $_=$2; @uniq=grep($_ ne $prev && (($prev) = $_), @_);} print join "\n",@uniq,"\n"; '

it seems silly to have all this code when the following will work fine

ifconfig -a | grep "inet " | awk -F":" ' { print $2 } ' | cut -d " " -f1