Day 3 - Basic Linux Commands and with Twists

Managing files and directories in Unix can be intimidating for beginners. But with the right commands, you can efficiently handle tasks such as viewing file contents, modifying permissions, and manipulating data. In this blog, I will cover the following topics:

  1. Viewing the content of a file and displaying line numbers.

  2. Changing file access permissions.

  3. Checking your command history.

  4. Removing directories and their contents.

  5. Creating and manipulating text files.

  6. Finding common lines between files.

  7. Counting lines, words, and characters in files.

Let's dive in! ๐ŸŠโ€โ™‚๏ธ


1. View the Content of a File and Display Line Numbers

To view the content of a file with line numbers, use the cat command with the -n option.

cat -n example.txt

View Content with Line Numbers


2. Change the Access Permissions of Files

To change a file's access permissions to make it readable, writable, and executable by the owner only, use the chmod command.

chmod 700 example.txt

๐Ÿ”’ Explanation:

  • 7 (owner): read (4) + write (2) + execute (1) = 7

  • 0 (group and others): no permissions

Change File Permissions


3. Check the Last 10 Commands You Have Run

To view the last 10 commands you executed, use the history command.

history | tail -10

๐Ÿ”„ Explanation:

  • history: lists all the commands you've executed.

  • tail -10: shows the last 10 lines of the history.

Last 10 Commands


4. Remove a Directory and All Its Contents

To remove a directory and all its contents, use the rm command with the -r (recursive) option.

rm -r directory_name

โš ๏ธ Caution: This command is irreversible. Make sure you double-check before executing it.

Remove Directory


5. Create a fruits.txt File and Display its Content

Create a fruits.txt file and add some fruit names, one per line.

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

๐ŸŽ Example:

  • The -e option enables interpretation of backslash escapes.

  • \n represents a new line.

Create Fruits.txt


6. Append "Pineapple" to devops.txt

Create a devops.txt file and append "Pineapple" to the end of the file.

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

๐Ÿ Example:

  • The >> operator appends text to the file.

Append Pineapple


7. Show the First Three Fruits in Reverse Order

To display the first three fruits in reverse order, use head and tac commands.

head -3 fruits.txt | tac

๐Ÿ”„ Example:

  • head -3 shows the first three lines.

  • tac reverses the lines.

First Three Fruits in Reverse


8. Show the Bottom Three Fruits Alphabetically

To display the bottom three fruits and sort them alphabetically, use tail and sort commands.

tail -3 fruits.txt | sort

๐Ÿ”  Example:

  • tail -3 shows the last three lines.

  • sort arranges them alphabetically.

Bottom Three Fruits Sorted


9. Create Colors.txt and Display its Content

Create a Colors.txt file and add some color names, one per line.

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

๐ŸŒˆ Example:

Create Colors.txt


10. Prepend "Yellow" to Colors.txt

To prepend "Yellow" to the beginning of Colors.txt, use sed command.

sed -i '1iYellow' Colors.txt
cat Colors.txt

๐Ÿ’› Example:

  • sed -i '1iYellow': inserts "Yellow" at the first line.

Prepend Yellow


11. Find and Display Common Lines Between Two Files

To find and display the common lines between fruits.txt and Colors.txt, use the comm command.

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

๐Ÿ” Example:

  • comm -12: displays common lines.

  • <(sort file): sorts the files before comparing.

Common Lines


12. Count the Number of Lines, Words, and Characters

To count the number of lines, words, and characters in fruits.txt and Colors.txt, use the wc command.

wc fruits.txt Colors.txt

๐Ÿ“Š Example:

  • wc: word count, displays lines, words, and characters.

Count Lines, Words, Characters


0
Subscribe to my newsletter

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

Written by

Himanshu Palhade
Himanshu Palhade