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 cd from sorted by
Terminal - Commands using cd - 186 results
cd /etc/network/if-up.d && iptables-save > firewall.conf && echo -e '#!/bin/sh -e\niptables-restore < $(dirname $0)/firewall.conf' > iptables && chmod a+x iptables
mydir=$(cd $(dirname ${BASH_SOURCE:-$0});pwd)
2011-04-27 16:33:38
User: xeor
Functions: cd dirname
Tags: cd script pwd
0

I submitted a command like this without $0 if $BASH_SOURCE is unset. Therefor, it did only work when using ./script, not using 'sh script'. This version handles both, and will set $mydir in a script to the current working directory. It also works on linux, osx and probably bsd.

find $PWD -type d | while read "D"; do cd "$D"; for filename in *.epub;do ebook-convert "$filename" "${filename%.epub}.mobi" --prefer-author-sort --output-profile=kindle --linearize-tables --smarten-punctuation --asciiize;done ;done
2011-04-19 15:51:50
User: rsimpson
Functions: cd find read
0

finds all epub files in the current directory and all child directories and converts them to .mobi format.

all of the ebook-convert -options are optional; the only parameters you are required to pass are the incoming file and the outgoing file, with the extension.

Has been tested on Ubuntu 10.10

current_dir=$(cd $(dirname $BASH_SOURCE);pwd)
2011-04-18 09:24:14
User: xeor
Functions: cd dirname
-2

I think this is the cleanest way of getting the current working directory of a script. It also works on osx, Linux, and probably bsd as well..

