When it comes to working with Linux, understanding how to navigate the filesystem is a fundamental skill. The ability to move around efficiently and locate files and directories is essential for any Linux user. In this blog post – Linux commands for beginners, we will explore four essential commands for filesystem navigation:
cd. We’ll provide step-by-step explanations and examples to help beginners grasp these commands and start navigating the Linux filesystem with confidence.
Many people speak of “freedom” with regard to Linux, but I don’t think most people know what this freedom really means. Freedom is the power to decide what your computer does, and the only way to have this freedom is to know what your computer is doing. Freedom is a computer that is without secrets. One where everything can be known if you care enough to find out.“William Shotts” (The linux command line)
In the world of computer programming and operating systems, the shell plays a pivotal role. It is a powerful tool that enables users to interact with their Linux System through a command-line interface (CLI). We will delve into the world of shells, exploring what they are, how they work, and why they are essential.
What is the shell?
A shell is a command line interface (CLI) program that acts as an intermediary between the user and the operating system.
It takes keyboard commands and passes them over to the operating system for execution.
A user can type the
ls command on the shell prompt, and the shell passes it to the Operating system which executes it by listing out files and directories.
Examples of popular shells include Bash (Bourne Again Shell), PowerShell, and Zsh.
Why use the command line?
Someone might ask, “Why do we have to use the command line when you can work easily with the graphical user interface”.
Well, the person might be right by saying “Work easily with a graphical interface”, but the TRUTH is that a graphical user interface makes easy tasks easy, while a command line interface (SHELL) makes different tasks POSSIBLE.
Sounds interesting, right?
With Shell, you have a flexible and efficient way to interact with the operating system. It enables you to perform complex tasks, automate repetitive processes, and write scripts that enhance productivity and system management.
What is the difference between a terminal and a shell?
Lots of people always confuse a terminal as a shell or a shell as a terminal. I was once there, I used to think the two are the same, but obviously, they are not!
So, what is a terminal?
Think of the terminal as a doorway that gives you access to the shell. It is a program application that provides a graphical user interface (GUI) for you to interact with the shell.
In simpler terms, it is a container that allows you to see and interact with the shell.
It is a text-based environment where you can type commands and receive text-based responses. It displays the output of commands and allows you to input new commands.
It provides features like scrolling, resizing, and managing multiple terminal windows or tabs.
The shell, on the other hand, is a command line interpreter. It is a program that understands the commands you type in the terminal and executes them. The shell is the actual “brain” that interprets and carries out your instructions.
When you type something into the terminal, the shell listens carefully and translates your instructions into a language that the computer can understand. It then tells the computer what to do and gets the results back.
Just think of the terminal as a door that lets you communicate with the shell, and the shell as your helpful friend who speaks the computer’s language and gets things done for you.
You can ask the shell to open files, create new folders, run programs, or do all sorts of cool stuff just by typing simple commands.
What is the shell prompt?
The shell prompt is where one types a command. It appears whenever the shell is ready to accept input.
The first thing to see in a shell is a prompt that contains the username and the name of the machine followed by a dollar sign.
Something like this:
To run any command on the shell, you need to type the command on the prompt and hit enter on the keyboard. This executes the command and prints out the output.
After each command’s execution, the shell displays the prompt to take in another instruction, and it goes on and on.
Most Linux distribution remembers the last 1000 commands by default. You can use the up and down arrow keys on the keyboard to view the history of commands on the shell.
Instead of repeating a particular command most often, you can use the history to easily identify the command and run it without getting to type it again.
Ending a terminal session – To end a terminal session, you can either use the
exit command or
CTRL + D on the keyboard to end a session. It closes all the processes running on the shell.
How to navigate the file system (Linux Commands for Beginners)
Unix-like operating systems such as Linux, organize their files in a hierarchical directory structure. This means they are organized in a tree-like pattern of directories which may contain files and other directories.
The first directory in the file system is called the “root directory”.
Navigation on the Linux file system simply means moving from one directory to another or listing the contents of your directory or checking to know your current location on the system.
To have your way around in a Linux file system, you need to understand these three basic most used Linux Commands for Beginners:
- pwd: prints name of the current working directory
- cd: changes directory
- ls: lists the content of a directory
pwd (prints the name of the current working directory):
Imagine you are in a massive building with many rooms, and you are not sure which room you are currently in. It can be quite confusing and disorienting.
In such a situation, what is the first thing you would do to find your way?
One approach would be to look for signs or labels that indicate the room number or name. By identifying the current room, you can communicate this information to others or use it as a reference point to navigate and find your way.
In the context of a computer’s file system, it is similar.
When you are working in the shell, you might find yourself in a specific directory (room) without knowing its exact location. This is where the
pwd command comes in.
pwd stands for “Print Working Directory”.
When you enter the “pwd” command in the terminal and press Enter, it displays the absolute path or the name of the current directory you are in.
It acts like the sign or label in our building analogy, telling you the exact location within the file system.
Knowing your current directory is essential because it helps you understand where you are located in the file system hierarchy. With this information, you can navigate to other directories, execute commands specific to that location, or communicate the precise location to others if needed.
cd (Change directory):
This is used to change the directory as the name implies. It is a command that helps you to move around your file system from one location to another depending on the pathname specified.
A pathname is a route you take along the branches of the file system tree to get to the directory you want.
When moving from one directory to another, you can specify either the Absolute pathname or the relative pathname.
Absolute pathname: This begins with the root directory and follows the tree, branch by branch until the path to the desired directory or file is completed.
You want to get into your directory where most of your system programs are stored, usually the “/usr/bin” in the Linux file system.
To do this, you specify the absolute path on your shell alongside the
cd command, see illustration below:
Current directory 👇
After changing the directory to “/usr/bin” with
The pathname “/usr/bin” specified is an absolute pathname because it starts from the root (/) to a directory called “usr” to another directory inside usr called “bin” where you will see your program files.
Relative Pathname: This starts from the working directory. It uses a couple of special notations to represent relative positions in the file system tree.
These special notations are dot (.) and dot dot (..)
- Dot (.) refers to the working directory
- Dot dot (..) refers to the working directories’ parent directory
Current working directory 👇
After changing the directory to the parent directory (using the relative path):
From the illustrations, using the dot dot (..) notation, takes you back one step which is the parent of the current directory.
Summary of cd commands
- cd : changes to the home directory
- cd . : current directory
- cd .. : changes to the parent directory
- cd – : changes to the previous working directory
- cd ~user_name : changes to the home directory of user_name
With the help of the cd command, you can move around your Linux file system from one location to another easily.
Ls (lists directory contents)
The “ls” command stands for “List” and is used to display the contents of a directory in the terminal.
When you enter “ls” followed by pressing Enter, it provides you with a list of files and directories present in the current directory.
By using the “ls” command, you can quickly get an overview of the files and directories within the current location. This allows you to see what is available and navigate through the file system more effectively.
Below are some examples of how the ls command can be used:
ls (Basic usage):
This lists all the contents of the current directory as shown above.
ls -l (Detailed listing):
The “-l” option provides a detailed listing that includes additional information such as file permissions, owner, group, size, and modification date.
ls -a (Display hidden files)
The “-a” option shows all files, including hidden files that start with a dot (.)
The cool part of “ls” is that you can use it to view multiple directories by simply specifying their path.
You can choose to see the content of the Desktop directory as well as the /usr directory all at a time.
All you need to do is pass the paths you wish to view its entries to the ls command giving space in between them.
See the example below;
These examples demonstrate different ways to use the ls command to obtain various types of information about files and directories within a specific location.
File command (determining the file type)
The file command is a powerful utility in the command-line interface that helps determine the type of a file. It analyses the contents of a file and provides information about its format and characteristics.
Here is how the file command works and some examples of its usage:
Replace the “filename” with the name of the file you want to examine. The file command will analyze the file’s contents and provide information about its type.
In the example above, the file command identifies the file as a text document.
The file command is beneficial when you come across a file without a file extension or when you want to confirm the type of a file before working with it. It can help you identify the correct program or tool to use for opening or processing a particular file.
less command (view file content)
The less command is a useful tool for viewing the contents of a file within the command-line interface. It allows you to scroll through and read the file without opening a separate text editor.
Here’s an overview of how to use the less command:
Replace the “filename” with the name of the file you want to view. The less command will open the file in a pager-like interface, enabling you to navigate and read its contents.
Once the file is opened with less, you can use various keyboard commands to navigate through the content, such as:
- Use the arrow keys or the Page Up and Page Down keys to scroll up and down.
- Press the Spacebar to scroll forward one screen at a time.
- Press the “b” key to scroll back one screen at a time.
- Press the “G” key to go to the end of the file.
- Press the “gg” keys to go to the beginning of the file.
To exit the less command and return to the command prompt, press the “q” key.
The less command is essentially useful for viewing large files or reading through the contents of log files, configuration files, or any text-based files within the command-line environment.
Overall, the less command provides a convenient way to view file contents interactively within the command-line interface.
Quiz Time! Test your knowledge and share your answers in the comment section below.
1. Which command is used to change the current directory?
2. What does the pwd command do?
a) Lists the contents of a directory.
b) Changes the current directory.
c) Prints the name of the current working directory.
d) Opens a file for viewing.
3. Which command is used to list the contents of a directory?
4. What is the purpose of the less command?
a) Changes the current directory.
b) Prints the name of the current working directory.
c) Lists the contents of a directory.
d) Views the contents of a file interactively.
5. What does the file command do?
a) Changes the current directory.
b) Prints the name of the current working directory.
c) Views the contents of a file interactively.
d) Determines the file type based on its content.
6. How can you navigate to the home directory using the cd command?
a) cd /home
b) cd ~
c) cd .
d) cd ..
Let’s see how well you’ve grasped the concepts of the cd, pwd, ls, less, and file commands.
Don’t worry if you’re not sure about some of the answers – it’s all part of the learning process! We’re excited to hear from you and discuss your insights and experiences with the command line.
Challenge your fellow readers and let’s engage in a friendly knowledge-sharing session. Ready, set, comment!“
🔖 Further Resources:
Before you wrap up, we invite you to delve deeper into the world of Linux file systems.
- Mastering the Linux File System: A Complete Overview – It offers a comprehensive understanding of the file system hierarchy, advanced management techniques, and more.
- How To Navigate Efficiently With Shell Terminal – Expand your Linux knowledge and become a file system expert!
- How to Use Vim for Linux Users: A Step-by-Step Guide
- How to use Emacs editor for Linux Users: A Step-by-Step Guide
Open up your command-line interface and put into practice what you’ve learned about the cd, pwd, ls, less, and file commands. Follow the step-by-step guide we’ve provided in this blog post to navigate directories, view file contents, and determine file types.
Don’t be afraid to experiment! Explore different directories, try different options and flags with the commands, and observe the results. Gain confidence as you see first-hand how these powerful tools can enhance your productivity and understanding of your file system.
Share your experiences with us! We want to hear about your adventures in the command line. Have you discovered any useful tips or tricks? Do you have any unique use cases for these commands? Leave a comment below and join the conversation.
Remember, practice makes perfect. The more you engage with the command line, the more comfortable and proficient you will become. Embrace the command-line interface as a valuable skill that can empower you in various aspects of your digital life.
Your journey awaits!
Got a Linux command question? Join our ‘Ask a Question’ forum! Connect, learn, and get expert help from our community. Join now!
Level up your Linux skills with Hostinger’s tutorial on “40 Essential Linux Commands Every User Should Know“. Explore now!