Join Kevin Skoglund for an in-depth discussion in this video Moving around the filesystem, part of Unix for Mac OS X Users.
- View Offline
Over the last couple of movies we saw how we can use pwd to see our present working directory and ls either with or without options to see the files and folders that are in the present working directory. Now I want us to talk about how we can move around and change our working directory and to do that we use the cd command. So cd followed by the name of the directory that we want to change into. So for example library. Now our present working directory is changed. We are now inside the library. It's the same thing as if we go over here and we double-click on library.
Now we have changed into the library. We can also hit the back arrow and change back. Here, do the same thing from the command line. We would type cd space and then dot, dot. Remember that I told you that I told you that dot, dot when we are seeing this listing is special file name. That references the parent of the current directory. dot is the current directory. dot, dot is the parent of this directory. You can see here that it actually says d in front of that. It treats it just like a directory and we can change into it. So it says, "I don't know what the name of the parent is, I don't care, I want to go backwards one." It's the same as hitting that back arrow.
So now we are back our back in original directory. We also saw that these are file separators, the forward slash. We can use those to be able to move several directories at once. So cd Library/preferences will move us into the preferences folder. Now our pwd is into the library preferences, we moved two directories at once. It's very important that they start with library and not a slash in front of it. Not like this and I will explain that in a moment. We can also go backwards the same way cd../..
Go to the parent of this directory and then to the parent of that directory. So we've now moved back two directories after we went in two directories. I also want to talk about a shortcut which is for auto completing file names. So if we say cd space and then just type an L and then hit the Tab key, you can see that it says, "Oh, I looked at the list of available files and folders in the current working directory and from that the only one I saw that starts with an L is Library, so that's probably what you mean" and it went ahead and filled it out for us. If it can't guess it, let's say we start typing Pref and then we hit Tab, it says Preference. Well actually no, we are looking for Preferences.
It didn't put the full name. Hit Tab a second time and it comes up and says "Oh, I am trying to decide between Preference panes and Preferences, those are the two that I can't distinguish," so got to give me a little more to go on. So it finishes as much as it can and then it stops and waits for you to do it. So library preference on its own, if I hit Return, it says no such file or directory. Preferences that directory does exist. So I just want to make sure that you see how auto complete works, because that's a really helpful thing when you are moving around to be able to start typing a name and hit Tab and have it finish the rest of the directory or file name for you.
Now I mentioned earlier that you did not want to put this beginning slash in front of it. That's because whenever something has a beginning slash it's an absolute path. That root cd/ that represents the very beginning of our hard drive. That's the root of the hard drive. cd/, now we are through to our hard drive. If we do cd users and then Kevin, we move forward back into users in Kevin. That's a relative path, relative from the root where we were, move into users and then into Kevin, but cd/users/kevin, that is an absolutely path that says "All right, I am going to give you the full path." So the one without the slash is starting with the working directory, I am going to give you the path, the one with the slash says, "I am going to give you the entire path starting from the root of the hard drive." You are not going to use anything relative to where you are now. It doesn't matter where you are. This path is the same if I am inside preferences or if I am inside library. cd /user/ Kevin is the same directory.
That user home directory we keep moving into, right. We are going to do that a lot. That's our directory that's where lot of our files and folders are located. So there is actually a shortcut for it, cd, and then the twiddle or the Tilde key and that will take us into our user directory. So if I move into the preferences and then cd back, it takes me back to my user directory. There is one other thing that's good to know which is cd space and then just dash takes you back to the last directory where you were.
So it just toggles between the current directory and the most recent directory. So cd- now I am back in my Preferences folder. cd- again now I am back in my users folder. So that's helpful if you need to just move between two directories. You want to switch to a directory, do a few things, and then switch back to where you were. That can be a really helpful tool. Now if you do any other cds while you are in there, then suddenly your directory will change and you won't be able to get back to where you were anymore, but if you just need to toggle between two, that cd- can be really helpful.
- Moving around the file system
- Creating and reading files
- Copying, moving, renaming, and deleting files and directories
- Creating hard links and symbolic links
- Understanding user identity, file ownership, and sudo
- Setting file permissions with alpha and octal notation
- Changing the PATH variable
- Using the command history
- Directing input and output
- Configuring the Unix working environment
- Searching and replacing using grep and regular expressions
- Manipulating text with tr, sed, and cut
- Integrating with the Finder, Spotlight, and AppleScript