cd () { cdop=""; while [ "$1" != "${1#-}" ]; do cdop="${cdop} ${1}"; shift; done; if [ $# -eq 2 ]; then newdir="${PWD/$1/$2}"; [ -d "${newdir}" ] || { echo "no ${newdir}"; return 1; }; builtin cd $cdop "${newdir}"; else builtin cd $cdop "$@"; fi }
2011-04-07 14:36:26
User: splante
Functions: cd echo return
Tags: bash cd
0

ksh's version of cd has an optional syntax where you can type "cd old new" and it will replace "old" with "new" in your current directory and take you there. This is very handy when you have a parallel directory structure, like source and object directories. As suggested, you can just type cd ${PWD/old/new} to get this in bash, but this function in your .bashrc will let you type the ksh cd syntax and avoid typing the special characters while preserving other cd functionality.

take() { mkdir -p $1 && cd $1; }
2011-04-06 15:22:13
Functions: cd mkdir
Tags: cd mkdir
1

This creates a bash function `take` that you can call with the name of the directory as the first parameter. Add the function to ~/.bashrc to have it available anytime.

mydir(){mkdir -p $1 && cd $1}
2011-04-06 15:03:33
User: m0jumb0
Functions: cd
Tags: cd mkdir
0

Make it a reusable function and add the -p flag to mkdir to create directories recursively

usage: mydir some/dir/to/create

Dir=dirname; mkdir $Dir && cd $Dir
2011-04-06 14:53:57
User: saibbot
Functions: cd mkdir
Tags: cd mkdir
-7

Create a directory called "dirname" and navigate into it.

( cd $DIR; command; )
2011-03-29 13:16:00
User: sanmiguel
Functions: cd
Tags: bash cd
4

Obviously the example given is necessarily simple, but this command not only saves time on the command line (saves you using "cd -" or, worse, having to type a fully qualified path if your command cd's more than once), but is vital in scripts, where I've found the behaviour of "cd -" to be a little broken at times.

cd ${PWD/a/b}
2011-03-03 06:27:12
User: greggster
Functions: cd
Tags: bash cd
7

This is useful for quickly jumping around branches in a file system, or operating on a parellel file.

This is tested in bash. cd to (substitute in PWD, a for b) where PWD is the bash environmental variable for the "working directory"

screencast() { arecord -R 1000 -f cd -t wav $1.wav & RECPID=$!; echo "Starting screencast in new shell. Exit subshell to quit."; script -t 2> $1.timing -a $1.session; kill $RECPID; }
2011-01-20 14:35:47
User: bartonski
Functions: arecord cd echo kill script
2

This shell function takes a single argument, which is used as the base name of the .wav, .timing and .session files created. To create a screencast:

screencast test

type and talk ...

then type 'exit' or to exit the screencast.

test.wav will contain the audio from your screencast.

test.session will contain text and control characters needed to paint the screen

test.timing will contain timing information needed to synch individual keystrokes in test.session with the audio.

to play back:

aplay test.wav & scriptreplay test.{timing,session}

NOTE: because the shell function uses the variable "$!", and bash likes to expand '!' during history expansion, you will need to turn off bash's history before you enter the shell function.

This can be achieved using the command

set +H
git archive --format=tar HEAD | (cd /var/www/ && tar xf -)
2010-12-23 05:50:28
User: ox0spy
Functions: cd tar
4

in fact, I want to know, how to only get the modified files.

cd $srcdir && find -type d -exec mkdir -p $dstdir/{} \;
2010-12-18 12:13:39
User: bashrc
Functions: cd find mkdir
Tags: copy
1

Clone directory structure without the files

cd /usr/src ; wget http://www.rarlab.com/rar/unrarsrc-4.0.2.tar.gz ; tar xvfz unrarsrc-4.0.2.tar.gz ; cd unrar ; ln -s makefile.unix Makefile ; make clean ; make ; make install
find . -type d -print0 | (cd $DESTDIR; xargs -0 mkdir)
2010-11-18 09:33:51
User: rocketraman
Functions: cd find xargs
1

Here is how to replicate the directory structure in the current directory to a destination directory (given by the variable DESTDIR), without copying the files.

mv -n * ../; cd ..; rmdir $OLDPWD
2010-11-12 20:22:50
User: kevingranade
Functions: cd mv rmdir
Tags: mv rmdir
0

Avoid clobbering files by either overwriting due to name collisions or by assuming the command worked and deleting the target directory.

mv * .[0-9a-Z]* ../; cd ..; rm -r $OLDPWD
cd -
cd /proc&&ps a -opid=|xargs -I+ sh -c '[[ $PPID -ne + ]]&&echo -e "\n[+]"&&tr -s "\000" " "<+/cmdline&&echo&&tr -s "\000\033" "\nE"<+/environ|sort'
1

Grabs the cmdline used to execute the process, and the environment that the process is being run under. This is much different than the 'env' command, which only lists the environment for the shell. This is very useful (to me at least) to debug various processes on my server. For example, this lets me see the environment that my apache, mysqld, bind, and other server processes have.

Here's a function I use:

aa_ps_all () { ( cd /proc && command ps -A -opid= | xargs -I'{}' sh -c 'test $PPID -ne {}&&test -r {}/cmdline&&echo -e "\n[{}]"&&tr -s "\000" " "<{}/cmdline&&echo&&tr -s "\000\033" "\nE"<{}/environ|sort&&cat {}/limits' ); }

From my .bash_profile at http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html

alias sshdo='ssh -q -t root@localhost -- cd $PWD \&\& sudo'
2010-10-20 22:33:22
User: darkfader
Functions: alias cd
Tags: sudo su ssh-agent
-2

Run program as root by SSH key forwarding instead of sudoers.

Put this alias line in .bashrc or wherever you like. Alias arguments might need extra escaping.

You might wonder about security. But you'd block out root login as much as possible of course. In sshd_config you put this:

PermitRootLogin no

Match Address 127.0.0.1

  PermitRootLogin without-password

svn co http://simile.mit.edu/repository/crowbar/trunk&& cd ./trunk/xulapp/ xulrunner --install-app && Xvfb :1 && DISPLAY=:1 xulrunner application.ini 2>/dev/null 1>/dev/null && wget -O- "127.0.0.1:10000/&url=http://www.facebook.com"
2010-10-16 05:12:11
User: argv
Functions: cd wget
-1

some other options:

&delay=1000

&mode=links

much more with piggybank as scraper

works well with your favourite curses or non-curses http clients

to() { eval dir=\$$1; cd "$dir"; }
2010-10-15 13:40:35
User: hfs
Functions: cd eval
4

Set a bookmark as normal shell variable

p=/cumbersome/path/to/project

To go there

to p

This saves one "$" and is faster to type ;-) The variable is still useful as such:

vim $p/<TAB>

will expand the variable (at least in bash) and show a list of files to edit.

If setting the bookmarks is too much typing you could add another function

bm() { eval $1=$(pwd); }

then bookmark the current directory with

bm p
cd ~/Desktop && for FILES in $(ls); do mv $FILES .${FILES}; done
cd $(find -inum inode_no)
2010-09-25 06:04:32
User: emacs
Functions: cd find
-2

First use ls -i to list files and directories with their inode number

Then if you want to change to one of the directories, replace inode_no with its inode then execute the command

cd !$
2010-08-26 08:51:01
User: YCFlame
Functions: cd
-1

using "!$" will save another ton of typing than 'ALT+.' or ' .'