bash Command in Linux

Introduction

In this practical lab, delve into the world of Linux system administration and master essential file system navigation skills. This tutorial will guide you through the use of fundamental Linux commands, including ls, cd, pwd, and mkdir, enabling you to explore and manage your Linux environment effectively. You'll also learn how to manipulate files and directories using commands such as touch, cp, and rm. Furthermore, discover the power of Linux utilities like grep and find for efficient data searching and filtering. This hands-on lab is designed to equip you with a solid foundation in Linux scripting and programming, crucial for any aspiring systemadmin.

This lab covers the following key topics:

  • Mastering File System Navigation with Core Linux Commands
  • Effectively Manipulating Files and Directories using Linux Commands
  • Leveraging Linux Utilities for Advanced Data Searching and Filtering

Navigating the File System with Basic Linux Commands

This section focuses on teaching you how to navigate the Linux file system with ease using essential commands. We will cover the core commands ls, cd, pwd, and mkdir to help you confidently explore and manage your directories and files within the Linux operating system.

First, let's determine your current working directory by utilizing the pwd command:

pwd

Example output:

/home/labex/project

As the output shows, your default working directory is ~/project. Now, let's view the contents of the current directory using the ls command:

ls

Example output:

file1.txt  file2.txt  subdirectory

To change your current directory, use the cd command. Let's move into the subdirectory:

cd subdirectory

Now, let's list the files and directories within the subdirectory:

ls

Example output:

file3.txt  file4.txt

To navigate back to the parent directory, use the cd .. command:

cd ..

Let's create a new directory named newdir using the mkdir command:

mkdir newdir

Now, let's list the contents of the current directory once again:

ls

Example output:

file1.txt  file2.txt  newdir  subdirectory

You've now gained the knowledge to effectively navigate the Linux file system using commands such as ls, cd, pwd, and mkdir. These are foundational skills for any Linux user or system administrator.

Manipulating Files and Directories using Linux Commands

This section will teach you how to expertly manipulate files and directories using essential Linux commands, a crucial skillset for any systemadmin or Linux enthusiast.

Let's begin by creating a new file named newfile.txt using the touch command:

touch newfile.txt

Now, let's list the contents of the current directory to confirm the successful file creation:

ls

Example output:

file1.txt  file2.txt  newdir  newfile.txt  subdirectory

To duplicate a file, utilize the cp command. Let's create a copy of newfile.txt and name the copy copyfile.txt:

cp newfile.txt copyfile.txt

Let's verify the successful creation of the copy:

ls

Example output:

file1.txt  copyfile.txt  file2.txt  newdir  newfile.txt  subdirectory

To relocate a file, use the mv command. Let's move copyfile.txt into the newdir directory:

mv copyfile.txt newdir/

Now, let's examine the contents of the newdir directory:

ls newdir

Example output:

copyfile.txt

To remove a file, employ the rm command. Let's remove the newfile.txt file:

rm newfile.txt

Verify that the file has been successfully deleted:

ls

Example output:

file1.txt  file2.txt  newdir  subdirectory

Finally, let's create a new directory called newdir2 and then remove it using the rmdir command:

mkdir newdir2
ls

Example output:

file1.txt  file2.txt  newdir  newdir2  subdirectory
rmdir newdir2
ls

Example output:

file1.txt  file2.txt  newdir  subdirectory

You have now mastered the techniques for manipulating files and directories using commands like touch, cp, mv, rm, and rmdir. These are core competencies for any systemadmin working with Linux.

Searching and Filtering Data with Linux Utilities

In this segment, you'll discover how to effectively search and filter data using powerful Linux utilities like grep, find, and awk. These tools are indispensable for any Linux systemadmin for efficiently managing and analyzing data.

Let's begin by creating a sample text file named data.txt with some content:

cat > data.txt << EOF
John,25,male
Jane,30,female
Bob,35,male
Alice,28,female
EOF

Now, let's search for lines containing the word "male" in the data.txt file using the grep command:

grep "male" data.txt

Example output:

John,25,male
Bob,35,male

To search for lines containing the word "female", we can use:

grep "female" data.txt

Example output:

Jane,30,female
Alice,28,female

You can also leverage the find command to locate files. For instance, to find all files within the current directory and its subdirectories, you can use:

find .

Example output:

.
./data.txt
./newdir
./newdir/copyfile.txt
./file1.txt
./file2.txt
./subdirectory
./subdirectory/file3.txt
./subdirectory/file4.txt

To search for a specific file, use the -name option:

find . -name "data.txt"

Example output:

./data.txt

Finally, let's utilize the awk command to extract specific fields from the data.txt file. For example, to print the second field (age) for each line:

awk -F',' '{print $2}' data.txt

Example output:

25
30
35
28

You've now learned how to effectively use grep, find, and awk to search, filter, and manipulate data within your Linux environment. These skills are crucial for efficient log analysis, system monitoring, and general systemadmin tasks, even when operating as the root user.

Summary

This lab has provided you with the foundational skills to navigate the Linux file system effectively. You have learned to use basic commands like ls, cd, pwd, and mkdir to explore directories, list their contents, and create new ones. Furthermore, you gained hands-on experience manipulating files and directories using commands such as touch, cp, and ls. These fundamental skills are essential for anyone working in a Linux environment, especially system administrators needing to efficiently manage files and directories on servers and workstations.

400+ Linux Commands