Linux skill Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to manage files and directories using basic Linux commands, as well as understand and use redirection and pipes in the Linux terminal. You will also explore automating repetitive tasks with shell scripting. These skills are essential for effective process management in a Linux environment. The lab covers practical examples and hands-on exercises to help you develop a solid understanding of these Linux utilities and techniques.

The lab starts by introducing you to essential file and directory management commands, such as ls, cd, mkdir, touch, rm, and mv. You will learn how to navigate the file system, create and delete files and directories, and move files between directories. The second part of the lab focuses on redirection and pipes, which allow you to control the input and output of commands and chain them together for more complex operations. Finally, you will explore shell scripting, a powerful tool for automating repetitive tasks and streamlining your workflow.

Linux Commands Cheat Sheet

Manage Files and Directories with Basic Linux Commands

In this step, you will learn how to manage files and directories using basic Linux commands. We will cover commands such as ls, cd, mkdir, touch, rm, and mv.

First, let's check the current working directory:

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:

ls

Example output:

file1.txt  file2.txt  directory1

To create a new directory, use the mkdir command:

mkdir directory2

You can now see the new directory in the list:

ls

Example output:

file1.txt  file2.txt  directory1  directory2

To create a new file, use the touch command:

touch file3.txt

You can verify the file was created by listing the directory contents again:

ls

Example output:

file1.txt  file2.txt  file3.txt  directory1  directory2

To move a file, use the mv command:

mv file3.txt directory1

Now, the file3.txt file has been moved to the directory1 subdirectory:

ls
ls directory1

Example output:

file1.txt  file2.txt  directory1  directory2
file3.txt

Finally, to delete a file, use the rm command:

rm file1.txt

Verify the file has been deleted:

ls

Example output:

file2.txt  directory1  directory2

Great! You have now learned how to manage files and directories using basic Linux commands.

Understand and Use Redirection and Pipes in the Linux Terminal

In this step, you will learn how to use redirection and pipes in the Linux terminal. Redirection allows you to redirect the input or output of a command, while pipes allow you to chain multiple commands together.

Let's start by creating a sample text file:

echo "This is line 1" > file1.txt
echo "This is line 2" >> file1.txt

Now, let's display the contents of the file using the cat command:

cat file1.txt

Example output:

This is line 1
This is line 2

You can also use redirection to redirect the output of a command to a file:

ls > file_list.txt

Verify the file was created and contains the list of files:

cat file_list.txt

Example output:

file1.txt
file_list.txt

Next, let's use pipes to chain multiple commands together. For example, we can use the grep command to search for a specific word in the file:

cat file1.txt | grep "line"

Example output:

This is line 1
This is line 2

You can also use pipes to filter the output of one command and pass it as input to another command. For example, let's list all the files in the current directory and filter the output to only show directories:

ls | grep directory

Example output:

directory1
directory2

Great! You have now learned how to use redirection and pipes in the Linux terminal.

Automate Repetitive Tasks with Shell Scripting

In this step, you will learn how to automate repetitive tasks using shell scripting. We will create a simple script to perform common file and directory operations.

First, let's create a new directory and navigate to it:

mkdir scripts
cd scripts

Now, let's create a new shell script file using the nano text editor:

nano file_operations.sh

In the editor, add the following content:

#!/bin/bash

## Create a new directory
mkdir new_directory

## Create a new file
touch new_file.txt

## List the contents of the current directory
ls -l

Save the file and exit the editor.

Make the script executable:

chmod +x file_operations.sh

Now, you can run the script:

./file_operations.sh

Example output:

total 0
drwxrwxr-x 2 labex labex 4096 Apr 18 12:34 new_directory
-rw-rw-r-- 1 labex labex    0 Apr 18 12:34 new_file.txt

As you can see, the script creates a new directory, a new file, and lists the contents of the current directory.

You can further enhance the script by adding more functionality, such as accepting user input, performing conditional operations, or even calling other commands and scripts.

Summary

In this lab, you learned how to manage files and directories using basic Linux commands such as ls, cd, mkdir, touch, rm, and mv. You also explored the use of redirection and pipes in the Linux terminal, allowing you to redirect the input or output of commands and chain multiple commands together. Finally, you gained hands-on experience with shell scripting, automating repetitive tasks and leveraging the power of the Linux command line.

The lab provided a comprehensive overview of essential Linux skills, equipping you with the knowledge and tools to effectively navigate and manipulate the Linux environment. By mastering these fundamental concepts, you have laid a strong foundation for further exploration and proficiency in Linux system administration and development.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like