Level Up Your Linux Game: Mastering Essential File and Directory Commands



This content originally appeared on DEV Community and was authored by Paul Anaekwe

Welcome, fellow Linux enthusiasts! Whether you’re a seasoned command-line veteran or just starting your journey, understanding fundamental file and directory manipulation is crucial. In this blog post, we’ll tackle some common tasks you’ll encounter daily, equipping you with the knowledge to navigate and manage your Linux environment like a pro. Let’s dive in!

Peeking Inside: Viewing File Content with Line Numbers

Ever needed to examine the contents of a file while keeping track of each line? Linux provides elegant solutions for this. The cat command, short for concatenate, is a go-to tool for displaying file content. To add line numbers to your output, simply use the -n flag:

Command:

cat -n filename.txt

Setting Boundaries: Managing File Permissions
Linux’s robust permission system allows you to control who can read, write, and execute your files. To grant the owner (user) full read, write, and execute permissions while denying any access to others (group and others), the chmod command is your friend. Using symbolic mode, you can achieve this with:

Command:

chmod u=rwx filename.txt

Here, u stands for the user (owner), and =rwx sets the read, write, and execute permissions. Numerically, this translates to 700, where each digit represents the permissions for the owner, group, and others, respectively (4 for read, 2 for write, 1 for execute). Thus, the equivalent numeric command is:

Command:

chmod 700 filename.txt

This ensures that only the owner of paulanaekwe.txt has full control over it.

Looking Back: Reviewing Your Command History

Mistakes happen, or sometimes you just need to recall a command you ran recently. The history command is your personal time machine for the command line. To view the last 10 commands you executed, simply type:

Command:

history | tail -n 10

This will display a numbered list of your most recent commands, making it easy to review or even re-execute them using !n (where n is the command number).

Going Nuclear: Removing Directories and Their Contents

When it’s time to say goodbye to a directory and everything within it, the rm command with the -r (recursive) flag is essential. Be cautious with this command, as it permanently deletes files and directories.

Command:

rm -r directoryName

For a more forceful removal without prompts, you can add the -f (force) flag:

Command (Use with caution!):

rm -rf directoryName

This command will ruthlessly delete directoryName and all its subdirectories and files without asking for confirmation.

File Creation and Population: Working with fruits.txt

Let’s create a file named fruits.txt and populate it with a list of our favorite fruits, one per line. We’ll use the echo command along with output redirection (>) to achieve this:

Command:

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

The -e option in echo enables interpretation of backslash escapes, allowing us to use \n to create newlines. To verify the content, we can use cat:

Adding More Goodness: Appending to fruits.txt

This means adding content to the end of a file, like adding pineapple to the fruit.txt file

To add “Pineapple” to the end of our fruits.txt file, we’ll use the append redirection operator (>>):

Command:

echo “Pineapple” >> fruits.txt

The >> operator adds the output to the end of the file without overwriting the existing content that is why you can see from the picture above that we now have pineapple at the end of the list.

Top Three in Reverse: Ordering File Content

Sometimes you need to focus on specific parts of a file. To display the first three fruits from fruits.txt in reverse alphabetical order, we can combine the head and sort commands using a pipe (|):

Command:

wc fruits.txt Colors.txt

head -n 3 extracts the first three lines, and the pipe sends this output to sort -r, which sorts the lines in reverse order.

Bottom Three, Alphabetically: Another Look at Ordering

Similarly, to see the last three fruits and then sort them alphabetically, we can use the tail command:

Command:

tail -n 3 fruits.txt | sort

tail -n 3 retrieves the last three lines, and sort arranges them in alphabetical order.

Introducing Colors: Creating and Populating Colors.txt

Let’s create another file named Colors.txt and fill it with a list of colors:

Command:

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

This creates a new file named colors.txt and adds several colors into it

Adding to the Beginning: Prepending to Colors.txt (A Robust Method)

In an attempt to add yellow at the top of the list as the first item this method creates a temporary file with “Yellow” at the top, then concatenates the original Colors.txt to it, and finally replaces the original file. This is very robust.

command:

echo “Yellow” > temp_colors.txt
cat Colors.txt >> temp_colors.txt
mv temp_colors.txt Colors.txt


Explanation:

echo “Yellow” > temp_colors.txt: Creates a new file temp_colors.txt and puts “Yellow” into it.

cat Colors.txt >> temp_colors.txt: Appends the entire content of your Colors.txt to temp_colors.txt. Now temp_colors.txt has “Yellow” at the top, followed by all your original colors.

mv temp_colors.txt Colors.txt: Renames the temp_colors.txt file to Colors.txt, effectively replacing your old file with the new, modified one.

Finding Common Ground: Identifying Shared Lines

To discover the lines that are present in both fruits.txt and Colors.txt, the comm command is invaluable. It compares two sorted files line by line. To display only the common lines, we use the -12 flags:

Command:

comm -12 fruits.txt Colors.txt

(Note: comm requires the input files to be sorted for accurate results. If your files are not sorted, you might want to sort them first using sort fruits.txt > sorted_fruits.txt and then use comm -12 sorted_fruits.txt sorted_colors.txt.)

Counting It All: Lines, Words, and Characters

Finally, to get a statistical overview of our files, the wc command (word count) is perfect. It provides the number of lines, words, and characters in the specified files:

Command:

wc fruits.txt Colors.txt

This will display a table with the line count, word count, character count, and filename for both fruits.txt and Colors.txt, along with a total.

Conclusion: Your Linux Command Arsenal Grows!

Congratulations! You’ve now learned how to perform several essential file and directory operations in Linux. Mastering these basic commands will significantly enhance your command-line proficiency and empower you to navigate and manage your Linux system with greater ease and confidence. Keep practicing, and you’ll be a Linux power user in no time! Stay tuned for more Linux tips and tricks in future blog posts.


This content originally appeared on DEV Community and was authored by Paul Anaekwe