Linux Command Line

Linux Command Line

Master the Linux Command Line and Take Control of Your System

Introduction

Welcome everyone! 👋🏻

In the first part of this series, you learned about the boot process in Linux. In this part, you'll learn how to use the Linux command line and some basic commands.

The Linux command line is a powerful tool that allows you to control your system and perform tasks quickly and efficiently. It is used by system administrators, developers, and anyone else who needs to manage a Linux system.

To follow this blog, you'll need a working Linux environment. If you don't have one, you can install Linux by searching for "how to install Linux" on Google.

So what are you waiting for? Let's get started! 🚀

The command line has been around since the early days of Linux. Back then, it was the only way to interact with the operating system. Today, most Linux distributions come with a graphical user interface (GUI), but there are still many reasons why you should learn about the command line.

Imagine you're working with Linux servers. There's no graphical user interface (GUI), so you can't use your mouse to interact with the system. You need to use the command line interface (CLI) to do everything. If you don't know how to use the CLI, you're going to be in a lot of trouble. You won't be able to do anything, and you'll be completely lost.

That's why it's important to learn the CLI. It's a powerful tool that can help you automate tasks, troubleshoot problems, and gain a deeper understanding of how Linux works. Whether you're a system administrator, developer, or just a curious user, learning the CLI is a valuable skill.

Starting a shell

The shell is a program that provides a user with a way to interact with the Linux system via the command line. It is a regular program that runs whenever a user logs in to a terminal. In most of the Linux distributions bash is the default shell.

To get started, open your terminal emulator. If you're using a graphical desktop environment (GUI), you can open it by searching for it in your application launcher. If you're using Linux without a GUI, you'll already have a terminal emulator open.

Below is a screenshot of a Linux bash shell using a terminal.

The prompt anurag@server1:~$ indicates the current user name (anurag), the system name (server1), the current working directory ~ (home), and $ indicates that the shell is waiting for a command. The prompt may be different in different distributions, but the idea remains the same.

Shell Commands

Now that you have a basic understanding of the shell, let's take a look at commands. With shell commands, you can do anything from navigating directories to running programs to managing your system.

Listing Files and Directories

  • To list the files and directories in your current directory, run the ls command in your terminal.

In my case I've only one file in your case it may be different. By default ls doesn't list hidden files, so to list those files use the next command,

  • To list files and directories including hidden ones, run the ls -a command.

this time it lists all files and directories.

  • To list files and directories with more information, run the ls -l or ls -al command.

Let's break down the above information:

The long listing format lists each file and subdirectory on a single line, with additional useful information. The first line shows the total number of blocks contained within the directory. The following lines show information about each file (or directory), including:

  • File type: A character that indicates the type of file or directory, such as d for directory, - for file, l for linked file, c for character device, or b for block device.

  • Permissions: The permissions for the file or directory.

  • Owner: The owner of the file or directory.

  • Group: The group that owns the file or directory.

  • Size: The size of the file or directory in bytes.

  • Date and time: The date and time the file or directory was last modified.

  • Filename: The name of the file or directory.

Don't worry if you didn't understand all of that. We'll revisit this with more detail in upcoming blogs.

  • Sometimes you only want information for a specific file or a few files. To do this, you can filter the output of the ls command on the command line. You can also use regular expressions for more complex use cases.

File Handling

  • To create a file, run the touch command followed by the name of the file you want to create. The touch command will create a new file with the specified name and assign your username as the file owner. It is important to note that the touch command does not create a file with any content. It simply creates an empty file.

    As you can see above, the file was created and its size is 0 bytes.

  • To delete a file, you can run the rm command followed by the name of the file you want to remove. It is important to remember that there is no recycle bin in the terminal, so all actions are permanent.

  • To copy a file, you can run the cp command followed by the source and destination name.

  • To rename or move a file, you can run the mv command followed by the source and destination name.

    Rename a file:

    Move a file to another location:

  • You will learn two commands to view the content of a file: cat and less. Later in this series, you will learn more advanced commands to do the same.

    Using the cat command:

    Using the less command:

    The cat command displays all the data inside a text file, while less provides several handy features for scrolling through a text file, both forward and backwards, as well as some advanced searching capabilities.

  • To view the type of a file, you can run the file command followed by the file's name. This will help you avoid trying to view the content of a binary file, which may appear as gibberish on your monitor and may even freeze your terminal emulator.

    As you can see above, the output of the file command for a JSON, ASCII, and binary file is different.

You can use the -i option with the mv, andcp commands. This will prompt you before the command attempts to overwrite any pre-existing files.

Navigating the Filesystem

If you're new to Linux, you may be confused by how it references files and directories. Linux uses forward slashes (/) to separate directories, and it does not use drive letters in pathnames. This is unlike Windows, which uses backslashes (\) to separate directories and drive letters to identify different drives.

  • To find out your current working directory, you can run the pwd command.

  • To change directories, run the cd command followed by the directory name.

    Running the cd command without any arguments will take you to your home directory.

    Running the cd command with a dash (-) as an argument will take you to your previous working directory.

Stay tuned for upcoming blogs for advanced navigation.

Managing Directories

  • To create a new directory, run mkdir command followed by the directory name, you want to create.

    The long listing shows a d character next to the name of the new-directory. This indicates that new-directory is a directory.

  • To create directories and subdirectories in bulk, you can use the mkdir command with the -p argument. This will create the directories and subdirectories in a single step.

    The -R argument to the ls command lists directories recursively. This means that it lists all of the directories and files in the current directory, as well as all of the subdirectories of those directories. However, there is a better way to traverse directories. I will show you how to do this in an upcoming blog on advanced commands.

  • To delete a directory, you can use the rm -r command. This will recursively delete the directory, including all of the subdirectories and files it contains. It is a permanent operation, so be sure to back up any files that you want to keep before using this command.

  • To copy a directory, you can use the cp -r command to copy a directory, followed by the source and destination name.

  • To rename or move a directory to a different location, use the mv command the same way you would use it to rename or move a file.

Tip: Use the tab key to autocomplete commands, which can help you avoid typos.

That's all for this part. I hope you found this article informative and helpful. If you have any feedback, please feel free to share it in the comments below. Thank you for reading!

This is the second part of the Master Linux series, where you've learned about the Linux CLI and some basic commands. In the upcoming blogs, you will learn about advanced commands. Stay tuned!