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 PATH from sorted by
Terminal - Commands tagged PATH - 35 results
echo $PATH | tr -s ':' '\n'
if [[ ":$PATH:" != *":$dir:"* ]]; then PATH=${PATH}:$dir; fi
2013-08-11 01:19:13
User: dmmst19
Tags: bash PATH $PATH
9

Sometimes in a script you want to make sure that a directory is in the path, and add it in if it's not already there. In this example, $dir contains the new directory you want to add to the path if it's not already present.

There are multiple ways to do this, but this one is a nice clean shell-internal approach. I based it on http://stackoverflow.com/a/1397020.

You can also do it using tr to separate the path into lines and grep -x to look for exact matches, like this:

if ! $(echo "$PATH" | tr ":" "\n" | grep -qx "$dir") ; then PATH=$PATH:$dir ; fi

which I got from http://stackoverflow.com/a/5048977.

Or replace the "echo | tr" part with a shell parameter expansion, like

if ! $(echo "${PATH//:/$'\n'}" | grep -qx "$dir") ; then PATH=$PATH:$dir ; fi

which I got from http://www.commandlinefu.com/commands/view/3209/.

There are also other more regex-y ways to do it, but I find the ones listed here easiest to follow.

Note some of this is specific to the bash shell.

convert_path2uri () { echo -n 'file://'; echo -n "$1" | perl -pe 's/([^a-zA-Z0-9_\/.])/sprintf("%%%.2x", ord($1))/eg' ;} #convert2uri '/tmp/a b' ### convert file path to URI
2013-07-01 08:54:45
User: totti
Functions: echo file perl
Tags: encoding PATH url
1

Really helpfull when play with files having spaces an other bad name. Easy to store and access names and path in just a field while saving it in a file.

This format (URL) is directly supported by nautilus and firefox (and other browsers)

