Introduction
Welcome to Linux Command Basics. This lab introduces you to essential Linux commands that form the foundation of Linux system administration and daily operations. Linux commands are powerful tools that allow users to perform various tasks within the operating system efficiently.
As a beginner in the Linux world, understanding basic commands is crucial for navigating the system, managing files, and performing administrative tasks. These commands serve as building blocks for more advanced operations and scripting techniques that you will learn as you progress in your Linux journey.
In this lab, you will learn how to use fundamental Linux commands, access documentation, and understand their various options. The skills you acquire here will be transferable to any Linux distribution and will prepare you for more advanced Linux operations in the future.
Basic Linux Commands
In this step, you will learn about some of the most essential Linux commands that help you navigate and interact with the system.
The echo Command
The echo command is used to display text or variables on the terminal. It's a simple but useful command for showing information or testing other commands.
Let's create a simple script that uses the echo command:
- Navigate to the project directory (if you're not already there):
cd ~/project
- Create a file named
hello.shusing theechocommand:
echo 'echo "Hello, Linux World!"' > ~/project/hello.sh
- Make the script executable:
chmod +x ~/project/hello.sh
- Run your script:
bash ~/project/hello.sh
You should see the following output:
Hello, Linux World!
The pwd Command
The pwd (Print Working Directory) command displays the full path of your current directory.
Try it now:
pwd
You should see output similar to:
/home/labex/project
This confirms that you are in the project directory within your home directory.
The ls Command
The ls command lists the contents of a directory. Let's try it:
ls
You should see hello.sh in the listing of files in your current directory.
To see more details, you can use ls with the -l option (long format):
ls -l
The output will show more information about each file, including permissions, owner, size, and date modified:
total 4
-rwxr-xr-x 1 labex labex 28 [date] hello.sh
Understanding Linux Documentation
Linux provides comprehensive documentation through the manual pages, commonly referred to as "man pages." These pages contain detailed information about commands, their options, and how to use them.
The man Command
The man command displays the manual page for a specified command. This is extremely useful when you need to understand how a command works or what options are available.
Let's explore the manual page for the echo command:
man echo
A manual page will open in your terminal. You can use the following keys to navigate:
- Press
SpaceorPage Downto scroll down - Press
borPage Upto scroll up - Press
/followed by a search term to search for specific information - Press
nto go to the next search match - Press
qto quit the manual page and return to the terminal
Read through the manual page to understand the various options available for the echo command.
When you're finished exploring, press q to exit the manual page.
Using Command Options
Based on what you've learned from the man page, let's try using the -n option with echo. This option prevents the command from adding a newline character at the end of the output.
- Create a file to document what you've learned:
cd ~/project
- Create a file that explains the
-noption:
echo "The -n option for echo prevents adding a newline at the end of the output" > ~/project/echo-options.txt
- Let's demonstrate the difference between using
echowith and without the-noption:
## Without -n option
echo "First line"
echo "Second line"
## With -n option
echo -n "First line (no newline): "
echo "Second line"
You should see the following output:
First line
Second line
First line (no newline): Second line
Notice how the second example shows both outputs on the same line because the -n option prevented the newline character.
- View the content of your documentation file:
cat ~/project/echo-options.txt
You should see:
The -n option for echo prevents adding a newline at the end of the output
Working with Files and Directories
In this step, you will learn essential commands for managing files and directories in Linux.
Creating and Navigating Directories
The mkdir command creates new directories, and the cd command allows you to change directories.
- Let's create a new directory for our exercises:
cd ~/project
mkdir linux_practice
- Navigate into the new directory:
cd linux_practice
- Confirm you're in the correct directory:
pwd
You should see:
/home/labex/project/linux_practice
Creating and Viewing Files
Now let's work with some files in this directory.
- Create a simple text file using the
echocommand and output redirection:
echo "Linux is a powerful operating system." > about_linux.txt
- Let's add another line to the file using the append redirection operator (
>>):
echo "It is open-source and widely used in servers and embedded systems." >> about_linux.txt
- View the content of the file using the
catcommand:
cat about_linux.txt
You should see:
Linux is a powerful operating system.
It is open-source and widely used in servers and embedded systems.
Copying and Moving Files
The cp command copies files, and the mv command moves or renames files.
- Let's make a copy of our file:
cp about_linux.txt linux_copy.txt
- Verify both files exist:
ls
You should see:
about_linux.txt linux_copy.txt
- Now let's rename the copy:
mv linux_copy.txt linux_benefits.txt
- Modify the content of the renamed file:
echo "Benefits of Linux include security, stability, and flexibility." > linux_benefits.txt
- Verify the content of both files:
echo "Content of about_linux.txt:"
cat about_linux.txt
echo -e "\nContent of linux_benefits.txt:"
cat linux_benefits.txt
You should see:
Content of about_linux.txt:
Linux is a powerful operating system.
It is open-source and widely used in servers and embedded systems.
Content of linux_benefits.txt:
Benefits of Linux include security, stability, and flexibility.
- Return to the project directory:
cd ~/project
Working with Text Processing Commands
Linux provides powerful commands for processing and manipulating text files. In this step, you'll learn about some basic text processing commands.
The grep Command
The grep command searches for specific patterns in files or command outputs. It's very useful for finding information in large files.
- First, let's create a file with multiple lines to practice with:
cd ~/project
echo -e "Apple\nBanana\nCherry\nDate\nEggplant\nFig\nGrape" > fruits.txt
- Use
grepto find a specific pattern:
grep "Apple" fruits.txt
You should see:
Apple
- You can also use grep with the
-ioption to perform case-insensitive searches:
grep -i "apple" fruits.txt
This will also display:
Apple
The wc Command
The wc (word count) command counts lines, words, and characters in a file.
- Let's count the elements in our fruits file:
wc fruits.txt
You should see output similar to:
7 7 42 fruits.txt
This means the file contains 7 lines, 7 words, and 42 characters.
- You can also use specific options to count only what you're interested in:
## Count lines only
wc -l fruits.txt
## Count words only
wc -w fruits.txt
## Count characters only
wc -c fruits.txt
The sort Command
The sort command arranges text lines in alphabetical or numerical order.
- Let's sort our fruits file alphabetically:
sort fruits.txt
You should see:
Apple
Banana
Cherry
Date
Eggplant
Fig
Grape
- To sort in reverse order, use the
-roption:
sort -r fruits.txt
You should see:
Grape
Fig
Eggplant
Date
Cherry
Banana
Apple
- Create a file summarizing what you've learned about these text processing commands:
echo "grep: Searches for patterns in files" > ~/project/text_commands.txt
echo "wc: Counts lines, words, and characters in files" >> ~/project/text_commands.txt
echo "sort: Arranges text lines in alphabetical or numerical order" >> ~/project/text_commands.txt
- View your summary:
cat ~/project/text_commands.txt
You should see:
grep: Searches for patterns in files
wc: Counts lines, words, and characters in files
sort: Arranges text lines in alphabetical or numerical order
Summary
In this lab, you have learned the fundamentals of Linux commands that are essential for anyone starting their journey with Linux systems. You practiced using basic commands to interact with the terminal, navigate the file system, and process text files.
Here's a summary of what you learned:
Basic Commands:
echo: Displaying text on the terminalpwd: Determining your current directory locationls: Listing directory contents
Documentation:
- Using
manpages to learn about commands and their options - Understanding command options like
echo -n
- Using
File and Directory Operations:
mkdir: Creating directoriescd: Changing directories- Creating files with
echoand redirection (>and>>) cat: Viewing file contentscp: Copying filesmv: Moving and renaming files
Text Processing:
grep: Searching for patterns in fileswc: Counting lines, words, and characterssort: Ordering text alphabetically or numerically
These commands form the foundation of Linux system administration and everyday usage. As you continue your Linux journey, you will discover how these basic commands can be combined and extended to perform more complex operations and automate tasks through shell scripting.
Remember that practice is key to mastering Linux commands. Try using these commands regularly in different contexts to reinforce your learning and develop your skills further.



