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
tmpfs(){ cd /;for i in $@;do tar czvf /tmp/$i $i;mount -t tmpfs tmpfs /$i;tar xvzf /tmp/$i;cd ~ ;}# usage: tmpfs etc var
_ff(){ cd /mnt;echo /mnt/*/* |sed 's/ \/mnt\//\&/g' |sed '/'"$1"'/!d'; cd -;}
2011-12-30 23:25:31
User: argv
Functions: cd echo sed
-5

_ff(){

cd /mnt;

echo /mnt/*/* |sed '

s/ \/mnt\//\&/g;

'|sed '/'"$1"'/!d';

cd -;

}

ff(){

case $# in

0)

echo "usage: ff glob [sed-cmds] [--|var-name]"

;;

1)

_ff $1 |sed =

;;

[2-9])

case $2 in

--) _ff $1 |less -SN

;;

*) _ff $1 |sed -n ''"$2"''|tr '\n' '\040' |sed 's/.*/export '"$3"'=\"&/;s/=\" /=\"/;s/ $/\"/' > $HOME/.ff;

case $# in

3)

. $HOME/.ff

;;

esac;

sed '

s/export .*=\"/\$'"$3"' = \"/;' $HOME/.ff;\

;;

esac

;;

esac;

}

v(){

local a=$HOME;

sed '

s/export /less -n \$/;

s/=.*//;

' $a/.ff > $a/.v ;

. $a/.v ;

}

Another approach using ls(1)

