Day 3: Mastering Basic Linux Commands ๐Ÿš€

Day 3: Mastering Basic Linux Commands ๐Ÿš€

ยท

4 min read

In today's task, we will cover essential Linux commands that are frequently used in day-to-day DevOps activities. This practical guide includes viewing files, managing permissions, handling file contents, and more. Let's dive in!

Task 1: View the Content of a File with Line Numbers ๐Ÿ“„

Linux provides the cat command to display the contents of a file. To display line numbers along with the content, we use the -n option.

Command :

cat -n chintamani.txt

Example :

This command prints the content of chintamani.txt with line numbers.

Task 2: Change File Permissions (Read, Write, Execute) ๐Ÿ”’

The chmod command is used to change file permissions. Here, we make the file readable, writable, and executable by the owner only.

Command :

chmod 700 chintamani.txt

Example :

This command sets permissions for the owner to read, write, and execute the file.

Task 3: Check the Last 10 Commands Executed ๐Ÿ“

We often need to check our command history. The history command helps, and we can use tail to show only the last 10 commands.

Command :

history | tail -10

Example :

his command shows the last 10 commands that you executed in your terminal.

Task 4: Remove a Directory and All Its Contents ๐Ÿ—‘๏ธ

The rm command is used to remove files and directories. To remove a directory along with all its contents, use the -r option.

Command :

rm -r chintamani.txt

Example :

This will remove the directory and all its files permanently.

Task 5: Create and Display a File ๐Ÿ“

Creating a file and adding content is easy with the echo command. We can use it to write multiple lines into a file.

Command:

vim fruits.txt
cat fruits.txt

Example :

This will create a fruits.txt file with fruit names on each line and then display the contents using cat.

Task 6: Append to a File ๐Ÿ“ฅ

Appending data to an existing file is also simple using the echo command.

Command :

echo "Pineapple" >> fruits.txt
cat fruits.txt

Example :

This will append the word "Pineapple" to the end of the fruits.txt file.

Task 7: Display the First 3 Fruits in Reverse Order ๐Ÿ”„

We can use head to show the first few lines of a file, and tac to reverse them.

Command :

head -3 fruits.txt | tac

Example :

This command will display the first three fruits from the file in reverse order.

Task 8: Show the Last 3 Fruits and Sort Alphabetically ๐Ÿ”ค

Similarly, we can use tail to get the last few lines and sort to organize them alphabetically.

Command:

tail -3 fruits.txt | sort

Example :

This shows the last 3 fruits, sorted in alphabetical order.

Task 9: Create a colors File and Display Content

Letโ€™s create a Colors.txt file and add colors line by line.

Command:

echo -e "Red\nPink\nWhite\nBlack\nOrange\nPurple\nGrey" > colors.txt
cat colors.txt

Example :

his command creates a colors.txt file and lists colors one by one.

Task 10: Prepend Content to a File ๐Ÿ› ๏ธ

Prepending content to a file is not as straightforward as appending. But we can manage using a trick.

Command:

echo "Yellow" | cat - colors.txt > temp && mv temp colors.txt
cat colors.txt

Example :

This will add "Yellow" at the beginning of colors.txt.

Task 11: Find Common Lines Between Two Files ๐Ÿ“‘

To compare two files and find common lines, we use the comm command.

Command:

comm -12 <(sort fruits.txt) <(sort colors.txt)

Example :

This command will show the lines common between fruits.txt and colors.txt.

Task 12: Count Lines, Words, and Characters ๐Ÿงฎ

The wc command gives the number of lines, words, and characters in a file.

Command:

wc fruits.txt colors.txt

Example :

This will count and display the lines, words, and characters for both files.

Wrapping Up ๐ŸŽ‰

These are some of the most basic but useful Linux commands for managing files and permissions. Performing these tasks not only helps you understand the command-line better but also builds a strong foundation for working with Linux in DevOps environments.

#90DaysofDevOps #TrainWithShubham #CloudComputing #Automation #Scaling #Infrastructure

ย