Linux Basics: How to Navigate the Terminal

The Linux command-line is the weapon of choice for any would-be hacker. This article will teach you the fundamentals of navigating the Linux file system

Linux Basics: How to Navigate the Terminal
Photo by Gabriel Heinzer / Unsplash

Contents


  1. Introduction
  2. First Steps...
  3. Changing Directories
  4. Raise Some Flags!
  5. Help! I'm Lost!
  6. Bonus Command!
  7. Wrap Up

Introduction


Alright, so you want to start your journey as a hacker-in-training and you hear all the experts scream "Learn Linux!". So you took some time to finally install that virtual image, or dual boot off a USB stick, or whatever! The point is your finally all set up to get started.

You boot up your computer, open the command-line and you're met with this:

Yes, I am using a MAC, but, terminal-wise, it's effectively the same thing. Don't judge me...

A blank terminal, coldly starring back at you, daring you to type something...

If you're wondering where to go from here. Well, fret not, because you're in the right place!

By the end of this tutorial your going to feel A LOT more comfortable navigating your way through the Linux Filesystem using just a handful of commands. They're quite intuitive so learning these should be a snap!

I'll even do you a special favour and add a bonus command near the end to make things even easier!

The commands you'll be using are as follows.

  • cd - Change directory
  • ls - List (current) directory
  • pwd - Print working directory
  • clear - clear the terminal
  • man - Looks at the manual page of a command

Oh, and if you're wondering why it says "bash" - or, perhaps, you're seeing "sh" - that's simply referring to the shell (and the version) that is being used to interpret your commands.

Click here for if you want to know more about shells.

First Steps...


Linux is typically made up of files and directories (folders). When you open a terminal, you are placed in the "home" directory. We'll discuss more about the home directory when we cover linux user administration in a future post.

If you want to know which directory your currently in then you can use pwd

pwd stands for 'Print Working Directory'

pwd output

The output shows that I'm in the 'Linux_User' directory, which is inside the 'Users' directory.

If you want to see the contents of the home directory - or any directory for that matter - simply type ls and hit enter.

ls output

The terminal will spit out a bunch of seemingly random words, but all it's doing is 'listing' what's inside the directory you're currently in. In my case, all of these are just other directories inside my home directory.

Changing Directories


There are A LOT of commands that you can learn. The bests ones, in my opinion, are intuitive and read like natural language. You can simply look at what you wrote and read it off like pseudo-english.

The clear and cd commands is a good example. If you want to clear the contents of your terminal, just type clear and hit enter. Great! It says what it does on the tin.

If want to go into the Scripts directory I would simply type cd Scripts. You're telling the terminal "Change into the Script directory" or "Change (current) directory into Scripts".

cd into Scripts directory and ls to see a bunch of script files

Personally, I find commands that I can 'translate' into a sentence in English far easier to remember. This will help you become a walking encyclopedia of Linux commands!

Scripts is a subdirectory of my home directory. If you want to go back up a level to the previous directory then type cd ..

.. is an alias for the directory that is the next level up. You can chain these together: cd ../.. this will take you two levels up.

Raise Some Flags!


If you're anything like me, then you prefer you command-line outputs to be a little more... organised. Lucky, there's a much nicer way to enjoy the contents of your directory!

Allow me to introduce to you the concept of flags. Flags are essentially command modifiers that will induce different effect (this is starting to sound like a tutorial in an RPG).

Each command has its own set of unique flags that modify the output. For the ls command we'll start with the -l flag.

Simply type ls -l and hit enter.

ls -l output

OK, now we've been given a lot more information to work with! The -l flag outputs the information in the 'Long Format'. Basically, just gives more information, but at this stage, we only care about the fact that it displays the contents in a nice, single, alphabetical list.

There's too many flags to cover, but if you want to know what flags are available for a particular command then you can use the man command.

This command lets you view the manual of whatever command you type after it. It's also a great way to learn more about commands in general.

Usage: man ls

man ls reveals a scrollable page with information on the ls command

You can navigate the manual by scrolling or using the down arrow key. To exit press the q key.

As you can see there's quite a lot of information plus an extensive list of flags to work with. I recommend a quick skim to see if there are any flags that peak your interest.

As your skills increase you will undoubtably build your own 'style' of using the command-line. Personally, I like to the combination of -l -r -t flags with ls.

If you're curious about what -r -t do, then I implore you to read the manual page. Consider it to be your homework!

HINT

In most cases you can combine flags instead of typing them out individually

ls -l -r -t = ls -lrt

Both do exactly the same thing!

ls -lrt -> "List the contents of the current directory in long format, in the reverse order of the time the contents were used."

Help! I'm Lost!


If, for whatever reason, you find yourself lost in the filesystem, then there are a few tricks you can employ.

We've already covered pwd and how it can tell you what directory you're in.

But if you want to go back to your home directory, then you've got two options.

  • Close and reopen the terminal, it will automatically open in the home directory
  • Use cd ~

The ~ (tilde) is an alias for the home directory,  it doesn't matter where you are in the file system, you can alway us cd ~ to go back home.

Bonus Command!


As promised, here's a neat little command that you can use to display the contents of your directory on multiple levels: tree

You may have to install it before you're able to use it. I won't go through installation because it's pretty straightforward, simply google "Install tree on..." followed by whatever Linux distro your using and you should be able to find it relatively easily.

Great way to check if you've installed it is by using the man command. If you see a manual page then you have it on your system.

tree allows you to view, not only whats in your currently directory, but also what's in the directories inside your current directory and displays it in a nice tree-like structure.

If I roll back into my Scripts directory for a second, create a bunch of new directories and enter the tree command this is what I see:

tree output

Now if I go into dir1 and dir2, add some more stuff, and enter tree again

tree output showing multiple levels

Now I see multiples levels. dir1 has a subdirectory, subdir1, which contains a single file, file1.

It's a much nicer way to visualise your file structure. The drawback being if you have a huge, complex file structure then your terminal can be overwhelmed with information.

There is a way to limit the levels that are displayed and it's your homework to figure out how!

HINT

Try the manual :)

Wrap Up


You should now have a fundamental understanding of navigating using a command-line. To quickly summarise:

  • cd - Change directory
  • ls - List (current) directory
  • pwd - Print working directory
  • clear - clear the terminal
  • man - Looks at the manual page of a command
  • tree - visualise your file structure
  • Induce various effects using flags

The more you practice, the easier it will be to commit commands to memory, and remember to 'read' the commands and see if you can make a sentence out of it.

Hopefully, the terminal is now a little less daunting. Remember that this is only the beginning, there is still a metric ton of commands that you can learn, and even combine commands using scripts, pipes, redirects, etc...

But those are for future posts. Hope you're excited to learn more!