Commands Involving Directories
Hopefully, you have grasped somewhat of an understanding on how commands for files work. They will facilitate working with files and help you in the long run if you know the proper way to use them. However, you will not only be working with files on your computer. Often times, you will have to organize them in folders. This is where directories come in. In this section of the web site, you will learn how to work with directories using various commands. It's better to get used to them now because they will make directories your best friend.
As I mentioned previously, there are commands that involve working with directories. While some commands used for files can work for directories interchangeably, a few commands are exclusive only for directories. After all, they are a crucial part of storage of files within a computer system. Computer users work with directories all the time, whether it involves saving a file in a folder, creating a folder on a desktop, or deleting files. As society evolves and computer technology continues to advance, it will be important for you to understand how to work with directories, as well as files. For Mac users, Unix is the perfect tool to facilitate that need. Let's take a look at the commands used to work with directories.
The cd Command: Choosing a Different Path
The cd command can be one of the most reliable commands that you will ever use in Unix. It enables you to change directories whenever you desire. This definitely helps when you want to work and create files in a certain directory. Using the cd command is like taking a taxicab in the world of the terminal because of the fact that it takes you wherever you direct the command toward. For example, if you were currently working in a directory called my_folder and wanted to switch to my_other_folder, you would simply use this following syntax: cd my_other_folder. This will take you from my_folder to my_other_folder as a result. You can use the command pwd and ls to verify where you are.
Using the cd Command With Absolute and Relative Path Names
Furthermore, it is imperative that you know the concepts of relative and absolute paths in order to effectively use the cd command. This conveys the fact that you cannot always simply input the command followed by the directory name. That will only work in certain cases, but not all of them. Due to this fact, we will discuss relative and absolute paths, as they are related to using cd.
When logging into Unix, you begin in your home directory, which you can think of as your default setting. If you want to change directories, you can do this in one of two ways: using an absolute or relative path name. An absolute path starts from the root directory to the chosen directory. The path name of an absolute path always begins with a slash (/). The slash character signifies the root directory and is used when naming an absolute path. Here's an example of the use of the cd command followed by an absolute path name:
- cd /user/Desktop/my_folder/my_subfolder
A relative path begins with your current directory and ends with your chosen directory. In other words, relative paths relate to your current directory. For instance, suppose you wanted to change directories from my_folder to my_subfolder. You would use this syntax to properly switch directories: cd my_folder/my_subfolder. This implies that you are starting from the directory you are currently in and ending in your chosen folder. But what if you wanted to go to my_folder from my_subfolder? This is where it gets a little tricky. You use two dots followed by a slash to move up one folder (../). This is how it looks in the command input: cd ../my_folder. To move up two folders, you use two sequences of a pair of dots followed by a slash (../../).
The pwd Command: Tell Me Where I Am
As I mentioned on the ls page, the ls command tells you what files you have in your working directory, giving you a list of the items. This command will also help you confirm that you have a certain file in your directory. You might be wondering why I'm mentioning the ls command on this page. This is because I'm introducing you to its equivalent command, but this command involves working with directories. The pwd command actually outputs the directory that you are currently working in. In fact, pwd stands for print working directory. This command can especially help when you forget what directory you're in or when you need to confirm that you changed to the right directory. The syntax for this command is really simple: just type the command pwd and it should work correctly.
Commands to Use When Lost
Like I said before, pwd helps when you forget where you are. This is beneficial due to the fact that it can prevent you from creating or saving a file in the wrong place. My advice to you is to use this command often because it can actually be your guide. If you use this along with the ls command, you'll know where you are and what contents you're working with. Even if you use pwd and discover that you're in the wrong directory, you can always travel to the right one using the cd command discussed above.
The mkdir Command: The Making of Directories
Directories are very important in the computer world because they are used to store and organize multiple files. Thy are like computer versions of storage rooms, which contains all sorts of things, such as word documents, HTML documents, pictures, and many more. Therefore, it is vital that Unix has a command that creates them. With the mkdir command, you can make as many directories as you want. Think of the mkdir command as an abbreviation of two words put together, which includes make directory. This may help you remember the name of the command for future references in case you forget. Now that you know there is a command that makes directories, it's time to put what you learned into practice. Look below at the syntax and applied example of the mkdir command:
- mkdir directoryName
- mkdir myFolder
By using the mkdir command, you will have the ability to organize files however you see fit. One scenario includes building a website. Suppose you want to create a web site but need to store all of your images, HTML documents, and CSS files. You can create a main folder that will hold all of your files for organizational purposes. Moreover, you can create sub-folders inside of your main folder in order to further group your files. For instance, you can create a sub-folder that will hold your images and another sub-folder that will store your CSS stylesheet. This can all be done using mkdir
The rmdir Command: Removing Directories
If you read the mkdir command page, you have already discovered that there is a way to make directories straight from the terminal. Thus, this command serves to be useful because it helps you to create directories when you need to organize files. But what if you have a directory that you do not need or that you accidentally created? This is where the rmdir command may accomodate. The rmdir command deletes the directory that you specify. One easy way to help you remember this command is that rmdir can be viewed as an abbreviation of the phrase remove directory. Again, this may sound a bit cheesy, but it helps to know commands like these, especially when you're trying to organize everything on your computer.
There are two very significant commands that you must keep in mind when working with directories. If you viewed the Working With Files section and looked at all the commands there, you most likely already saw the mkdir command, which makes directories when called upon. Using this and the rmdir command will most definitely help you organize your content on your computer. Just remember to use mkdir to make directories and use rmdir to remove directories from your computer if you have no use for them anymore. Removing any unnecessary content will not only help you retain organization, but it will also give you more space to store any recent files or directories that you may find useful.