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 /bin; set -- *; x=$((1+($RANDOM % $#))); man ${!x})
tar -cf - ./file | lzma -c | ssh user@sshserver $(cd /tmp; tar --lzma -xf -)
cd
bsro3 () { P=`pwd`; S=$1; R=$2; ls *.odt > /dev/null 2>&1; if [[ $? -ne 0 ]]; then exit 1; fi; for i in *.odt; do mkdir ${P}/T; cd ${P}/T; unzip -qq "$P"/"$i"; sed -i "s/$S/$R/" ${P}/T/content.xml; zip -qq -r "$P"/"$i" *; cd ${P}; rm -rf ${P}/T; done; }
2010-06-30 04:43:54
User: danpos
Functions: cd exit ls mkdir rm sed
2

This function does a batch edition of all OOO3 Writer files in current directory. It uses sed to search a FOO pattern into body text of each file, then replace it to foo pattern (only the first match) . I did it because I've some hundreds of OOO3 Writer files where I did need to edit one word in each ones and open up each file in OOO3 gui wasn't an option. Usage: bsro3 FOO foo

for d in $(find . -maxdepth 1 -type d -name '[^.]*'); do cd "$d"; svn up; cd ..; done
2010-05-28 10:09:19
User: udog
Functions: cd find
-2

If you have a directory with many working copies of various subversion projects and you want to update them all at once, this one may be for you.

cd /some/empty/folder/website_diffs/sitename && wget -N http://domain.com/ 2>&1 |grep -q "o newer" || printf "Sites web page appears to have updated.\n\nSuggest you check it out.\n\n"|mail -s "Sites page updated." david@email.com
2010-05-09 07:28:42
User: DaveQB
Functions: cd grep mail printf wget
2

A cronjob command line to email someone when a webpages homepage is updated.

find ~ -maxdepth 2 -name .git -print | while read repo; do cd $(dirname $repo); git pull; done
cd() { if [ -z "$1" ]; then command cd; else if [ -f "$1" ]; then command cd $(dirname "$1"); else command cd "$1"; fi; fi; }
2010-04-23 19:17:43
User: xeor
Functions: cd command dirname
Tags: cd command
9

This little function will smarten 'cd'. If you try to cd into a file (which I guess we all have done), it cd's into the directory of that file instead.

I had to use nesten if's, to get cd to still work with 'cd' (to get to $HOME), 'cd -' (to get to last directory), and 'cd foo\ bar'.

cd /etc/init.d && sudo ./apache2 start
2010-04-17 16:01:18
User: nco
Functions: cd sudo
-9

This command is designed in order to avoid this error :

/etc/init.d/apache2 force-reload

* Reloading web server config apache2 httpd not running, trying to start

(13)Permission denied: make_sock: could not bind to address 0.0.0.0:80

no listening sockets available, shutting down

Unable to open logs

[fail]

for host in *; do { if [ -d $host ]; then { cd ${host}; for share in *; do { if [ -d $share ]; then { cd $share; rsync -av --delete rsyncuser@$host::$share . 2>../$share.err 1>../$share.log; cd ..; }; fi; }; done; cd ..; }; fi; }; done;
2010-03-11 19:54:31
User: c3w
Functions: cd host rsync
Tags: rsync
0

traverses e.g. "/data/myhost1.com/myrsyncshare"; logs stderr and stdout. useful with cron.

export QQ=$(mktemp -d);(cd $QQ; curl -s -O http://www.commandlinefu.com/commands/browse/sort-by-votes/plaintext/[0-2400:25];for i in $(perl -ne 'print "$1\n" if( /^(\w+\(\))/ )' *|sort -u);do grep -h -m1 -B1 $i *; done)|grep -v '^--' > clf.sh;rm -r $QQ
2010-01-30 19:47:42
User: bartonski
Functions: cd export grep mktemp perl sort
8

Each shell function has its own summary line, as a comment. If there are multiple shell functions with the same name, the function with the highest number of votes is put into the file.

Note: added 'grep -v' to the end of the pipeline, to eliminate extraneous lines containing only '--'. Thanks to matthewbauer for pointing this out.

dmd () { ( if [ "$1"x != "x" ]; then cd $1; fi; mkdir `date +%Y%m%d` ) }
2010-01-27 15:53:26
User: bartonski
Functions: cd mkdir
1

Creates a directory named with the current date, in the format YYYYMMDD. If you give it a directory name as an argument, it will create the new directory inside the specified directory.

This is an alternative to command #1993.

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; }
removedir () { echo "Deleting the current directory $PWD Are you sure?"; read human; if [[ "$human" = "yes" ]]; then blah=$(echo "$PWD" | sed 's/ /\\ /g'); foo=$(basename "$blah"); rm -Rf ../$foo/ && cd ..; else echo "I'm watching you" | pv -qL 10; fi; }
2010-01-17 11:34:38
User: oshazard
Functions: basename cd echo read rm sed
-3

CHANGELOG

Version 1.1

removedir () { echo "You are about to delete the current directory $PWD Are you sure?"; read human; if [[ "$human" = "yes" ]]; then blah=$(echo "$PWD" | sed 's/ /\\ /g'); foo=$(basename "$blah"); rm -Rf ../$foo/ && cd ..; else echo "I'm watching you" | pv -qL 10; fi; }

BUG FIX:

Folders with spaces

Version 1.0

removedir () { echo "You are about to delete the current directory $PWD Are you sure?"; read human; if [[ "$human" = "yes" ]]; then blah=`basename $PWD`; rm -Rf ../$blah/ && cd ..; else echo "I'm watching you" | pv -qL 10; fi; }

BUG FIX:

Hidden directories (.dotdirectory)

Version 0.9

rmdir () { echo "You are about to delete the current directory $PWD. Are you sure?"; read human; if [[ "$human" = "yes" ]]; then blah=`basename $PWD`; rm -Rf ../$blah/ && cd ..; else echo "I'm watching you" | pv -qL 10; fi; }

Removes current directory with recursive and force flags plus basic human check. When prompted type yes

1. [user@host ~]$ ls

foo bar

2. [user@host ~]$ cd foo

3. [user@host foo]$ removedir

4. yes

5. rm -Rf foo/

6. [user@host ~]$

7. [user@host ~]$ ls

bar

wtzc () { wget "$@"; foo=`echo "$@" | sed 's:.*/::'`; tar xzvf $foo; blah=`echo $foo | sed 's:,*/::'`; bar=`echo $blah | sed -e 's/\(.*\)\..*/\1/' -e 's/\(.*\)\..*/\1/'`; cd $bar; ls; }
2010-01-17 11:25:47
User: oshazard
Functions: cd sed tar wget
-1

Combines a few repetitive tasks when compiling source code. Especially useful when a hypen in a file-name breaks tab completion.

1.) wget source.tar.gz

2.) tar xzvf source.tar.gz

3.) cd source

4.) ls

From there you can run ./configure, make and etc.

mkdir dir; cd $_
2010-01-13 11:56:36
Functions: cd mkdir
1
_ expands to the last argument of the last command that was executed
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!

function ..(){ for ((j=${1:-1},i=0;i<j;i++));do builtin cd ..;done;}
2010-01-02 08:36:12
User: bhepple
Functions: cd
Tags: cd
3

Instead of typing "cd ../../.." you can type ".. 3". For extremely lazy typists, you can add this alias:

alias ...=".. 2" ....=".. 3"

- so now you can write just .... !!!

NB the .. function needs to be "source"d or included in your startup scripts, perhaps .bashrc.

cd /usr/home && for i in *;do chsh -s bash $i;done
2009-12-31 18:48:53
User: casidiablo
Functions: bash cd chsh
6

This command will set bash as the default shell for all users in a FreeBSD system.

cd() { if [[ "$1" =~ ^\.\.+$ ]];then local a dir;a=${#1};while [ $a -ne 1 ];do dir=${dir}"../";((a--));done;builtin cd $dir;else builtin cd "$@";fi ;}
2009-10-29 21:43:51
User: eightmillion
Functions: cd
Tags: cd
3

This is a kind of wrapper around the shell builtin cd that allows a person to quickly go up several directories.

Instead of typing:

cd ../..

A user can type:

cd ...

Instead of:

cd ../../..

Type:

cd ....

Add another period and it goes up four levels. Adding more periods will take you up more levels.

pushd .> /dev/null; cd /; for d in `echo $OLDPWD | sed -e 's/\// /g'`; do cd $d; echo -n "$d "; ls -ld .; done; popd >/dev/null
2009-10-22 12:32:11
User: syladmin
Functions: cd echo ls sed
Tags: permissions
0

Can easily be scripted in order to show permission "tree" from any folder. Can also be formated with

column -t

{ pushd .> /dev/null; cd /; for d in `echo $OLDPWD | sed -e 's/\// /g'`; do cd $d; echo -n "$d "; ls -ld .; done; popd >/dev/null ; } | column -t

from http://www.commandlinefu.com/commands/view/3731/using-column-to-format-a-directory-listing

script_path=$(cd $(dirname $0);pwd)
2009-10-14 16:04:03
User: jgc
Functions: cd dirname
Tags: cd pwd PATH
7

Another way of doing it that's a bit clearer. I'm a fan of readable code.

PATH=$(cd ${0%/*}; pwd)
( cd /my/directory; xterm& )
2009-10-13 13:07:21
User: ashawley
Functions: cd
Tags: subshells
-4

Perfect time for the rarely used sub shell.

gitstart () { if ! [[ -d "$@" ]]; then mkdir -p "$@" && cd "$@" && git init; else cd "$@" && git init; fi }