Day 3 DevOps challenge - Mastering Basic Linux Commands: A Practical Guide

Day 3 DevOps challenge - Mastering Basic Linux Commands: A Practical Guide

Β·

4 min read

πŸ§πŸ› οΈ What Are Linux Commands? πŸš€πŸ’»

πŸ§πŸ”§ The term "Linux command" refers to the various commands that can be used in the Linux operating system's terminal (also known as the shell or command-line interface) to perform specific tasks, manage files, manipulate data, and control the system. πŸ’»πŸ› οΈ

Linux commands are usually typed in the terminal, and they follow a general syntax: command [options] [arguments]. The basic structure is as follows:

πŸ‘‰ command: The name of the Linux command you want to execute.

πŸ‘‰ options: Optional flags or switches that modify the behavior of the command.

πŸ‘‰ arguments: Input data or parameters required by the command. πŸ“₯

πŸ“„ To View What's Written in a File 🧐

πŸ“„ View File Contents with cat Command 🐱

With the cat command in Linux, you can easily display the contents of a file directly on the terminal. It's as simple as 🐱 and allows you to see what's inside at a glance!πŸš€ Let's assume we have a file named example.txt with the following content: "This is an example file. Hello, world!"

To view the content of the example.txt file, you can use the cat command like this:

cat example.txt

Output:

This is an example file.
Hello, world!

πŸ”’ To Change the Access Permissions of FilesπŸ”’

πŸ” Changing File Permissions with chmod Command πŸ›‘οΈ

In Linux, the chmod command is used to change file permissions. It controls who can πŸ‘€ read(r), ✍️ write (w), and πŸƒ execute(x) the file.

Let's say we have a file named example.txt Default permissions: -rw-r--r-- (Owner can read and write, Group and Others can only read)

To give full control to the owner, group, and others (equivalent to 777), you can use the chmod command as follows:

chmod 777 example.txt

Updated permissions: -rwxrwxrwx The permission 777 grants full read, write and execute access to the file for the owner (root), group, and other users.

πŸ”πŸ“œ To check which commands you have run till nowπŸ’»:

You can use the history command to display a list of previously executed commands in the terminal.

history

πŸ—‘οΈπŸ“‚ To remove a directory/folder in Linux πŸ§πŸ’»:

  1. To remove an empty directory using rmdir:

     rmdir directory_name
    
  2. To remove a directory and its contents recursively using rm:

     rm -r directory_name
    
  3. To remove a file using rm:

     rm filename.txt
    

To create a 🍊fruits.txt file and to viewπŸ‘€ the contentπŸ“

  1. To create the fruits.txt file:
echo -e "Apple\nBanana\nOrange\nGrapes" > fruits.txt
  1. To view the content of the fruits.txt file, you can use cat:
cat fruits.txt

Add content in devops.txt (One in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava.

To add the content into a file named devops.txt with each item on a new line, you can use the following command:

echo -e "Apple\nMango\nBanana\nCherry\nKiwi\nOrange\nGuava" > devops.txt

This command uses the echo command with the -e option to enable interpretation of escape characters like \n (newline). It then redirects the output of echo to the devops.txt file, effectively creating the file with the specified content, with each item on a new line.

🍊 Show Top Three Fruits from the File 🍎

To show only the top three fruits from the file devops.txt, you can use the head command

head -n 3 devops.txt

πŸ‰ Show the Bottom Three Fruits from the File πŸ‡

To show only the bottom three fruits from this file, you can use the following command

tail -n 3 devops.txt

πŸŽ¨πŸ“ Create Colors.txt and πŸ“– View the Content πŸŽ¨πŸ“

To create a new file named Colors.txt and view its content, you can use the following commands:

  1. To create the Colors.txt file:
echo -e "Red\nGreen\nBlue\nYellow\nOrange\nPurple\nPink" > Colors.txt
  1. To view the content of the Colors.txt using cat:
cat Colors.txt

πŸ”πŸ“ Finding Differences between fruits.txt and Colors.txt πŸ”πŸ“

To find the difference between the fruits.txt and Colors.txt files, you can use the diff command. The diff command compares two files line by line and shows the lines that are different. Here's how you can do it:

diff fruits.txt Colors.txt

Mastering Linux commands unlocks endless possibilities! πŸš€πŸ’» From managing systems to manipulating data, these tools empower you to take full control. Embrace the command line and venture forth with confidence into the world of Linux! 🐧πŸ’ͺ Happy exploring! 🌟

Did you find this article valuable?

Support Adarsh Jha by becoming a sponsor. Any amount is appreciated!

Β