lsl(){

_lsl ()

{

ls -l $3 /mnt/*/$1* 2>/dev/null;

};

case $# in

0)

echo "usage: lsl pat [ls-options|result-no]";

echo "usage: lsle pat [sed-cmds]"

;;

1)

_lsl $1 |sed =

;;

2)

case $2 in

-*) _lsl $1 $@;;

*)

_lsl $1 |sed 's/.* //;

'"$2"'!d;

'"$2"'q' > $HOME/.lsl ;

export v=$(sed 1q $HOME/.lsl);

echo \$v = $v

;;

esac

;;

esac;

}

exp(){

echo "%s/\$/ /";

echo "%j";

echo "s/^/export v=\"";

echo "s/\$/\"";

echo "s/ \"\$/\"";

echo ".";

echo "wq";

}

lsle(){

lsl $1 -1 |sed $2 > .lsl&&

exp |ed -s .lsl >&-&&

. .lsl&&

echo \$v = $v;

}

STARTING_DIR=$(cd $(dirname $0) && pwd)
2011-11-30 17:35:15
User: bbbco
Functions: cd dirname
0

Sometimes you need the full path to your script, regardless of how it was executed (which starting directory) in order to maintain other relative paths in the script.

If you attempt to just use something simple like:

STARTING_DIR="${0%/*}"

you will only get the relative path depending on where you first executed the script from.

You can get the relative path to the script (from your starting point) by using dirname, but you actually have to change directories and print the working directory to get the absolute full path.

cd ~/ruby/project_a ; find . -name "*profile*" -exec pax -rw {} ~/ruby/project_b/ \;
2011-11-10 21:18:15
User: cbpowell
Functions: cd find pax
0

Let's say you have a set of files in tree A that you want duplicated to tree B while preserving their directory structure / hierarchy. (For example, you might want to copy your 'profile' model/views/controller from one Rails application to another.) The "pax" command will copy all matching files to the destination while creating any necessary directories.

#!/bin/bash cd /usr/share/consolefonts/; for i in * ; do setfont; echo "testing >> $i << font" ; setfont $i ; showconsolefont ; sleep 5 ; clear ; done
export HISTIGNORE=' cd "`*: PROMPT_COMMAND=?*?'
2011-10-18 19:58:39
User: mpb
Functions: cd export
1

I was surprised to find that with RedHat bash, I could not find any comment lines (begining with #) in my bash shell history. Surprised because in Mageia Linux this works. It turns out that RedHat's bash will keep comment lines if in my .bashrc, I define:

export HISTIGNORE=' cd "`*: PROMPT_COMMAND=?*?'

Why have comment lines in shell history? It's a handy and convenient way to make proto-commands (to be completed later) and for storing brief text data that is searchable in shell history.

upto() { cd "${PWD/\/$@\/*//$@}" }
jda() { cd $(pwd | sed "s/\(\/$@\/\).*/\1/g"); }
jd() { cd **/"$@"; }
2011-10-05 11:47:57
User: sharfah
Functions: cd
-3

Usage: jd dir

Requires globstar. To set globstar use:

shopt -s globstar
tar cvf - /src | ( cd /dest ; tar xvf - )
2011-09-22 15:35:24
User: a8ksh4
Functions: cd tar
-3

This is how I've done it in the past

(cd src && tar -cf - .) | (cd dest && tar -xpf -)
2011-09-22 11:30:57
User: Cowboy
Functions: cd tar
0

Simple tar pipe to be used to copy directories while including hidden files and maintaining file permissions

cd /path/to/pmwiki/wiki.d;/bin/ls -1 | perl -ne 'my ($group,$name)=split(/\./);$counts{$group}++;' -e 'END { foreach $group (sort keys %counts) {printf("%d\t%s\n",$counts{$group},$group);} }'|sort -rn
2011-09-14 19:33:39
User: tamouse
Functions: cd perl sort
Tags: sort perl pmwiki
-2

PmWiki stores wiki pages as Group.Name. Simply split the directory listing and count frequency of group occurances.

myreadlink() { [ ! -h "$1" ] && echo "$1" || (local link="$(expr "$(command ls -ld -- "$1")" : '.*-> \(.*\)$')"; cd $(dirname $1); myreadlink "$link"; }
2011-09-13 11:02:27
User: keymon
Functions: cd command dirname echo ls
0

This is a equivalent to the GNU ' readlink' tool, but it supports following all the links, even in different directories.

An interesting alternative is this one, that gets the path of the destination file

myreadlink() { [ ! -h "$1" ] && echo "$1" || (local link="$(expr "$(command ls -ld -- "$1")" : '.*-> \(.*\)$')"; cd $(dirname $1); myreadlink "$link" | sed "s|^\([^/].*\)\$|$(dirname $1)/\1|"); }
mkdir /home/foo/doc/bar && cd $_
2011-08-12 11:29:19
User: kzh
Functions: cd mkdir
44

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

cd $(ls -ltr|grep ^d|head -1|sed 's:.*\ ::g'|tail -1)
2011-08-10 03:39:35
Functions: cd grep head ls sed tail
-1

Replace the head -1 with head -n that is the n-th item you want to go to.

Replace the head with tail, go to the last dir you listed.

You also can change the parameters of ls.

cd / ; tar -cvpzf backup.tar.gz --exclude=/backup.tar.gz --exclude=/proc --exclude=/lost+found --exclude=/sys --exclude=/mnt --exclude=/media --exclude=/dev /
2011-07-20 15:44:07
User: strzel_a
Functions: cd tar
0

Backup your entire system on a tar ball file format.

tar cf - . |(cd /targetdir; tar xvf -)
cd -
2011-07-05 05:28:32
Functions: cd
-1

Add dirs to list:

pushd /tmp

/tmp ~

then

cd -

/home/user

cd -

/tmp

cd $(ls -1t --color=never | head -1)
cd -
cd() { if [ -n "$1" ]; then [ -f "$1" ] && set -- "${1%/*}"; else [ -n "$CDDIR" ] && set -- "$CDDIR"; fi; command cd "$@"; }
2011-06-24 08:48:13
User: flatcap
Functions: cd command set
Tags: cd test set
0

Move efficiently between directories.

.

This command adds a couple of extra features to cd, without affecting normal use.

CDPATH use is also unaffected. It introduces and environment variable CDDIR which is used as an alternate home directory.

.

Note: I don't want to alter $HOME because then all my dot files will move.

.

Examples:

.

cd dir

Change directory to "dir" (using CDPATH if necessary)

.

cd dir/file.txt

Change directory to "dir" (containing folder of "file.txt")

This allows you to cut'n'paste, or use

.

CDDIR is unset

cd

Change directory to $HOME

.

CDDIR=/home/flatcap/work

cd

Change directory to /home/flatcap/work

.

For convenience, put the command, and the following, in your .bashrc or .bash_profile

export CDDIR="/home/flatcap/work"

alias cdd="CDDIR=$(pwd)"

parallel -j4 cd {}\; pwd\; git pull :::: <(git submodule status | awk '{print $2}')
2011-06-20 00:20:26
User: clvv
Functions: awk cd
2

Make sure to run this command in your git toplevel directory. Modify `-j4` as you like. You can also run any arbitrary command beside `git pull` in parallel on all of your git submodules.

cd /usr/share/gnome-shell/search_providers/ && cat google.xml | sed "s/www.google.com\/search/duckduckgo.com\//; s/Google/DuckDuckGo/g" > duckduckgo.xml
2011-05-27 22:07:36
User: scripteles
Functions: cat cd sed
0

Add DuckDuckGo Search as search provider on gnome-shell/gnome3 .

Needs root permission. To see the results, use alt+f2 and then type r.

for i in $(find . -name *md5checksum_file* | sed 's/\(\.\/.*\)md5checksum_file.txt/\1/'); do cd "$i"; md5sum -c "md5checksum_file.txt"; cd -; done | tee ~/checksum_results.txt | grep -v "<current directory>"
2011-05-17 01:08:44
User: gocoogs
Functions: cd find grep md5sum sed tee
0

extracts path to each md5 checksum file, then, for each path, cd to it, check the md5sum, then cd - to toggle back to the starting directory. greps at the end to remove cd chattering on about the current directory.

cd <directory>; touch ./-i
2011-05-12 11:01:58
User: ljmhk
Functions: cd touch
Tags: touch
15

Forces the -i flag on the rm command when using a wildcard delete.