Using CD, LS Navigation Commands in Linux – Clean Tutorial

Page Contents

Introduction to Navigation Commands in Linux

With this tutorial, you’ll learn folder navigation with navigation commands in Linux (cd and ls) command line. To follow along with me you’ll need a Linux virtual machine ready to go. If you don’t have one, no worries, search for any tutorial on building an Ubuntu VM with Virtualbox. Before going to navigation commands in Linux you need to know the basics about Linux Terminal

Getting Started with Terminal

First, open up the 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”.

Using PWD command - CD, LS Navigation Commands in Linux - Command Line Tutorial
Using PWD command

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.

Bash Command Structure

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.

Bash Command Structure

command -a -b -c <targets>
command -abc <targets>

command [OPTIONS]… [FILES]…

LS (List Command)

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 are many flags that change the output of this navigation commands in Linux.

  • 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 that begin with a dot
Basic LS commands - CD, LS Navigation Commands in Linux - Command Line Tutorial
Basic LS commands

If your screen is getting full of text, type clear, or press 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.

System Manual for LS Command

The possibilities go on and on, which you can further learn by the reading the system manuals. You can type man ls to see it for this navigation commands in Linux.

Navigate through the Manual page

To navigate through the manual 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.

This “R flag” says it lists subdirectories recursively.

-R flag

Let’s open up a new tab with Ctrl+Shift+T to check it out. You can see, ls -R gives us all the subfolders and files in the directory we’re in. Now type exit to close this shell.

Executing ls -R

To go back to the beginning of the manual page, type 1G to jump to line one of the manual.

How to search for a text

To search for text, type forward-slash ( / ) followed by the search query. If you search for a word with multiple occurrences, it’ll highlight them all. You can simply press N to search forward, and Shift+N, to search backward. Type q to quit.

List Contents in Another Directory Using The Navigation Commands in Linux Command Line

If you wanna list the contents of another directory in the file manager, You have to click through several folders to get there in either tree or tile view. In the command line, you can pass ls [relative path] and use the “tab key” to autocomplete the command. Autocompletion needs enough characters to differentiate between similar names.

List contents in another directory with relative path
List contents in another directory with relative path

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 [absolute path] to a directory that works no matter which folder you’re in. As you can see I’ve got an image on the Pictures called “Image.jpg”.

List contents in another directory with an absolute path
List contents in another directory with an absolute path

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 navigation commands in Linux.

Note: The spaces in this folder name are little important. If you type it as is, LS will think you’re trying to show two separate folders: “Linux” and “Tutorials”, which don’t exist. Use up arrow to go back and escape the space characters with back-slashes ( \ ), or by using quotes, but never both.

Both of these methods work for list contents
Both of these methods work for list contents
Learn Linux Quickly: A Friendly Guide to Easily Master the World’s Most Powerful Operating System

CD (Change Directory Command)

To go to a different folder, use cd, or the change directory command with tab completion to get where you want. Notice that when you’re in a directory, you can use ls by itself. To jump back to the folder you just came from, type cd -. What we just did is similar to entering this folder in a file manager, and clicking the back button. If you type cd - again, you will 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 ~.

If you wanna go up a directory, type cd .. “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 -1a flag with ls.

The following screenshot shows all CD navigation commands in Linux executed in the terminal.

Commands executed in the terminal using the Change Directory command

Wrap Up

You may wonder what the use of a single dot folder is, but that’s something I’ll show you in another tutorial. So that’s just about everything you need to know to navigation commands in Linux like a pro – I believe you have found them as reminders and helpful. In addition to the ones in this tutorial, if you have any other ideas you want to inform the Linux community, add a comment below and let us know what Linux commands you’d like to learn.

Also Read: Ubuntu 21.10 Beta (Impish Indri) Released – Top New Features

Follow us on:

Disclosure: Your support helps keep the site running! We earn a referral fee for some of the services we recommend on this page. Learn More

Contribute to Linux Talks

Do you have news that the Linux community should know about? Use the form below to submit your story. Our moderators review every submission.

Share your love

Leave a Reply

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock