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 - 178 results
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
43

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.

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.