Day 3: Basic Linux commands with a twist

Hi, I’m Rakshita. A Cloud, DevOps, AI, and Python enthusiast passionate about learning and simplifying technology for others. I love exploring how modern tools and automation can make systems smarter and more efficient. Here, I write about: ☁️ Cloud & DevOps practices 🤖 AI in the world of automation 🐍 Python for real-world problem-solving 💡 Growth, consistency, and the learner’s mindset My goal is to bridge the gap between learning and doing, and help others grow confidently in the evolving tech landscape.
- View the content of a file and display line numbers
To display the content of file with line numbers in Linux, we use 'cat' command with the '-n' option.
cat -n filename.txt
- Change the access permissions of files to make them readable, writable, and executable by the owner only?
To change the access permissions of file so that it is readable, writable and executable by the owner only, we use 'chmod' command with '700' mode.
chmod 700 filename.txt
Here's what 700 means:
'7' for the owner: read (4), write (2), and execute (1) permissions (4+2+1 = 7).
'0' for the group: no permissions.
'0' for others: no permissions.
- Check the last 10 commands you have run?
The 'history' command is generally used to display all the commands we used. By default, it shows all the commands we've run in the current session, but we can filter it to show only the last 10 commands:
history | tail -n 10
Here’s what each part of this command does:
history: Displays the command history.
|: Pipes the output of history to another command.
tail -n 10: Shows the last 10 lines of the piped output.
- Remove a directory and all its contents?
To remove a directory and all its contents, we use 'rm' command with the '-r' (recursive) option.
rm -r directory_name
- Create a
fruits.txtfile, add content (one fruit per line), and display the content?
To create a file in Linux, we can use 'touch' command or directly open the file with a text editor like 'nano' or 'vim'.
Creating a file (fruits.txt):
Using 'touch'
touch fruits.txt
Adding content to 'fruits.txt':
Using text editor 'vim'
vim fruits.txt
After using above (vim) command we can add contents in 'fruits.txt' file. (Write different fruits name per line). To save and exit the file, press 'esc' then ':wq' and 'enter'.
Display the content of fruits.txt:
Using 'cat' command
cat fruits.txt
- Add content in
devops.txt(one in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava. Then, append "Pineapple" to the end of the file?
We can use text editor like 'vim' to create and enter fruits directly:
vim devops.txt
In the text editor, add the following lines:
Apple
Mango
Banana
Cherry
Kiwi
Orange
Guava
To save and exit the file, press 'esc' then ':wq' and 'enter'.
To append "Pineapple" to the file, use 'echo' command with the append operator '>>'.
echo "Pineapple" >> devops.txt
To verify use 'cat' command
cat devops.txt
- Show the first three fruits from the file in reverse order?
To show first three fruits from file we use 'head' command
head -n 3 devops.txt
The 'head -n 3 devops.txt' command will output:
Apple
Mango
Banana
To reverse the order of the first three lines, we’ll use a combination of the 'head' and 'tac' commands. The 'tac' command is the reverse of the 'cat' command and displays lines in reverse order:
head -n 3 devops.txt | tac
This command first takes the top three lines of the file and then reverses their order. The output will be:
Banana
Mango
Apple
- Show the bottom three fruits from the file, and then sort them alphabetically?
To display the bottom three lines of the devops.txt file, we can use the 'tail' command
tail -n 3 devops.txt
The output will be:
Orange
Guava
Pineapple
To sort these lines alphabetically, we can pipe the output of the 'tail' command into the 'sort' command:
tail -n 3 devops.txt | sort
The output will be:
Guava
Orange
Pineapple
- Create another file
Colors.txt, add content (one color per line), and display the content?
To create and add content in Colors.txt we use 'vim' command:
vim Colors.txt
Add different colors name per line in file. To save and exit, press 'esc' then ':wq' and then 'enter'.
To display the content we use 'cat' command
cat Colors.txt
- Add content in
Colors.txt(one in each line) - Red, Pink, White, Black, Blue, Orange, Purple, Grey. Then, prepend "Yellow" to the beginning of the file?
To add content in Colors.txt we use 'vim' command
vim Colors.txt
Add content in above file
Red
Pink
White
Black
Blue
Orange
Purple
Grey
Save and exit by pressing 'esc', ':wq' and 'enter'.
To prepend "Yellow" in the beginning of the file, we can use 'echo' command with the append operator '>>' and a temporary file. Then, overwrite Colors.txt with the new content.
echo "Yellow" > tmp.txt
cat Colors.txt >> tmp.txt
mv tmp.txt Colors.txt
To display we use 'cat' command
cat Colors.txt
The output will be:
Yellow
Red
Pink
White
Black
Blue
Orange
Purple
Grey
- Find and display the lines that are common between
fruits.txtandColors.txt?
First, ensure both 'fruits.txt' and 'Colors.txt' are sorted alphabetically.
We'll use 'sort' command to sort the content and -o flag to create new file with sorted output.
sort fruits.txt -o fruits_sorted.txt
sort Colors.txt -o Colors_sorted.txt
Now, use the 'comm' command with the sorted files to find and display the common lines (in this case, common fruits/colors):
comm -12 fruits_sorted.txt Colors_sorted.txt
-1 suppress lines unique to fruits_sorted.txt.
-2 suppress lines unique to Colors_sorted.txt.
-12 display lines common to both files.
- Count the number of lines, words, and characters in both
fruits.txtandColors.txt?
We used 'wc ' command to count the number of lines, words and characters in files.
For Fruits.txt:
wc fruits.txt
For Colors.txt:
wc Colors.txt
The first column will represent: Number of lines.
The second column will represent: Number of words.
The third column will represent: Number of characters.
Conclusion
We explored fundamental Linux commands for efficient file operations like: Viewing File Content with Line Numbers, Changing File Permissions, Command History Management, Directory Deletion, Creating and Manipulating Text Files, Text Manipulation and File Metrics.
These foundational Linux commands empower developers and system administrators to efficiently manage file operations, ensure security, and analyze data effectively. Mastering these skills enhances proficiency and productivity in Linux environments, enabling smoother workflow and robust file management capabilities.
Happy Learning🌟




