Todayβs challenge in the #90DaysOfDevOps journey was all about getting comfortable with basic Linux commands while adding some fun twists. These are foundational skills for any DevOps engineer, as most of the infrastructure you'll deal with will be Linux-based.
Let's dive into today's tasks and see how to solve them with practical Linux commands.
π Task 1: Viewing File Content with Line Numbers
To view the contents of a file and display line numbers, we use the cat command with the -n option.
cat -n filename.txt
Example:
cat -n Devops_File --> This will display the content of fruits.txt with line numbers next to each line. Perfect for tracking changes or debugging!
π Task 2: Changing File Permissions
To make a file readable, writable, and executable by the owner only, we use the chmod command with the appropriate numeric code for permissions.
chmod 700 Devops_File
Explanation:
7 means the owner has read (r), write (w), and execute (x) permissions.
0 means no permissions for the group and others.
Example:
chmod 700 Devops_File
This will restrict access to the owner only.
πΉ Task 3: Viewing Command History
To view the last 10 commands you have run in the terminal, use:
history | tail -n 10
This will show the most recent 10 commands from your shell history.
π Task 4: Removing a Directory and Its Contents
The rm command with the -r option allows you to recursively remove a directory and all its contents.
rm -r directoryName
This will delete the directory and everything inside it.
Example:
rm -r old_directory
π Task 5: Creating and Viewing fruits.txt
To create a file and add one fruit per line:
echo -e "Apple\nMango\nBanana\nCherry\nKiwi\nOrange\nGuava" > fruits.txt
You can now view the contents with:
cat fruits.txt
π Task 6: Appending to a File
To append a new line to an existing file, use the echo command with the append (>>) operator.
echo "Pineapple" >> fruits.txt
This will add "Pineapple" as the last entry in fruits.txt.
π Task 7: Displaying Fruits in Reverse Order
To show the first three fruits from the file in reverse order:
head -n 3 fruits.txt | tac
Explanation:
head -n 3: Shows the first three lines.
tac: Reverses the order of lines.
π Task 8: Sorting the Bottom Three Fruits
To display the bottom three fruits and sort them alphabetically:
tail -n 3 fruits.txt | sort
This will show the last three fruits in alphabetical order.
π Task 9: Creating Colors.txt
To create a Colors.txt file and add colors line by line:
echo -e "Red\nPink\nWhite\nBlack\nBlue\nOrange\nPurple\nGrey" > Colors.txt
View the content with:
cat Colors.txt
π Task 10: Prepending to Colors.txt
To prepend "Yellow" at the beginning of the file:
echo "Yellow" | cat - Colors.txt > temp && mv temp Colors.txt
This command adds "Yellow" as the first line in the file.
π Task 11: Finding Common Lines
To find and display lines that are common between fruits.txt and Colors.txt:
comm -12 <(sort fruits.txt) <(sort Colors.txt)
This command sorts both files and compares them to find common lines.
π Task 12: Counting Lines, Words, and Characters
To count the number of lines, words, and characters in both files:
wc fruits.txt Colors.txt
This command displays the line, word, and character count for each file separately.
π§ Wrapping Up Day 3
Learning Linux commands is not just about memorizing syntax; itβs about understanding how to use them effectively in day-to-day DevOps tasks. These commands are key when working with cloud infrastructure, scripting, and automation.
If you're on the #90DaysOfDevOps journey too, feel free to connect! Letβs keep learning and growing together. π‘
Top comments (1)
I'd like to offer a suggestion:
would be better using
printf
because generallyprintf
is better thanecho
for portability, and because people are used to it using those sort of escapes.