Today you’ll learn folder navigation with cd and ls in the Linux command line
in five minutes or less.
To follow along with me you’ll need a Linux virtual machine ready to go.
If you don't have one, no worries, watch my tutorial on building an Ubuntu VM with Virtualbox,
link in the description below.
Let’s get started!
First open up a terminal by searching it in the application list.
To get oriented with which folder you’re currently in, type pwd.
Pwd stands for print working directory.
We’re in the user’s home folder, which is shown by the tilde symbol.
Tilde is a shortcut that always refers to the current user’s home directory.
This section in blue will also show the path of whatever folder you’re currently in.
The dollar sign prompt means the shell is ready to accept your typed commands.
You can type --help after almost any command to get usage info about it.
In Bash, which is the default command line shell for most Linux distributions
the structure goes like this:
you first have a command, with one or more flags that modify the command’s
behavior, followed by one or more target files.
Depending on the program, you can use individual flags or group them with a single hyphen.
Sometimes you’ll see the structure shown like this.
Let’s go over the ls, or list command now.
Ls is very similar to expanding a folder in tree view in the file manager to see what’s
inside without actually entering that folder.
There’s many flags that change the output of this command.
ls -1 shows one item per line.
ls -l shows detailed permissions, file sizes, number of items, and timestamps.
ls -a shows all files, including hidden ones which begin with a dot.
If your screen is getting full of text, type clear, or CTRL-L to clear it.
You can chain flags together, such as ls -1a, to combine the behaviors of both.
Passing multiple folders, such as both Desktop and Documents
will show the contents of them all.
The possibilities go on and on, which you can further learn
by the reading the system manuals.
I’ll type man ls to see it for this command.
To navigate through the man pages, use the arrow keys to scroll up and down one line
at a time, or page down and page up to scroll one page at a time.
To search for text, type forward slash followed by the search query.
This capital R flag says it lists subdirectories recursively.
Let’s open up a new tab with Ctrl-Shift-T to check it out.
As you can see, ls -R gives us all the subfolders and files in the directory we’re in.
I’ll type exit to close this shell.
To go back to the beginning of the man page, Hit 1 capital G
to jump to line one of the manual.
If I search for a word with multiple occurrences, it’ll highlight them all, and you can type
n to search forward, and capital N, holding Shift, to search back.
Type q to quit.
If I wanna list the contents of another directory in the file manager, I’d have to click through
several folders to get there in either tree or tile view.
In the command line, you can pass ls a relative path and use tab to autocomplete the command.
Autocompletion needs enough characters to differentiate between similar names.
Hitting tab a couple of times with just the letter “D” will suggest folders beginning
with that letter.
Feeding it more characters narrows it down to the one that you want.
You can also pass ls an absolute path to a directory
which works no matter which folder you’re in.
As you can see I’ve got a Python script on the Desktop called generate.py.
If there’s a file or folder with a long name, you can drag it from the file manager
into the terminal to save some typing.
Note the spaces in this folder name.
If you type it as is, ls will think you’re trying to show three separate folders:
Perfect, Life, and Formula, which don’t exist.
Use up arrow to go back and escape the space characters with backslashes, or by using quotes,
but never both.
To go to a different folder, use cd, or the change directory command with tab completion
get where you want.
Notice that when I’m in a directory, you can use ls by itself.
To jump back to the folder you just came from, type cd hyphen.
What we just did is similar to entering this folder in a file manager, and then clicking
the back button.
If I typed, cd hyphen again, I’d wind up in the original folder again.
Now typing cd by itself will take you back to your home directory no matter where you are
which is the same as typing cd tilde.
If you wanna go up a directory, type cd dot dot.
Dot dot is a special folder that points back to its parent.
There’s also a special folder called dot that points to the current folder you’re in
and doesn't change your location.
We can see them using the -a flag with ls.
You may wonder what the use of a single dot folder is, but that’s something I’ll show
you in another video.
So that’s just about everything you need to know
to navigate the command line like a pro.
Hit that like button if you enjoyed this video, subscribe if you aren’t already, and let
me know what Linux commands you'd like to learn in the comments below.
See you soon.