Linux Commands You Need to know Part 1: Navigation

Jeremy Morgan - Apr 6 '22 - - Dev Community

The command prompt in Linux can be intimidating. It's one of the reasons many people avoid trying Linux if they come from a Windows or Mac environment. For years you had to know the command prompt very well to use Linux, so many people avoided it.

This series will tackle the mystery of the command prompt, and help you learn how to use Linux better and faster.

Popular Linux distributions like Ubuntu, Pop!_OS, Fedora, etc., are very graphical. You don't have to do much with the command prompt to use Linux. However, it's still a valuable skill to have, especially if you're developing software or websites in Linux. In this series, we will dive into Linux commands, how they are used, and why we use them.

If you can't navigate around the file system, you can't do much. So in part one of this series, we'll learn how to explore the file system from the prompt.

Here is a typical file system in Linux:

"Helpful Linux Commands"

You'll notice it looks a lot like Windows and Mac. However, if you notice at the top where it says /home/jeremy, there is a file structure there, and that's what we're going to explore. Note that in Linux, we usually call folders "directories." They're the same concept, but it can be confusing at first.

Let's dive into some well known commands for navigation.

We will learn:


1. pwd

Purpose: to show where you are located on the filesystem

This stands for "print working directory," it tells you where you're at on the file system. It's handy because it's easy to get "lost" on the file system.

usage:

pwd

When I run pwd on my machine, it shows I'm in /home/jeremy:

"Helpful Linux Commands"


2. cd

Purpose: to navigate the file system

This stands for "change directory," and it allows you to navigate.

usage

cd (name of the directory you want to go to)

Now this one has more than a few interesting options.

You can go into the next folder up by typing its name.

cd [name of directory]

So if I'm in /home/jeremy and I want to go to games I would just type in cd games.

"Helpful Linux Commands"

That's a relative path. If I want to do an absolute path, I will type in cd /home/jeremy/games.

Now I'm in that directory. What if I want to go directly to my Go bin folder? I would type in

cd /usr/local/go/bin

and you'll move directly to that folder:

"Helpful Linux Commands"

You can cd into a directory above where you are, or explicitly.

You can also go down one directory by typing

cd ..

If you want to go down two directories, you can do that too.

So let's say you're in /usr/local/go/bin and you want to go to /usr/local you would type:

cd ../..

"Helpful Linux Commands"

And there you have it! cd is a great navigation tool.


3. ls

Purpose: to list the objects in a directory

The ls command lists all the objects in a directory. So, we're in our /usr/local directory. If we type in ls we'll see this:

"Helpful Linux Commands"

This isn't super helpful. I prefer something like ls -la which shows "all" of the information about the objects:

"Helpful Linux Commands"

If you want to show what's in the directories as well, type ls -R

"Helpful Linux Commands"

If you want to search for a specific file name or extension, use the * symbol. Let's say I want to find every .toml file in my directory.

I type in ls -la *.toml. The asterisk means "everything that has .toml" at the end:

"Helpful Linux Commands"

Or, we can look for "every file with the name staging in it" by typing ls -la *staging*

"Helpful Linux Commands"

ls is a simple command that can be very powerful.


4. ~

Ok, so ~ isn't a command, but something you append to commands. It simply means "home".

if you type in cd ~ it will take you to your home directory:

"Helpful Linux Commands"

Not only that, you can create directories using ~ as a shortcut for home. You can navigate to directories under your home directory with ease.

Let's say I'm in /etc/X11/init and doing some work. But I want to get to a specific directory in my home directory.

Instead of typing out cd /home/jeremy/repos/JeremyMorganDotCom,

I can type cd ~/repos/JeremyMorganDotCom as a shortcut.

"Helpful Linux Commands"

This may seem like a small thing but can add up to big savings once you get into the habit of using it.


5. tree

Tree is a helpful command for visualizing a file system. As a note: this command doesn't come with all Linux distributions. You may need to install it. It's a great way of visualizing the directory layout in the filesystem where you're at. Here's an example in a repository of one of my projects.

To use it, you type:

tree

"Helpful Linux Commands"

I haven't used this one as much, but it can be helpful for confusing filesystem layouts.

Conclusion

Here are the typical commands for navigating in Linux. I encourage you to try them out and get comfortable with moving around. If you haven't installed Linux yet, you can still get some practice doing this. Here are some options so you can try it out on your Windows or Mac machine:

  1. Open Terminal on your Mac. It's the same commands.
  2. Install WSL (Windows Subsystem for Linux) and get a Linux installation within Windows.
  3. Use Virtualbox and create a virtual Linux installation

These are some ways you can try out Linux virtually risk-free. If you've already been bitten by the Linux bug, start expanding your skills with our Linux Fundamentals Path where you can advance your Linux skills quickly.

You can test your Linux skills to see where you're at:

"Helpful Linux Commands"

I scored a 203, so I still have some room to grow. What's your score? Take your SkillIQ now.

Have any questions? Comments? Let me know!

--Jeremy

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .