Linux - Day 3

Linux - Day 3

Day 3: Getting Hands-On with Basic Linux Commands 💻

Hey there! 👋 Today, I continued my Linux journey by diving into some of the most commonly used commands. If you’re new to Linux or just brushing up on your skills, these commands are the perfect way to get comfortable with managing files and working in the command line. Let me walk you through what I learned today, with examples!


📝 Tasks and What I Learned:

1. Viewing File Contents with Line Numbers

Want to see the contents of a file along with line numbers? This is super helpful when you need to refer to specific lines in a script or document. Here’s how you do it:

cat -n <filename>

This command shows the file content with line numbers, making it easier to reference and review.


2. Changing File Permissions (Owner Only)

Managing who can access your files is important. To limit access to yourself (the file owner) so only you can read, write, and execute, use this command:

chmod 700 <filename>

This makes the file only accessible to you, keeping it secure from others on the system.


3. Checking Your Recent Commands

Need to look back at the commands you’ve recently used? Linux has got you covered. To check the last 10 commands you ran:

history | tail -10

This is a quick way to find that one command you forgot or to retrace your steps.


4. Deleting a Directory and Its Contents

If you ever need to remove an entire folder along with everything inside it, use this command:

rm -rf <directory>

This deletes the directory and all its contents without asking for confirmation (so be careful!).


5. Creating a Simple Text File (fruits.txt)

Next, I created a file to store some fruit names, one per line. Here’s how to do that:

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

The -e option makes sure that each fruit gets added on a new line. After creating the file, you can check what’s inside with:

cat fruits.txt

6. Adding More Content to the File

Want to add something to the end of a file without overwriting it? This command lets you append new text:

echo "Pineapple" >> fruits.txt

This adds "Pineapple" to the end of the fruits.txt file.


7. Showing the First Three Items in Reverse Order

I wanted to see the first three fruits but in reverse order. Using a combination of two commands (head and tac), here’s how I did it:

head -n 3 fruits.txt | tac

First, it grabs the first three lines, and then it reverses their order. Pretty neat, right?


8. Sorting the Last Three Items

To see the last three fruits in alphabetical order, I used these commands together:

tail -n 3 fruits.txt | sort

This first shows the last three lines of the file and then sorts them alphabetically.


9. Creating a Colors File

Next, I created another file called Colors.txt and added a list of colors:

echo -e "Red\nPink\nWhite\nBlack\nBlue\nOrange\nPurple\nGrey" > Colors.txt

To double-check the contents, just run:

cat Colors.txt

10. Adding a Line at the Beginning of a File

What if you need to add something to the top of a file instead of the end? Here’s how I added "Yellow" to the beginning of my Colors.txt file:

sed -i '1i Yellow' Colors.txt

This command inserts "Yellow" as the first line.


11. Finding Common Lines Between Two Files

I wanted to see if any lines were the same in both my fruits.txt and Colors.txt files. Using this command, I was able to compare them:

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

It shows any lines that appear in both files after sorting them.


12. Counting Lines, Words, and Characters in Files

Lastly, I learned how to count the number of lines, words, and characters in a file. Here’s how I did it for both my files:

wc fruits.txt Colors.txt

This gives you a quick breakdown of how much content is in each file.


🚀 What I Took Away from Today:

Today’s practice really showed me how powerful even the simplest Linux commands can be. Whether it's managing file permissions, comparing files, or quickly sorting content, these commands make it easy to work efficiently in the command line.

If you’re learning Linux, give these a try—they're great for building a strong foundation.


👨‍💻 Your Turn!

What Linux commands do you find helpful? Have any tips or tricks for making life easier in the terminal? I’d love to hear your thoughts! Drop a comment below and let’s share our knowledge. 😊