Day #3 - Linux Essential Commands

ยท

3 min read

Hi Everyone and Welcome back On the Day 3 of our DevOps journey, we're diving deep into the heart of Linux to unravel its remarkable commands. Today, we'll discover how to interact with files and directories like seasoned pros. Let's dive in with practical examples.

Viewing File Contents:

Ever been curious about the contents of a file? Linux provides the cat command to satisfy your curiosity. Just type cat filename, and you'll see the magic unfold:

[ec2-user@ip-172-31-1-21 ~]$ cat content.txt

This reveals the entire content of the file content.txt. It's like opening a treasure chest to find the hidden gems inside.

Changing File Permissions:

Linux empowers you to control who can access your files. The chmod command is your key to managing permissions. For example:

[ec2-user@ip-172-31-1-21 ~]$ chmod 600 content.txt

This command changes the permissions of the file content.txt so that only the owner can read and modify it, while others have no access.

Reviewing Command History:

The history command is your time machine, letting you travel back in time to revisit the commands you've used. Just type:

[ec2-user@ip-172-31-1-21 ~]$ history

It's like flipping through the pages of a diary filled with your digital adventures, showing all the commands you've run.

Removing Directories:

If you need to tidy up your digital space by removing directories, Linux offers two powerful commands: rmdir for empty directories and rm -r for directories with content. For example:

[ec2-user@ip-172-31-1-21 ~]$ rm -rf siddharth/

This command deletes the directory siddharth along with all its contents, just like cleaning out an old attic.

Creating and Viewing Files:

Creating a new file is as simple as pie with the touch command. To create a file named "fruits.txt," simply enter:

[ec2-user@ip-172-31-1-21 ~]$ touch fruits.txt

Once created, you can view the file's contents using the cat command:

[ec2-user@ip-172-31-1-21 ~]$ cat fruits.txt

Adding Content to Files:

The echo command helps you add content to your files. For example, to create a "devops.txt" file and populate it with a list of fruits, use:

[ec2-user@ip-172-31-1-21 ~]$ echo -e "Apple\nMango\nBanana\nCherry" > devops.txt

This command adds each fruit on a new line within "devops.txt."

Displaying Top and Bottom Lines:

If you're looking for a quick overview of a file, Linux offers two handy commands: head and tail. To display the top three fruits from "fruits.txt," use:

[ec2-user@ip-172-31-1-21 ~]$ head -n 3 fruits.txt

This provides a glimpse of the first three lines in the file.

Now, to show only the bottom three fruits from a file, you can use the tail command with the -n option to specify the number of lines you want to display. Here's how you can do it:

[ec2-user@ip-172-31-1-21 ~]$ tail -n 3 fruits.txt

This command will display the last three lines from the "fruits.txt" file, which is like plucking the last three fruits from your basket. It's a useful way to view the end of a file, especially when you have a large document or log file.

Creating and Comparing Another File:

You can create another file, like "Colors.txt," using the touch command. Then, add content using echo. For instance:

[ec2-user@ip-172-31-1-21 ~]$ echo -e "Red\nPink\nWhite\nBlack\nBlue\nOrange" > Colors.txt

To compare "fruits.txt" and "Colors.txt" and see the differences, the diff command is your guide:

[ec2-user@ip-172-31-1-21 ~]$ diff fruits.txt Colors.txt

Conclusion

With these Linux commands in your toolbox, you're well on your way to becoming a Linux expert. Stay tuned for more thrilling discoveries on our DevOps journey! ๐ŸŒ #DevOps #LinuxCommands #LearningJourney

ย