echo "$: << '.'" >> $IRBRC
echo -e ${PATH//:/\\n}
realpath -s <filename>
2011-03-02 23:40:57
User: kalaxy
Tags: PATH
0

Will convert relative paths into absolute paths.

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 --

perl -le 'chomp($w=`which $ARGV[0]`);$_=`file $w`;while(/link\b/){chomp($_=(split/`/,$_)[1]);chop$_;$w.=" -> $_";$_=`file $_`;}print "\n$w";' COMMAND_NAME
2010-07-30 19:26:35
User: dbbolton
Functions: perl
0

This will show you any links that a command follows (unlike 'file -L'), as well as the ultimate binary or script.

Put the name of the command at the very end; this will be passed to perl as the first argument.

For obvious reasons, this doesn't work with aliases or functions.

rp() { local p; eval p=":\$$1:"; export $1=${p//:$2:/:}; }; ap() { rp "$1" "$2"; eval export $1=\$$1$2; }; pp() { rp "$1" "$2"; eval export $1=$2:\$$1; }
2010-07-15 18:52:01
User: cout
Functions: eval export
0

I used to do a lot of path manipulation to set up my development environment (PATH, LD_LIBRARY_PATH, etc), and one part of my environment wasn't always aware of what the rest of the environment needed in the path. Thus resetting the entire PATH variable wasn't an option; modifying it made sense.

The original version of the functions used sed, which turned out to be really slow when called many times from my bashrc, and it could take up to 10 seconds to login. Switching to parameter substitution sped things up significantly.

The commands here don't clean up the path when they are done (so e.g. the path gets cluttered with colons). But the code is easy to read for a one-liner.

The full function looks like this:

remove_path() { eval PATHVAL=":\$$1:" PATHVAL=${PATHVAL//:$2:/:} # remove $2 from $PATHVAL PATHVAL=${PATHVAL//::/:} # remove any double colons left over PATHVAL=${PATHVAL#:} # remove colons from the beginning of $PATHVAL PATHVAL=${PATHVAL%:} # remove colons from the end of $PATHVAL export $1="$PATHVAL" } append_path() { remove_path "$1" "$2" eval PATHVAL="\$$1" export $1="${PATHVAL}:$2" } prepend_path() { remove_path "$1" "$2" eval PATHVAL="\$$1" export $1="$2:${PATHVAL}" }

I tried using regexes to make this into a cleaner one-liner, but remove_path ended up being cryptic and not working as well:

rp() { eval "[[ ::\$$1:: =~ ^:+($2:)?((.*):$2:)?(.*):+$ ]]"; export $1=${BASH_REMATCH[3]}:${BASH_REMATCH[4]}; };
unset MANPATH; manpath >/dev/null
2010-07-02 19:45:27
Functions: manpath unset
Tags: man PATH
0

If I type 'man something', I want it to find the manpage in the same order as my PATH.

You can add something like this to your .bashrc

#

# Add my MacPorts, my personal utilities and my company utilities to my PATH.

export PATH=$PATH:/opt/local/bin:$HOME/bin:/our_company_utils/bin/

# Now set the manpath based on the PATH, after man(1) parses man.conf

# - No need to modify man.conf or manually modify MANPATH_MAP

# - Works on Linux, FreeBSD & Darwin, unlike /etc/manpaths.d/

# Must unset MANPATH first. MANPATH is set on some systems automatically (Mac),

# which causes manpath to ignore the values of PATH like /opt/local/bin (MacPorts).

# Also MANPATH may be deprecated. See "SEARCH PATH FOR MANUAL PAGES" in man(1)

unset MANPATH

# manpath acts differently on Solaris, FreeBSD, MacOSX & GNU. This works everywhere.

manpath >/dev/null

#

Note that MacOSX, FreeBSD & Linux have fancier ways to do some of this. (e.g. 'man --path' or 'man -q'), but this command is more universal and should work everywhere.

hash -r
2010-06-18 11:16:23
User: dpoblador
Functions: hash
Tags: PATH hash $PATH
5

i.e.: Useful if you add ~/bin to your $PATH and you want to override locations of previously ran commands and you don't want to log out and log back in to be able to use them.

function wherepath () { for DIR in `echo $PATH | tr ":" "\n" | awk '!x[$0]++ {print $0}'`; do ls ${DIR}/$1 2>/dev/null; done }
2010-04-02 20:32:36
User: mscar
Functions: awk ls tr
Tags: find locate PATH
0

The wherepath function will search all the directories in your PATH and print a unique list of locations in the order they are first found in the PATH. (PATH often has redundant entries.) It will automatically use your 'ls' alias if you have one or you can hardcode your favorite 'ls' options in the function to get a long listing or color output for example.

Alternatives:

'whereis' only searches certain fixed locations.

'which -a' searches all the directories in your path but prints duplicates.

'locate' is great but isn't installed everywhere (and it's often too verbose).

printf ${PATH//:/\\n}
ls -d $(echo ${PATH//:/ }) > /dev/null
type <command>
whereis command
which command
echo -e ${PATH//:/\\n} | less
2010-01-09 23:25:02
User: nicoulaj
Functions: echo
Tags: PATH
0

Display the $PATH with one line per entry, in a pager.

printf "%s\n" ${PATH//:/\/* }
in bash hit "tab" twice and answer y
absolute_path () { readlink -f "$1"; };
path+=( /sbin /usr/sbin /usr/local/sbin ); path=( ${(u)path} );
2009-10-31 02:32:25
User: atoponce
Tags: sudo zsh PATH sbin
0

On RHEL, Fedora and CentOS systems, and maybe others, the sbin directories aren't in the user's $PATH. For those systems that use 'sudo', this can be inconvenient typing the full path all the time. As a result, you can easily take advantage of adding the sbin directories to your PATH by adding this simple line to you .zshrc.

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.

(IFS=:;for p in $PATH; do test -d $p || echo $p; done)
2009-09-19 17:51:06
User: haivu
Functions: echo test
Tags: bash PATH
9

I often need to know of my directory in the PATH, which one DOES NOT exist. This command answers that question

* This command uses only bash's built-in commands

* The parentheses spawn a new sub shell to prevent the modification of the IFS (input field separator) variable in the current shell