Basic commands in Linux (Simply explained with examples)

ยท

7 min read

Basic Command Line Operations:

  • Here we will learn about the Linux command line interface (CLI) and its benefits.

    Also, we will look into essential commands for file and directory operations most simply:

  • We will go through some basic commands like touch, mkdir, cp, mv, and rm.

  • File and text manipulation commands: cat, grep, head, tail

  • Let us understand and see the basic commands with an example:

    1. The touch command is used when we want to make an empty file.

    In the above example, we made a file namely DevOps using the Touch command and to see that file we have listed using the ls command

    1. In Linux systems, folders are considered to be Directories. And to create directories we use command mkidir

      Here we made a directory namely AWS using mkdir command and again we have listed the content by ls command.

    2. How to create multiple files and directories in a single command?

      Well, there are two ways to do it as shown above.

      For creating multiple directories within directory we use below method.

      Here, we have used -p for making directories, which stands for a parent.

      And thus we can make directories within a directory and those can be checked and verified by cd command which stands for change directory.

      So, if we wish to go to a particular directory then we use the cd command. And to go back one step further we use cd .. as shown below.

    3. If we need to check what directory we are currently in then we can use pwd command which stands for the present working directory.

    4. ls, ls -a and ls -la commands are used to show a list of content, show hidden content and show hidden content as a list (in more detail), below shows the usage of these 3 commands.

    5. Now we have learned so far about creating files and directories and how to list them. Now we will see how can we add content to the file.

      Here we can see, echo commands with quotations print the content on the screen directly, However, if we redirect that content using > sign to a particular file will add content to that file. Also to check the content of any file we use the cat command which stands for concatenate.

    6. Removing files and directories in Linux

      So, from rm command, we can remove files that are present/created.

      However, we need to use rmdir to remove directories.

      Also, it is important to note from the above image that we need to go to the last directory present from the parent directory to remove the same.

      We cannot delete the directory directly from the parent directory.

      To remove all the directories (including parent directory) in single command we use rm -r <parent directory name>

      Here -r stands for recursive or repetitive.

    7. The below image explains how texts can be copied from one file to another using the cp command which stands for copy and how we can move content from one file to another using mv command which stands for move.

      Also to note that, once we move the file it acts like cut and copy and gets removed from the origin and copied to the destination, unlike the cp command.

      • Text editors in Linux: Vim, Nano, and Vi editors.

        • Basic commands and shortcuts for editing files using Vim or Nano.

        • Saving and exiting files in different text editors.

          So generally we use file editors like vi or vim, here we can write a script, program or any content for that file. Since we cannot write number of lines and programs in a structured way. We use file editors.

vim <file name>

Once you are in Vim editor it won't allow you to write anything out there, we need to go into editing mode first by pressing I button from the keyboard and start editing your file.

In the above image, We have edited the text7 file from our list and then added the above content in insert mode. Now we have added our content and want to exit the editor, for that, we have to press escape then : and then wq and enter, this will exit the editor by saving your changes to the file.

A good point to note here is that if we don't want to save our changes to the file then we can simply press escape and : qa! and enter, this will not save your latest content and will exit the editor.

  1. To see the top and bottom content of our file, we use head and tail commands as shown below image:

  2. Now comes the major concept of root indicated by /

    1. Root Directory (/): The root directory is the top-level directory in the Linux file system hierarchy. It serves as the starting point for all other directories and files in the system. The root directory is represented by a forward slash (/) and contains all other directories, including system directories, user directories, and application directories.

    2. Root User (superuser): The root user, also known as the superuser or administrator, is a special user account with unrestricted access to the entire system. The root user has the highest level of privileges and can perform any administrative tasks, including modifying system files, installing software, and managing user accounts.

    3. Root Access: Root access refers to the ability to log in and operate the system as the root user. By default, regular users do not have root access to prevent accidental or unauthorized changes that could harm the system. Root access is typically obtained by using the "su" (switch user) command or by prefixing commands with "sudo" (superuser do) to execute them with root privileges.

    4. Root Permissions: Files and directories in Linux have ownership and permissions associated with them. The root user can

    5. change ownership and permissions of any file or directory on the system, providing full control over access and security.

    6. Root's Home Directory: The root user has its home directory, usually located at "/root." This directory is separate from regular user home directories and serves as the root user's workspace for storing configuration files and other system-related data.

It's important to exercise caution when using root privileges, as any changes made by the root user can have a significant impact on the system. It is generally recommended to use root access sparingly and only for necessary administrative tasks to maintain the security and stability of the Linux system.

We use sudo su command to become root user

Package Management:

  • We will review package management systems like APT (Advanced Package Tool)

  • Installing, updating, and removing software packages using package managers.

    • Difference between Install, Update and Upgrade.

      sudo apt update ---> We have packaged downloaded so index versions are being updated by this command. The apt-get update command is used to resynchronize the package index files from their sources on the Internet.

      sudo apt upgrade ---> Updated packages now will be installed by this command.

      sudo apt-get update ---> get command gives exposure to finding updates from the internet.

      sudo apt install <package name> ---> Installation of particular package

      Example: sudo apt install docker.io

      sudo apt purge docker.io ---> To remove packages

      Some other common commands:

    • lsb_release -a ---> OS distributor details

    • uname -a ---> OS details (like IP, Linux version, OS release date etc)

    • echo $SHELL ---> Shows the shell of the OS, and the types of shell that are there.

    • echo $USER ---> It is used to display the username of the currently logged-in user in Linux. When you execute this command in a terminal, it will print the username associated with your current session.

  • Key points to remember (General and not technical)

    1. Make mistakes while learning...we should master troubleshooting.
      You can only shoot if you make trouble.

    2. Ubuntu releases two versions of OS in one year (April and October).

    3. Fun fact about Ubuntu:

      Ubuntu, one of the most popular Linux distributions, derives its name from the South African concept of "ubuntu," which roughly translates to "humanity towards others." The name was chosen to reflect the project's aim of creating an open-source, community-driven operating system that is accessible and beneficial to everyone.

Thanks for reading the blog & do share them with someone in need :)

Please share your views and suggestions, they are always welcome.

See you then in the next blog.

Happy learning :)

ย