From the course: Learning Zsh

Autocd and cd shorthand - Unix Tutorial

From the course: Learning Zsh

Start my 1-month free trial

Autocd and cd shorthand

- [Narrator] As we take a look at using different features of Z-Shell, I'll quickly go over some of the shell basics we'll need to understand those features. If you need a more in-depth look at how to use the command line in general, take a look at my course Learning Linux Command Line, here on LinkedIn Learning. For the most part, while you're navigating around the file system, you'll be using commands provided by a set of tools available on the system, not specific features of any given shell. A Z-Shell provides some helpful built-ins, though, so we'll take a look at those as they come up. An important idea in browsing around the file system, is the concept of the working directory, and that's the directory that you're working or operating inside of. The first place the shell will look for commands you type, and the location where, if you don't specify a place, files will be created, deleted, or modified in response to a command. We can print out the working directory with the pdw built-in. I'm inside my users home directory. I can see that from the path here, starting with a / representing the route of the file system, the highest level, then the home directory, and then my users home directory. The primary tool we'll use to navigate is cd, the built-in command to change directory. And this is often paired up with the ls command, to list files and directories on the system. I can see what's inside this working directory with ls, and I can see what's inside the documents directory with ls documents. I can move into that directory with cd Documents, using the relative path. Or cd/home/scott/documents using the absolute path. And now that's my working directory. I have a few ways of moving back to my home directory. I could write cd/home/my user name, using the absolute path. Or I could write cd .. Using the two dots shortcut that represents the parent directory. I could also just write cd, and the assumption is that if you don't give cd a path, you want to move back to your user's home directory. I could also write cd - which moves to whatever the previous directory was. That's different than the parent directory, basically it lets you switch between two directories easily without having to type the path if you need to move back and forth. Z-Shell provides some helpful features that make navigating a little bit easier. One of these is called autocd, and it saves you a few keystrokes by letting you type and change to a directory name without the cd command if that directory exists. Autocd is an optional feature, so I can turn it on with setopt autocd. And then let's take a look at the directories I have here. To move into my documents directory, now I can just type Documents, and then I can see that I'm inside that directory, without having to type cd. This works for other directories that exist on the file system as well. I'll type /etc and now I can see that I'm in the etc directory inside the file system root. That can be useful if you move around within directories a lot. I'll turn it back off here for now though, and I'll go back to my home directory with cd. I can also move between directories at the same level using a shorthand. So let's make two directories. I'll write mkdir folder01 and folder02. And then I'll move into folder01. Now to move to the directory called folder02, I could either write cd ../folder02 using the relative path, or I could just type cd 01 02, relying on the shell to figure out that I want to move from folder01 to folder02, both of which are at the same level. Here, I can see the path that the shell assumed, and I can see that I've moved into that directory. But for now I go back to my home directory with cd.

Contents