What’s up guys, in this video I'm gonna
take you through command line folder operations
with mkdir, rmdir, pushd, and dirs. They go
hand-in-hand with the file operation commands
that we went over in the last video and help
make you more independent of a GUI file manager.
Let’s go! Mkdir stands for make directory
and lets you create a new folder. I’m gonna
create a Projects folder in the home directory.
This is pretty much the same thing as right-clicking
in a file manager and doing the same thing.
Mkdir has a couple of advantages though, in
that I can create multiple folders in one
command without having to navigate to that
directory first. I’m gonna make three projects
folder on the desktop like this. Btw, bash
has a way to expand expressions using the
squiggly brackets. I could have done the same
thing as above by typing 1..3 between the
brackets to save some typing. Since these
folders already exist, let me try 4..6 instead.
To have verbose output showing the folders
being created, use the -v flag. I'll make
fourteen more folders, with the first six
being skipped. In the file manager, you might’ve
had to click a ton of times just to do the
same thing! Now if you want to remove these
folders, we can use the rmdir command, which
only works on empty folders. I can type rmdir
Project* to to delete everything beginning
with the word Project, or use our bash expansion
brackets to just do a subset of them. This
is sometimes more precise and faster than
drag-selecting in the file manager. Both mkdir
and rmdir have a -p flag, that creates parent
directories for you if they don’t already
exist. For example, if I type this command,
it’ll fail because none of the parent folders
exist. Adding the -p for parent flag will
create the nested directories as needed. You
can see them in the file manager here. Rmdir
with the -p flag lets you do the same thing
and remove all the parent directories of a
folder, as long as they’re empty. I’m
gonna add the verbose flag to show some output
feedback. Now, if any of the parent directories
contain files, the command will stop at that
folder level. Let me run the mkdir command
again on this nested path, but now try to
remove it with my home and Desktop folders
at the front too. As you can see, it stopped
at the Desktop level because it isn’t empty.
With all these nested folders, let me show
you a cool way to keep track of all the directories
you've been in to navigate around easier.
Pushd is a command that pushes a directory
to the top a stack that you can access later.
Let’s say I recreate my long nest of folders
and navigate to it. I can use pushd to save
where I am. Dot means the current directory.
Let’s also cd to the Tutorials folder which
is two levels up and save it. Now let me cd
to my home directory. To show a list of saved
folders, type dirs -v to see what we saved.
Index 0 always refers to the current directory
you’re in. Index 1 is the most recent folder
you've pushed, kind of like a stack of plates,
where the most recent one is on top. If you
type popd, bash will navigate you to whatever
folder occupies index 1 and remove it from
the stack. This can be kind of confusing and
tiresome to mentally track which folder is
at the top, so I prefer to use cd instead.
If you type ~ followed by the index number,
cd will jump you directly to the folder at
that number. This is incredibly useful if
you’re doing many things across multiple
folders and don't wanna type long path names
all the time. Whenever you wanna save the
folder you're in, just use pushd dot to save.
When you navigate somewhere else, run dirs
to see the stack, and cd ~ and an index number
to jump right to it! So that's it for folder
operations in the command line. Spend some
time everyday creating multiple nested folders,
removing them, and navigating around, and
you’ll be fully comfortable in no time!
If you found this Linux tutorial helpful,
hit that like button, subscribe if you aren’t
already, and let me know what commands you'd
like to learn in the comments below. See you
soon!