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 tricks from sorted by
Terminal - Commands tagged bash tricks - 24 results
function mkdcd () { mkdir "$1" && cd "$1" }
for i in *ext; do mv $i ${i%.ext}; done
2011-11-13 03:58:08
User: paulochf
Functions: mv
-3

For those files in current folder that would be shown in `ls *ext`, for some extension ext, move/rename that file removing the .ext suffix from the file name.

It uses Bash's parameter substitution, as seen in

http://tldp.org/LDP/abs/html/parameter-substitution.html#PCTPATREF

(for analog use in prefix, see http://tldp.org/LDP/abs/html/parameter-substitution.html#PSOREX2 )

param=${param:-$(read -p "Enter parameter: "; echo "$REPLY")}
2011-09-08 20:48:31
User: frans
Functions: echo read
8

Can be used for command line parameters too.

If you have a more complicated way of entering values (validation, GUI, ...), then write a function i.e. EnterValue() that echoes the value and then you can write:

param=${param:-$(EnterValue)}
mkdir /home/foo/doc/bar && cd $_
2011-08-12 11:29:19
User: kzh
Functions: cd mkdir
43

The biggest advantage of this over the functions is that it is portable.

command & echo $!
2011-06-08 18:16:38
User: Mahrud
Functions: command echo
-2

Actually $! is an internal variable containing PID of the last job in background.

More info: http://tldp.org/LDP/abs/html/internalvariables.html#PIDVARREF

Using $! for job control:

possibly_hanging_job & { sleep ${TIMEOUT}; eval 'kill -9 $!' &> /dev/null; }
<ALT>+<.> or <ALT>+<NUM>+<.> or <ALT>+<NUM>,<ALT>+<.>
2011-03-01 17:41:08
User: aikikode
8

After executing a command with multiple arguments like

cp ./temp/test.sh ~/prog/ifdown.sh

you can paste any argument of the previous command to the console, like

ls -l ALT+1+.

is equivalent to

ls -l ./temp/test.sh

ALT+0+. stands for command itself ('ls' in this case)

Simple ALT+. cycles through last arguments of previous commands.

bind '"\C-e":"\eb `which \ef`\e\C-e"'
2011-01-26 16:11:52
User: jennings6k
0

Tested with bash v4.1.5 on ubuntu 10.10

Limitations:

as written above, only works for programs with no file extention (i.e 'proggy', but not 'proggy.sh')

because \eb maps to readine function backward-word rather then shell-backward-word (which

is unbinded by default on ubuntu), and correspondingly for \ef.

if you're willing to have Ctrl-f and Ctrl-g taken up too , you can insert the following lines

into ~/.inputrc, in which case invoking Ctrl-e will do the right thing both for "proggy" and "proggy.sh".

-- cut here --

\C-f:shell-backward-word

\C-g:shell-forward-word

"\C-e":"\C-f`which \C-g`\e\C-e"

-- cut here --

sort -t $'\t' -k 2 input.txt
2010-07-11 12:58:51
User: postrational
Functions: sort
4

Use this BASH trick to create a variable containing the TAB character and pass it as the argument to sort, join, cut and other commands which don't understand the \t notation.

sort -t $'\t' ... join -t $'\t' ... cut -d $'\t' ...
<ALT> .
<CTRL+w>
<ALT> <BACKSPACE>
2010-01-27 19:52:51
User: wincus
2

hit BACKSPACE more than once to delete more words

!$
2010-01-24 17:59:52
User: ringlerun
9

for example if you did a:

ls -la /bin/ls

then

ls !$

is equivalent to doing a

ls /bin/ls
<ALT> .
<ESC> .
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}
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.

md () { mkdir -p "$@" && cd "$@"; }
2009-09-24 16:09:19
User: drewk
Functions: cd mkdir
28

How often do you make a directory (or series of directories) and then change into it to do whatever? 99% of the time that is what I do.

This BASH function 'md' will make the directory path then immediately change to the new directory. By using the 'mkdir -p' switch, the intermediate directories are created as well if they do not exist.

Ctrl-R <search-text>
2009-09-20 05:07:31
User: tarkasteve
Tags: bash tricks
13

Searches backwards through your command-history for the typed text. Repeatedly hitting Ctrl-R will search progressively further. Return invokes the command.

cd `dirname $_`
cd !$:h
2009-08-07 00:37:08
User: lingo
Functions: cd
25

Uses the last argument of the last executed command, and gets the directory name from it.

Use $!:t for the filename alone, without the dirname.

a=`printf "%*s" 16`;b=${a//?/{0..1\}}; echo `eval "echo $b"`
FILENAME=${FILE##*/};FILEPATH=${FILE%/*};NOEXT=${FILENAME%\.*};EXT=${FILE##*.}