Command-line Basics: Changing Directories

joshtronic

Love or hate the command-line, you’ve probably used it at some point in your life. Even if you’re not a CLI-ficionado, there’s a great chance you’ve used the cd command at one point. Checking into my own command-line history, turns out that 13% of the commands I’ve run have been cd. I’d say that makes it one of the most important tools in my command-line toolbox. Ready to take a deeper look?

Changing Absolutely

The cd command can take an argument telling it which directory you’d like to move to. One way you can do so is by telling it the absolute path you’d like to go to.

An absolute path is just that, it’s absolute and relative only to the root of the file system. Absolute paths start with / and tend to be longer than relative paths since you have to specify the entire tree:

cd /home/gator/one/two/three/four/five

There’s not a ton of guess work going on here, just the exact path you’re looking to change to.

Relative Changes

Similar to absolute paths, relative paths are relative to your current location which may or may not be the root directory on the file system.

Taking the previous example, if you were already in the home directory for the gator user, you could abbreviate part of the path with a single .:

cd ./one/two/three/four/five

The . tells cd (and other commands that use the file system) that you’d like to look for the directory starting with the pwd (present working directory).

Traversing the Tree

Similar to the . syntax, you can also use .. to tell cd that you’d like to move up a directory.

Building on the previous example, if you’re in the five directory, you can move to the four directory with ..:

cd ..

And from the four directory to the two directory with even more dots:

cd ../..

You can chain as many ../ together as you’d like assuming the directory you’re attempting to change to exists.

Homeward Bound

Based on what we know right now, if we wanted to travel from the five directory back to /home/gator we could chain together a bunch of ../:

cd ../../../../../

Easy enough, but that’s a lot of dots to keep track of. Fortunately the home directory can be referenced by using the tilde character:

cd ~

This tells the cd command to jump back to the current user’s home directory. That means if you’re currently using sudo or su you may not end up where you’d like to end up.

If you’re having an identity crisis, you can always run whoami to find out which user you’re currently logged in as.

The ~ can also be chained together with more directories relative to your user’s home directory:

cd ~/one/two/three

Or even chained together with ..:

cd ~/../anotheruser

Go back from Whence You Came

Another alias that you can use with cd is the dash. The - character tells cd to take you back to your previous working directory.

Let’s say we started in /home/gator/one/two/three/four/five and then changed directories to /tmp/some/crazy/long/path/that/isnt/relative/to/my/home:

cd /tmp/some/crazy/long/path/that/isnt/relative/to/my/home

Since this crazy long path isn’t relative to our home directory, we’d either have to type out an whole lot of periods, or leverage the ~ and specifying the rest of the path relative to the home directory.

Or we can just use the -:

cd -

And like magic, we’re transported to our old present working directory.

I find this one to be super handy when I need to jump half way across the file system and come right back without opening a new terminal.

The command cd - is the equivalent of cd "$OLDPWD", which means if you ever wanted to know the last directory you were in, you can simply echo "$OLDPWD" to find out.

Saving Keystrokes

Typing out all of these path names is great and all, but it’s not terribly efficient. Depending on your terminal configuration you probably have some sort of command completion available.

If that’s the case, all you need to do is type the first character or so of the path, hit <TAB>, and like magic, that part of the path will be expanded / completed:

cd ~/o<TAB>
cd ~/one/t<TAB>
cd ~/one/two/t<TAB>
cd ~/one/two/three/f<TAB>
cd ~/one/two/three/four/f<TAB>

In instances where you have multiple directories with the same first letter, you may be presented with a list of available completion options, or forced to enter in another character and hit <TAB> again to get things moving.

Again, YMMV based on the shell completion options that are configured.

Listing Files After Changing Directory

As somebody that lives on the command-line, I’m always trying to improve my workflows and routines. Every keystroke saved is another keystroke that can be spent on writing code and creating awesome experiences.

A while back I started to realize that my usage of cd was usually followed by an immediate ls command to see what the heck was going on in the directory I just changed to.

This got me thinking, “wouldn’t it be nice if I could automatically run ls after entering a new directory”?

Turns out it’s really nice and also really simple.

If you were to add the following function to your shell profile (for zsh it’s .zshrc and if you use bash it’s .bashrc) and reload your terminal, every time you use cd the ls command will be run immediately after:

function cd {
  builtin cd "$@" && ls
}

It’s probably my favorite command-line hack.

Easier Said Than Done

The cd command comes across as a pretty simple command. You tell it where you want to go and assuming the destination exists, you’ll end up there.

Between you and me, while one of my most regularly used commands, I wouldn’t even consider it all that powerful.

Like many things that seem simple, the cd command is actually pretty crazy internally. The simple act of changing a directory fires off a series of conditionals to make sure that you’re transported to the right place.

Taking a look at the man page on my Arch Linux system, cd runs through 10 different operations and somehow does it all with great efficiency.

Speaking of man pages, remember that on the command-line you always learn more about your favorite commands by typing in man <command>.

  Tweet It

🕵 Search Results

🔎 Searching...

Sponsored by #native_company# — Learn More
#native_title# #native_desc#
#native_cta#