The course is part of this learning path
This course will get you up to speed with the fundamentals of Linux and prepare you for further study. We'll look at the essentials of Linux including the directory structure, basic commands, the shell, and using the command line.
Gain a foundational understanding of:
- Linux directories and permissions
- Shell scripting and the command line
- Anyone with little to no knowledge of Linux who wants to learn more about the operating system
- Professionals who want to learn about Linux to enhance their career prospects
This is a beginner-level course so there are no prerequisites, but an interest in Linux and programming knowledge in general would be beneficial.
In today's lesson, we're going to talk about some shorthand ways to access directories, how to execute commands that may live outside of your PATH environment variable, and how to create and remove directories. Directories are just containers for other files and directories.
In a previous lesson, we've already covered how directories provide a tree like structure for the file system. Directories can be accessed by their name, a full path, or a couple of shortcuts that we're going to go over next. When working on Linux systems, a period is often referred to as dot. This is no different than google.com instead of Google period com. So in Linux, a single dot represents this directory, the current directory.
Two dots represent the parent directory. You can think of the parent directory as being the directory above your current directory. The cd command provides a shortcut as well. If you type in cd space dash + enter, then you'll be placed into your previous directory. The directory separator in Linux is the forward slash. So you can think of directories as ending in slash, and often this is understood.
For example, if you cd into /home/jason, that's the same thing as running cd/home/jason/. Using directory shortcuts can make navigation easier, so let's look at a couple of examples. pwd shows our present working directory. You can type in the name of a directory to change to it. It shows that we are in that directory. Cd .. is go to the parent directory. Let's run that again, cd .. that takes us to /home. And you can end directories with a slash, so cd jason is the same as cd jason/. See what happens when we type cd . So the dot didn't take us anywhere because dot represents this directory.
Let's do this one more time, cd .. takes us to the directory above. And we can also specify a full path. For instance, cd/home/jason or cd /home/jason/. Let's change to another directory, cd /var/temp. OLDPWD, this environment variable holds the directory that you were previously in. So if we look at that it shows us that we were previously in home/jason. So if we use cd -, that takes us back to home/jason.
We've previously talked about how the environment variable of PATH determines your command search path. You can specify a command with a full path. You can execute and commands that are not in your path. And if you wanna execute a command in the current director you're in, you type dot forward slash and the command.
So our path environment variable determines where your shell will look for commands. So which tells us that if we just run cat, that it would run bin cat, and we can also specify the full path to that /bin/cat. And we get the same result. However, if we want to run a different cat, then we could do something like this.
Again, that cat will not be executed because home/jason/my-cat is not in the path environment. It's not in my command search path. Let me change directory into my-cat. So we can remember that dot is this directory, slash is a directory separator, and this is the name of the program and it executes.
So if you wanna execute something in your current directory, just remember dot slash and the name. If you want to create a directory, you can use the mkdir command. Now -p is in brackets. And when I play something in brackets, that means that it's optional. So make directory requires that you give it a directory named create. However, -p is optional. So mkdir space, new dir will create a new directory named new any wdir.
Dash p stands for parents. So if we wanna create a directory structure, that is more than just one directory deep, we need to specify dash P so that it will create the parent directories. For example, we could use make dir space dash P space one slash two slash three, that would create three directories with one command. One, that's one directory, one slash two, that is your second depth through your second directory, one slash two slash three, your third directory.
When you want to remove a directory, use the rmdir command. The rmdir command only removes directories that are empty. They have no file contents. So if you have a directory that you want to remove it and all the files you need to use rm-rf, that directory name. And that recursively deletes everything in that directory and below that directory.
One important thing to note, when you're working at the command line in Linux, there is no undo. There is no trash can that you can go and retrieve a file that you've deleted, when you delete something it's gone. When you recursively delete something, all of those files and directories that you recursively deleted are gone. You can't get them back. So just be careful when you're using rm-rf.
Let's create a directory. Let's try this again. We can see that maker command will not create the third directory because it's parent dir2 does not yet exist. So if we want mkdir to do this, we'll need to specify -p, for parents so that it will create the dir2 directory. And that worked. Again, the rmdir command only removes empty directories. So dir1 contains dir2 and three.
If we want to recursively remove them we can use rm-rf dir1. And now that we'll delete dir1. And if there were any files in that directory, they would be gone. In this case there's only one directory there, so dir2 will be gone. And then all the files and directories that are in there. Again, this deletes everything recursively. So anything at dir1 and below will be gone and you can't get it back.
Today we've talked about some directory shortcuts. How dot represents this directory, and dot dot represents a parent directory. How cd - will take you to the previous directory that you were in. We talked about how to execute commands that may not live in your search PATH. We can use a full path, or you can use dot slash and the name of the command to run a command that is in the current directory that you're in.
We also talked about how to create and remove directories. Practice navigating with directory shortcuts like cd .. to take you to the parent directory and cd -, to take you back to the directory that you were previously in. Also execute some commands with full pass, like /bin/cat to execute cat. Also work on creating and removing directories and using rm- rf to recursively removed directories.
Jason is the founder of the Linux Training Academy as well as the author of "Linux for Beginners" and "Command Line Kung Fu." He has over 20 years of professional Linux experience, having worked for industry leaders such as Hewlett-Packard, Xerox, UPS, FireEye, and Amazon.com. Nothing gives him more satisfaction than knowing he has helped thousands of IT professionals level up their careers through his many books and courses.