Introduction
In this lab, you will learn how to navigate the Linux file system using basic commands like ls, cd, pwd, and mkdir, as well as manipulate files and directories with commands such as touch, cp, and rm. You will also explore how to search and filter data using Linux utilities like grep and find. This lab aims to provide you with a practical understanding of essential Linux scripting and programming skills.
The lab covers the following topics:
- Navigating the File System with Basic Linux Commands
- Manipulating Files and Directories using Linux Commands
- Searching and Filtering Data with Linux Utilities
Navigating the File System with Basic Linux Commands
In this step, you will learn how to navigate the Linux file system using basic commands. We will cover essential commands like ls, cd, pwd, and mkdir to explore and manage directories and files.
First, let's check our current working directory using the pwd command:
pwd
Example output:
/home/labex/project
As you can see, the default working directory is ~/project. Now, let's list the contents of the current directory using the ls command:
ls
Example output:
file1.txt file2.txt subdirectory
To change the current directory, we can use the cd command. Let's navigate to the subdirectory:
cd subdirectory
Now, let's list the contents of the subdirectory:
ls
Example output:
file3.txt file4.txt
To go back to the parent directory, we can use cd ..:
cd ..
Let's create a new directory called newdir using the mkdir command:
mkdir newdir
Now, let's list the contents of the current directory again:
ls
Example output:
file1.txt file2.txt newdir subdirectory
You have now learned how to navigate the Linux file system using basic commands like ls, cd, pwd, and mkdir.
Manipulating Files and Directories using Linux Commands
In this step, you will learn how to manipulate files and directories using essential Linux commands.
Let's start by creating a new file called newfile.txt using the touch command:
touch newfile.txt
Now, let's list the contents of the current directory to verify the file creation:
ls
Example output:
file1.txt file2.txt newdir newfile.txt subdirectory
To copy a file, we can use the cp command. Let's make a copy of newfile.txt and name it copyfile.txt:
cp newfile.txt copyfile.txt
Let's verify the copy:
ls
Example output:
file1.txt copyfile.txt file2.txt newdir newfile.txt subdirectory
To move a file, we can use the mv command. Let's move copyfile.txt to the newdir directory:
mv copyfile.txt newdir/
Now, let's check the contents of the newdir directory:
ls newdir
Example output:
copyfile.txt
To delete a file, we can use the rm command. Let's delete the newfile.txt file:
rm newfile.txt
Verify the file deletion:
ls
Example output:
file1.txt file2.txt newdir subdirectory
Finally, let's create a new directory called newdir2 and then delete 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 learned how to manipulate files and directories using commands like touch, cp, mv, rm, and rmdir.
Searching and Filtering Data with Linux Utilities
In this step, you will learn how to search and filter data using powerful Linux utilities such as grep, find, and awk.
Let's start by creating a sample text file called 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 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 that contain the word "female", we can use:
grep "female" data.txt
Example output:
Jane,30,female
Alice,28,female
You can also use the find command to search for files. For example, to find all files in 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, you can use the -name option:
find . -name "data.txt"
Example output:
./data.txt
Finally, let's use 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 have now learned how to use grep, find, and awk to search and filter data in Linux.
Summary
In this lab, you learned how to navigate the Linux file system using basic commands such as ls, cd, pwd, and mkdir. You explored directories, listed their contents, and created new directories. Additionally, you learned how to manipulate files and directories using commands like touch, cp, and ls. These fundamental skills are essential for effectively working with files and directories in a Linux environment.



