Linux Command-Line Basics: Practical Exercises for Fundamental Operations

Delta Rahmat Fajar Delviansyah
6 min readFeb 7, 2024

--

Linux commands (source: Unsplash)
Linux commands (source: Unsplash)

Introduction:

In this article, we’ll engage in practical exercises centered around essential commands within the Linux operating system, specifically focusing on file and directory manipulation. These exercises are designed to reinforce your command-line skills and proficiency in handling common tasks related to navigating directories, creating and managing files, as well as renaming and moving them. While the exercises primarily revolve around file operations, they provide a solid foundation for beginners and offer a hands-on approach for mastering fundamental Linux commands. Join us on this journey to enhance your command-line expertise through interactive exercises, perfect for those looking to bolster their Linux skills. Let’s get started!

Question 1: Directory Navigation

What is the command to navigate to the directory /home/<username>/Desktop?

Answer: To navigate to the directory /home/<username>/Desktop, you can use the cd (change directory) command. The command is:

cd /home/<username>/Desktop
Example of using the cd command
Example of using the cd command

Explanation: The cd command is used to change the current working directory, and by specifying the path after cd, you move into the desired directory.

Question 2: Viewing Directory Contents

Which command is used to view the contents of a directory, including hidden files and directories?

Answer 2: The command to view the contents of a directory, including hidden files and directories, is ls with the -a option.

ls -a
Example of using ls command
Example of using ls command

Explanation: The ls command is used to list directory contents, and the -a option shows hidden files and directories, denoted by filenames starting with a dot (.)

Question 3: Creating Directories

How do you create a new directory named ProjekBaru along with Backup and images within the /home/<username>/ directory in the current directory? Also, create files named: Skripsi.docx, Laporan.txt, data.txt, Old.txt, catatan.txt

Answer 3: To create the specified directories and files, you can use the following commands:

mkdir /home/<username>/ProjekBaru /home/<username>/backup /home/<username>/images
touch /home/<username>/Skripsi.docx /home/<username>/Laporan.txt /home/<username>/data.txt /home/<username>/Old.txt /home/<username>/catatan.txt
Example of using mkdir & touch command
Example of using mkdir & touch command

Explanation: The mkdir command creates directories, and the touch command creates empty files.

Question 4: Deleting Files

What command is used to delete the file named Laporan.txt?

Answer 4: The command to delete the file named Laporan.txt is rm (remove):

rm laporan.txt
Example of using rm command
Example of using rm command

Explanation: The rm command removes or deletes files.

Question 5: Copying Files

How do you copy the file data.txt from the current directory to the /home/<username>/backup directory?

Answer 5: To copy the file data.txt to the /home/<username>/backup directory, you can use the cp (copy) command:

cp data.txt /home/<username>/backup/
Example of using cp command
Example of using cp command

Explanation: The cp command is used to copy files.

Question 6: Moving Files

What is the command to move the file gambar.png from the current directory to the /home/<username>/images directory?

Answer 6: To move the file gambar.png to the /home/<username>/images directory, you can use the mv (move) command:

mv gambar.png /home/<username>/images/
Example of using mv command
Example of using mv command

Explanation: The mv command is used to move or rename files and directories.

Question 7: Renaming Files

What command is used to rename the file old.txt to new.txt?

Answer 7: The command to rename the file old.txt to new.txt is mv (move):

mv old.txt new.txt
Example of using mv command
Example of using mv command

Explanation: The mv command not only moves files but can also be used to rename them by specifying a new destination name.

Question 8: Finding Files

How do you search for a file named Skripsi.docx throughout the entire system?

Answer 8: To search for a file named Skripsi.docxthroughout the entire system, you can use the find command:

find / -type f -name Skripsi.docx 2>/dev/null
Example of using find command
Example of using find command

Explanation: The find command searches for files and directories based on specified criteria. In this case, it looks for a file named Skripsi.docx (-name) of type file (-type f) starting from the root directory (/). The 2>/dev/null part redirects standard error (stderr) to /dev/null, discarding any error messages and ensuring a cleaner output.

Example of using find command without 2>/dev/null
Example of using find command without 2>/dev/null

This approach is useful to avoid being inundated with permission-denied messages or other potential errors when searching for files in directories where the user may not have access.

Question 9: Displaying File Contents

What command is used to display the contents of the file catatan.txt?

Answer 9: To display the contents of the file catatan.txt, you can use both cat and strings commands:

# Using cat
cat catatan.txt

# Using strings
strings catatan.txt
Example of using cat & strings command
Example of using cat & strings command

Explanation:

Using cat:

  • cat is a straightforward command used to concatenate and display the entire contents of a file. It's suitable for small text files and provides a simple way to view the file content.
cat catatan.txt

Using strings:

  • strings is a command that extracts printable strings from a binary file, but it can also be used to display the textual content of a file. It's particularly useful for extracting human-readable text from binary files.
strings catatan.txt

Both commands provide different ways to view the contents of the file. While cat displays the entire file, strings focuses on extracting readable text, which can be useful in scenarios where the file might contain binary data or non-printable characters.

Question 10: Editing Files with Nano

How do you edit the file /etc/hosts by adding specific commands, then copy it to hosts-new.txt and display the results using the Nano text editor?

151.101.53.140 reddit.com
151.101.53.140 old.reddit.com
151.101.53.140 www.reddit.com
151.101.53.140 www.old.reddit.com
198.41.209.141 http://out.reddit.com

Answer 10: To edit the /etc/hosts file, add specific commands, copy it to hosts-new.txt, and display the results using Nano, you can follow these steps:

nano /etc/hosts
# Add the specified commands
151.101.53.140 reddit.com
151.101.53.140 old.reddit.com
151.101.53.140 www.reddit.com
151.101.53.140 www.old.reddit.com
198.41.209.141 http://out.reddit.com

# Save and exit Nano (usually Ctrl + X, then Y, then Enter)
cp /etc/hosts hosts-new.txt
cat hosts-new.txt
Example of using nano command
Example of using nano command
Example of using nano text editor
Example of using nano text editor
Example of using cp & cat command
Example of using cp & cat command

Explanation: The nano command opens the Nano text editor for editing the /etc/hosts file. After making changes, you save and exit Nano. Then, the cp command copies the modified hosts file to hosts-new.txt, and cat displays the contents of hosts-new.txt.

Great job on completing these Linux exercises! You’ve just unlocked some essential skills for using the command line. Whether you’re a beginner or refining your knowledge, mastering these basics sets the foundation for more advanced Linux adventures.

As you continue, remember to explore and experiment. The command line is your tool to efficiently manage your system, and practicing regularly will make you more comfortable over time.

We hope these exercises were helpful, and encourage you to keep learning and trying new things. Happy coding, and enjoy your journey with Linux! :)

--

--