How to Make Your ‘CD’s Less Painful

You know about the PATH environment variable. Did you know about CDPATH? If you’ve been typing long cd commands like me, this post is for you.

I came across this gem this morning, while reading Daniel Barrett’s new book Efficient Linux at the Command…


This content originally appeared on DEV Community and was authored by Akshay Khot

You know about the PATH environment variable. Did you know about CDPATH? If you've been typing long cd commands like me, this post is for you.

I came across this gem this morning, while reading Daniel Barrett's new book Efficient Linux at the Command Line. It instructs the cd command to search for the directory you specify in locations other than your current directory.

A cd search path works like your command search path $PATH. However, instead of finding commands, it finds the subdirectories. You can configure it with the shell variable CDPATH, and it has the same format as the PATH variable: a list of directories separated by colons.

Set this variable in your ~/.zshconfig or ~/.bashrc file to include the directories you visit most frequently.

export CDPATH=$HOME:$HOME/software:$HOME/software/ruby:$HOME/software/rails:$HOME/software/youtube

Now, whenever you want to cd into a directory, the shell will look through all the above places in addition to the current directory.

What's more, the search is lightning fast. It looks only in parent directories that you specify and nothing else.

For example, let's say you have a $HOME/software/blog directory and you've configured the CDPATH to include the $HOME/software directory.

Now, if you type cd blog from anywhere in the filesystem, the cd command will take you to the $HOME/software/blog directory, unless it finds another blog directory in another pre-configured path. So the order of CDPATH matters. If two directories in $CDPATH have a subdirectory named blog, the earlier parent wins.

In the above example, cd will check the existence of the following directories in order, until it finds one or it fails.

  1. in the current directory
  2. $HOME/software/blog
  3. $HOME/software/ruby/blog
  4. $HOME/software/rails/blog
  5. $HOME/software/youtube/blog

To summarize,

Set CDPATH with your most important or frequently used parent directories, and you can directly cd into them or their subdirectories no matter where you are in the file system. You don't have to type the full path anymore.

Trust me, this is pretty freaking cool. It has changed the way I use the shell.


This content originally appeared on DEV Community and was authored by Akshay Khot


Print Share Comment Cite Upload Translate Updates
APA

Akshay Khot | Sciencx (2022-06-07T00:16:56+00:00) How to Make Your ‘CD’s Less Painful. Retrieved from https://www.scien.cx/2022/06/07/how-to-make-your-cds-less-painful/

MLA
" » How to Make Your ‘CD’s Less Painful." Akshay Khot | Sciencx - Tuesday June 7, 2022, https://www.scien.cx/2022/06/07/how-to-make-your-cds-less-painful/
HARVARD
Akshay Khot | Sciencx Tuesday June 7, 2022 » How to Make Your ‘CD’s Less Painful., viewed ,<https://www.scien.cx/2022/06/07/how-to-make-your-cds-less-painful/>
VANCOUVER
Akshay Khot | Sciencx - » How to Make Your ‘CD’s Less Painful. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/06/07/how-to-make-your-cds-less-painful/
CHICAGO
" » How to Make Your ‘CD’s Less Painful." Akshay Khot | Sciencx - Accessed . https://www.scien.cx/2022/06/07/how-to-make-your-cds-less-painful/
IEEE
" » How to Make Your ‘CD’s Less Painful." Akshay Khot | Sciencx [Online]. Available: https://www.scien.cx/2022/06/07/how-to-make-your-cds-less-painful/. [Accessed: ]
rf:citation
» How to Make Your ‘CD’s Less Painful | Akshay Khot | Sciencx | https://www.scien.cx/2022/06/07/how-to-make-your-cds-less-painful/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.