Basic Linux for DevOps

Basic Linux for DevOps

Introduction

Linux is the foundation on which DevOps stands. We need to be at our absolute best while learning Linux since a lot of Linux commands are used in various DevOps tools. In this article and the coming articles, we are going to understand the Linux file system, how to edit files, file permissions, filters & redirection, users & groups, sudo user and its uses, package management, services & processes and a lot more Linux commands which are essential while learning DevOps.

Linux OS

Linux is a free and open-source operating system that was first released in 1991 by Linus Torvalds. It is based on the Unix operating system and is widely used in servers, supercomputers, embedded devices, and mobile phones.

One of the main advantages of Linux is its flexibility and customizability. It comes in many different distributions or "distros," each with its own set of features and pre-installed applications. Some of the most popular Linux distros include Ubuntu, Fedora, Debian, and CentOS.

In this article, we are mainly going to discuss RPM-based Linux commands.

Basic Linux Commands to start with

  • pwd - Displays current working directory.
$ pwd

  • whoami - Know which user you are.

      $ whoami
    

  • ls -l - Lists the contents in a directory along with the hidden files in it.

      $ ls -l
    

  • cd - Changes one directory to another.

$ cd [Directory]
$ cd .. -> Takes one level up in the directory tree.

  • man - Displays the manual of any command if available.
$ man [Command]
  • mkdir - Creates a directory.
$ mkdir [Directory Name]

  • rmdir - Removes the directory.

      $ rmdir [Directory Name]
    
  • touch - For creating files in directories.

      $ touch [file-name]
    
  • cat - Reads the content of a file.

$ cat [file]

Note: For editing a file we will need to install a text editor such as Vim editor or Nano editor. We will be learning to do that later in the upcoming articles.

Absolute path & Relative path

What is an absolute path?

An absolute path is defined as specifying the location of a file or directory from the root directory(/). We can say that the absolute path is a complete path from the start of the actual filesystem from the root directory.

Examples of absolute path:

1. /home/ayaan/linux-practice/
2. /var/ftb/pub/
3. /boot/grub/grub.conf/

You can see that all these paths started from the "/" directory which is the root directory for every Linux machine.

What is a relative path?

A relative path is defined as a path to the present working directory*(pwd)*. If I am located in the /home/ayaan directory and I want to change the directory to /home/ayaan/linux-practices directory, I can use the relative path to do so.

If you see, this path did not start with the "/" directory.

Copying, Moving, & Removing files.

In the above commands, we have seen how to create files and directories in Linux. Now we will see how to copy, move or remove the contents in these files and directories.

  • Copying files into the directory.

      $ cp [file] [directory]
    

In the above image, I have created four files namely file1.txt, file2.txt, file3.txt, & file4.txt. I also created two directories, newdir and testdir respectively.

For copying a file into the directory we need to use the cp command. Hence, I used the cp command to copy the file1.txt into the testdir directory.

  • Copying directories from one location to another.

      $ cp -R [source_directory] [destination_directory]
    

    To copy a directory on Linux, you have to execute the cp command with the -R option for recursive and specify the source and destination directories to be copied.

In the above image, we have two directories, testdir & newdir. I copied the newdir into the testdir using cp -R command. By changing the directory into the testdir and listing its contents we can that the newdir has been successfully copied into it.

  • Moving files from one location to another.

      $ mv [source_file] [destination_file]
    

In the above image, I created a new directory namely the devopsdir and moved the devopsdir into the testdir using the mv command. By changing the directory into the testdir and listing its contents we can see that the devopsdir has been successfully moved into it.

Similarly, we can move files from one location to another using the mv command.

Here, the file2.txt & file3.txt has been moved into the newdir.

  • Removing files and directories.

    Removing files:

      $ rm [filename]
    

    To remove non-empty directories and all the files without being prompted, use rm with the -r (recursive) and -f options:

      $ rm -rf [dirname]
    

Conclusion

In this blog, I explained the basics of Linux commands and concepts that are required for starting with DevOps. In the upcoming blogs, I will explain the other important topics that I mentioned at the start of this blog.

Did you find this article valuable?

Support Ayaan Bordoloi by becoming a sponsor. Any amount is appreciated!