Linux Essentials

TABLE OF CONTENTS

Today, we will dive into the fundamental concepts of Linux, an open-source operating system that plays a crucial role in the world of DevOps. Understanding Linux is essential for any DevOps practitioner, as it forms the foundation of many server environments and provides a powerful platform for running applications and managing resources. In this blog post, we will explore the key components of Linux, its architecture, and some basic commands to get you started on your Linux journey

What is Linux?

Linux is an open-source operating system that serves as the foundation for numerous computing systems. Linux allows users to access and modify its source code. Linux interacts directly with a system's hardware, managing resources such as the CPU, memory, and storage. It provides a robust and secure platform for running applications and servers.

Components of the Linux File System:

a. Kernel: The Linux kernel is the core part of the operating system. It interacts directly with the hardware, providing an abstraction layer that hides low-level hardware details from system and application programs. It handles key activities and manages resources efficiently.

b. System Libraries: Linux offers a set of system libraries that implement most of the functionalities of the operating system. These libraries provide functions and programs that allow application programs and system utilities to access the kernel's features without requiring direct access rights to the kernel modules.

c. System Utility Programs: System utility programs in Linux perform specialized and individual tasks. These programs help manage the system, configure settings, perform administrative functions, and automate various operations.

The architecture of Linux:

A Linux system consists of several layers:

a. Kernel: The kernel is the heart of the Linux operating system. It handles critical tasks, manages hardware resources, and provides an interface for system processes and applications.

b. System Libraries: These libraries contain pre-compiled functions and code that allow applications to interact with the kernel and access system resources. They provide an abstraction layer, simplifying the development process for software developers.

c. System Utility Programs: System utility programs offer a range of functionalities to manage and configure the system. Examples include package managers, network configuration tools, and system monitoring utilities.

d. Hardware Layer: This layer comprises the physical components of the system, including the CPU, hard disk drives (HDD), RAM, and other peripheral devices.

e. Shell: The shell acts as an interface between the user and the kernel. It can be either a command-line shell or a graphical shell, enabling users to interact with the system and execute commands.

Basic Linux Commands:

To get started with Linux, here are some essential commands:

a. ls : Lists files and directories in the current location : ls

b. cd: Changes the current directory:cd directory_name

c. mkdir: Creates a new directory : mkdir directory_name

d. touch: creates a new file: touch file_name

e. rm: Removes a file : rmdir directory_name

f. cp: Copies a file : cp source_file destination path

g. mv: Moves or renames a file : mv source_file destinamtion

h. cat : Displays the contents of a file : cat file_name

i. Clear: Clears the terminal screen : clear

j.history: Shows a list of previously executed commands : history

Some performed operations :

Present Working Directory (pwd)

The Print/Present Working Directory command (pwd) allows you to quickly check your current location in the file system. It provides the absolute path of the directory you are currently working in.

Command: pwd: /home/user

Long List (ls -a)

The Long List command (ls) with the option -a is a versatile tool for listing directories and files, including hidden ones. It provides detailed information about each item, such as permissions, ownership, size, and last modification date.

Command -1 : ls: (Displays the list of directories and files in the current directory)

Command -2 : ls -a : (Displays the list of directories and files, including hidden ones)

Create a Nested Parent Directory

Creating nested directories can be accomplished easily using the mkdir command with the -p flag. This allows you to create multiple directories simultaneously, even if the parent directories don't exist.

Command: mkdir -p test1/test2/test3 Output: (Creates the nested directories test1/test2/test3)

1: View the Contents of a File

To view the contents of a file, we can use the cat command. For example, if we have a file named test.txt, we can enter the following command in the terminal:

cat test.txt, this will display the contents of the file on the terminal screen.

2: Change File Access Permissions

To change the permissions of files or directories, we use the chmod command, which stands for "change mode." In Linux, every file has three types of permissions: read (r), write (w), and execute (x). These permissions can be set for three different categories of users: the file's owner (u), the group (g) the file belongs to, and others (o) who are not the owner or part of the group.

For example, let's assume we have a file named test.txt where the user has read and execute access, and the group has read, write, and execute access. If we want to give the user read, write, and execute access, and the group only read access, we can use the following command:

In the above command, u+rwx grants read, write, and execute permissions to the file owner, and g-wx revokes write and execute permissions from the group.

3: Check Command History

To check the commands you have run in the current terminal session, you can use the history command. This command displays a list of previously executed commands, along with their line numbers.

4: Remove a Directory/Folder

To remove a directory or folder, we can use the rmdir or rm command. The rmdir command is used specifically to remove empty directories, while the rm command can be used to remove directories with contents and files.

For example, to remove an empty directory named XYZ:

And to remove a directory with contents and files named XYZ:

5: Create and View the Content of a File

To create a file, we can use the touch command to create a file named devopstools.txt:

To view the contents of a file, you can use the cat command.

6: Add Content to devopstools.txt

To add content to the devopstools.txt file, we can use either the vim or echo command.

Using the vim command:

This will open the devopstools.txt file in the Vim text editor. Press I enter insert mode, then add the following tools, one per line:

  • Jenkins

  • ansible

  • Docker

  • K8S

  • maven

  • git

  • terraform

  • datadog

  • slack

  • selenium

  • Once you've added the tools, press Esc to exit insert mode, then type :wq to save the changes and exit Vim.

7: Show Only the Top Three tools from the File

To show only the top three tools from the devopstools.txt file, we can use the head command with the -n option.

This command will display the first three lines (tools) from the file.

8: Show Only the Bottom Three tools from the File

To show only the bottom three tools from the devopstools.txt file, we can use the tail command with the -n option.:

This command will display the last three lines (tools) from the file.

9: Create and View the Content of another File

To create another file named services.txt, we can use the touch command:

Similarly, to view the contents of the services.txt file, you can use the cat command.

10: Add Content to services.txt

To add content to the services.txt file, we can use either the vim or echo command.

11: Find the Difference between devopstools.txt and services.txt Files

To find the difference between the contents of two files, such as devopstools.txt and services.txt, we can use the diff command. For example:

This command will display the differences between the two files, if any

Conclusion:

Congratulations! You've learned some essential commands to navigate and manage directories and files in the Linux file system. The pwd command helps you determine your current location, while the ls command with the -a option enables you to list directories and files, including hidden ones. Additionally, you now know how to create nested directories efficiently using the mkdir command with the -p flag. Also, We covered important concepts related to Linux fundamentals, such as file manipulation, permissions, and comparing files. Continue exploring the Linux file system, and remember to practice these commands to become more proficient. Stay tuned for more exciting blogs, where we will go further into Linux concepts to continue our journey in the world of DevOps. Happy learning!

Thank you for reading, and happy Linux exploring!

Let's learn together! I appreciate any comments or suggestions you may have to improve my blog content.

Thank you,

Akshay Nazirkar