Mastering Basic Linux Commands with a Twist

Mayuresh PatilMayuresh Patil
2 min read

Linux commands form the foundation of efficient system administration and automation. In this blog, we explore essential Linux commands with a unique twist, helping you enhance your command-line skills while tackling real-world scenarios.


1. Viewing File Content with Line Numbers

To read a file with numbered lines, use:

cat -n filename.txt

Example:

cat -n fruits.txt

This will display the contents of fruits.txt with each line numbered.


2. Changing File Permissions

Restrict file access to the owner by making it readable, writable, and executable only by them:

chmod 700 filename.txt

Example:

chmod 700 fruits.txt

Now, only the owner can read, write, and execute fruits.txt.


3. Checking Command History

To see the last 10 commands you executed, run:

history | tail -10

This helps in quickly reviewing recently used commands.


4. Removing a Directory and Its Contents

To permanently delete a directory along with its contents:

rm -r directory_name

Example:

rm -r my_folder

Caution: This command irreversibly deletes the directory and its files.


5. Creating and Displaying a fruits.txt File

To create a file with a list of fruits:

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

This adds multiple fruit names, each on a new line, and then displays the content.


6. Adding and Appending Content in devops.txt

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

This creates devops.txt with a list of fruits and later appends "Pineapple" to it.


7. Reversing the First Three Fruits

head -3 fruits.txt | tac

This command takes the first three lines and prints them in reverse order.


8. Sorting the Bottom Three Fruits

tail -3 fruits.txt | sort

This extracts the last three lines and sorts them alphabetically.


9. Creating and Displaying a Colors.txt File

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

This creates and displays a file listing different colors.


10. Prepending "Yellow" to Colors.txt

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

This adds "Yellow" as the first entry in Colors.txt.


11. Finding Common Lines Between fruits.txt and Colors.txt

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

This command displays lines that appear in both files.


12. Counting Lines, Words, and Characters

wc fruits.txt Colors.txt

Example output:

  7  7  42 fruits.txt
  8  8  50 Colors.txt
 15 15  92 total

This provides the total number of lines, words, and characters for both files.

0
Subscribe to my newsletter

Read articles from Mayuresh Patil directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mayuresh Patil
Mayuresh Patil

Hi, I am an Aspiring Engineer who was actively learning new skills which will maximize my knowledge.