Linux Logical Commands and Redirection

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn Linux logical commands (&&, ||, ;, !), redirect command (>, >>), and pipeline commands (|). These commands are some of the most commonly used in Linux and are essential for any programmer or system administrator to know. The lab will be step-by-step and will include code examples that will range from simple to complex.

Achievements

  • logical commands (&&, ||, ;, !)
  • redirect command (>, >>)
  • pipeline commands (|)

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/InputandOutputRedirectionGroup(["`Input and Output Redirection`"]) linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/AdvancedScriptingConceptsGroup(["`Advanced Scripting Concepts`"]) shell(("`Shell`")) -.-> shell/SystemInteractionandConfigurationGroup(["`System Interaction and Configuration`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicFileOperationsGroup -.-> linux/head("`File Beginning Display`") linux/BasicFileOperationsGroup -.-> linux/tail("`File End Display`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/InputandOutputRedirectionGroup -.-> linux/pipeline("`Data Piping`") linux/InputandOutputRedirectionGroup -.-> linux/redirect("`I/O Redirecting`") linux/BasicSystemCommandsGroup -.-> linux/logical("`Logic Operations`") linux/BasicSystemCommandsGroup -.-> linux/test("`Condition Testing`") linux/TextProcessingGroup -.-> linux/grep("`Pattern Searching`") linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/FileandDirectoryManagementGroup -.-> linux/mkdir("`Directory Creating`") linux/UserandGroupManagementGroup -.-> linux/passwd("`Password Changing`") shell/BasicSyntaxandStructureGroup -.-> shell/quoting("`Quoting Mechanisms`") shell/AdvancedScriptingConceptsGroup -.-> shell/adv_redirection("`Advanced Redirection`") shell/SystemInteractionandConfigurationGroup -.-> shell/exit_status_checks("`Exit Status Checks`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") linux/FileandDirectoryManagementGroup -.-> linux/wildcard("`Wildcard Character`") subgraph Lab Skills linux/cat -.-> lab-48{{"`Linux Logical Commands and Redirection`"}} linux/head -.-> lab-48{{"`Linux Logical Commands and Redirection`"}} linux/tail -.-> lab-48{{"`Linux Logical Commands and Redirection`"}} linux/echo -.-> lab-48{{"`Linux Logical Commands and Redirection`"}} linux/pipeline -.-> lab-48{{"`Linux Logical Commands and Redirection`"}} linux/redirect -.-> lab-48{{"`Linux Logical Commands and Redirection`"}} linux/logical -.-> lab-48{{"`Linux Logical Commands and Redirection`"}} linux/test -.-> lab-48{{"`Linux Logical Commands and Redirection`"}} linux/grep -.-> lab-48{{"`Linux Logical Commands and Redirection`"}} linux/cd -.-> lab-48{{"`Linux Logical Commands and Redirection`"}} linux/mkdir -.-> lab-48{{"`Linux Logical Commands and Redirection`"}} linux/passwd -.-> lab-48{{"`Linux Logical Commands and Redirection`"}} shell/quoting -.-> lab-48{{"`Linux Logical Commands and Redirection`"}} shell/adv_redirection -.-> lab-48{{"`Linux Logical Commands and Redirection`"}} shell/exit_status_checks -.-> lab-48{{"`Linux Logical Commands and Redirection`"}} shell/globbing_expansion -.-> lab-48{{"`Linux Logical Commands and Redirection`"}} linux/wildcard -.-> lab-48{{"`Linux Logical Commands and Redirection`"}} end

Check Whether the File Exists or Not

The test command in Linux is used to evaluate expressions and determine if they are true or false. It is commonly used in shell scripts and conditional statements to check the status of various elements, such as files, directories, and variables.

The test command can be used in several ways. For example, check whether the file file.txt exists or not:

test -e file.txt

It is important to note that the test command will not print true or false. Instead, it will return 0 if the expression is true and 1 if the expression is false. If you want to check whether the file exists or not, you can use the following command:

echo $?

$? is a special variable that stores the exit status of the last command. If the file exists, the command will return 0. Otherwise, it will return 1.

Check Whether the File Is a Regular File or Not

If the file is a regular file, the test command returns true. Otherwise, it returns false.

test -f file.txt

Check Whether the Directory Exists or Not

If the directory exists, the test command returns true. Otherwise, it returns false.

test -d Code

Logical Command &&

The && operator allows you to execute the command on the right only if the command on the left is successful. For example, the following command will only display the contents of the file file.txt if the file exists:

test -e file.txt && cat file.txt

This command will return 1 because the file file.txt does not exist. If you create the file and run the command again, it will return 0 and display the contents of the file.

Logical Command ||

The || operator allows you to execute the command on the right only if the command on the left is not successful. For example, the following command will display an error message if the file test.txt does not exist:

test -e test.txt || echo "test.txt does not exist"

This command will return 0 and print the error message, even though the file test.txt does not exist.

Logical Command !

The ! operator allows you to execute the command on the right only if the command on the left is not successful. For example:

! test -e test.txt

This command will returns 0 even though the file does not exist.

Logical Command ;

The ; operator allows you to execute multiple commands in sequence. For example, the following command will create a new directory and then change into it:

mkdir new_directory
cd new_directory

Override redirection

The > operator allows you to redirect the output of a command to a file. For example, the following command will redirect the output of the echo command to a file called output.txt:

echo "one" > output.txt
echo "two" > output.txt
echo "three" > output.txt

Now, the file output.txt will contain the following:

three

Additional Redirection

The > command will overwrite the contents of the file output.txt with the output of the echo command. If you want to append the output of the echo command to the file output.txt, you can use the >> operator:

echo "one" >> output2.txt
echo "two" >> output2.txt
echo "three" >> output2.txt

Now, the file output2.txt will contain the following:

one
two
three

Pipeline Operator

A pipeline in Linux allows you to chain multiple commands together so that the output of one command is used as the input for the next command. The pipeline operator is |.

For example, the following command will display the contents of the file /etc/passwd, but only the lines that contain the word "labex":

cat /etc/passwd | grep labex

Multiple Pipeline Operator

You can chain multiple commands together to form more complex pipelines in a single line. For example, the following command will display the fourth and fifth lines of the /etc/passwd file:

cat /etc/passwd | head -n 5 | tail -n 2

Summary

In this lab, you have learned how to use Linux logical commands, the redirect command, and pipeline commands to create more complex and powerful commands. These commands are essential tools for any programmer or system administrator, and mastering them will greatly enhance your ability to work with Linux systems.

Other Linux Tutorials you may like