Combine and Execute Multiple Linux Commands


Linux is a powerful and versatile operating system that has gained immense popularity in computing world. One of most useful features of Linux is ability to combine and execute multiple commands at once, which can significantly improve your productivity and efficiency.

In this article, we will discuss various techniques for combining and executing multiple Linux commands, along with examples that demonstrate their practical applications.

Combining Commands with Pipes

The most common way to combine Linux commands is by using pipes. A pipe is a feature that allows output of one command to be passed as input to another command. syntax for using a pipe is as follows −

command1 | command2

In this example, output of command1 is passed as input to command2. Let's look at some practical examples of using pipes.

Example 1: List all files in a directory and display first 10 lines of output

The following command lists all files in current directory and passes output to head command, which displays first 10 lines of output.

ls | head

Example 2: Find all files in a directory that contain a specific keyword and display line numbers where keyword appears

The following command searches for all files in current directory that contain keyword "example" and passes output to grep command, which displays line numbers where keyword appears.

grep -rn "example" . | awk -F ":" '{print $1, $2}'

In this example, we use -rn option with grep command to search for all files in current directory that contain keyword "example" and display line numbers where keyword appears. We then pass output to awk command, which separates output into two columns based on delimiter ":" and displays line number and filename.

Combining Commands with Semicolons

Another way to combine commands in Linux is by using semicolons. A semicolon allows you to execute multiple commands on a single line, separated by semicolons. syntax for using semicolons is as follows −

command1 ; command2 ; command3

In this example, all three commands will be executed in sequence. Let's look at some practical examples of using semicolons.

Example 1: Create a new directory, change to directory, and list all files

The following command creates a new directory called "example" using mkdir command, changes to new directory using cd command, and lists all files in directory using ls command.

mkdir example ; cd example ; ls

Example 2: Remove all files in a directory and then remove directory

The following command removes all files in current directory using rm command with wildcard symbol (*) to remove all files. It then removes directory using rmdir command.

rm * ; rmdir example

Combining Commands with Braces

Braces allow you to group commands together and treat them as a single command. syntax for using braces is as follows −

{ command1 ; command2 ; command3 ; }

In this example, all three commands will be treated as a single command. Let's look at some practical examples of using braces.

Example 1: Create a new directory and change to directory in one command

The following command creates a new directory called "example" using mkdir command and changes to new directory using cd command within braces.

{ mkdir example ; cd example ; }

Example 2: Rename all files in a directory with a specific extension

The following command renames all files in current directory with .txt extension to have a .doc extension using mv command within braces.

{ for file in *.txt; do mv "$file" "${file%.txt}.doc"; done ; }

In this example, we use a for loop to iterate over all files in current directory with .txt extension. We then use mv command within braces to rename each file by replacing .txt extension with .doc extension.

Combining Commands with && and || Operators

The && and || operators allow you to execute commands conditionally. && operator executes second command only if first command is successful, whereas || operator executes second command only if first command fails.

The syntax for using && operator is as follows −

command1 && command2

In this example, second command will only be executed if first command is successful. Let's look at an example.

Example: Create a new directory and change to directory only if creation is successful

The following command creates a new directory called "example" using mkdir command and changes to new directory using cd command only if creation of directory is successful.

mkdir example && cd example

The syntax for using || operator is as follows −

command1 || command2

In this example, second command will only be executed if first command fails. Let's look at an example.

Example: Remove a file if it exists, and display an error message if it doesn't exist

The following command removes a file called "example.txt" using rm command and displays an error message if file does not exist using echo command.

rm example.txt || echo "File does not exist"

Combining Commands with tee Command

The tee command allows you to redirect output of a command to a file and also display output on screen. syntax for using tee command is as follows −

command | tee filename

In this example, output of command is redirected to a file called "filename" and also displayed on screen. Let's look at an example.

Example: Find all files in a directory that contain a specific keyword and save output to a file and display it on screen

The following command searches for all files in current directory that contain keyword "example" using grep command and saves output to a file called "output.txt" using tee command.

grep -rn "example" . | tee output.txt

This command also displays output on screen, allowing you to view results in real-time.

Combining Commands with xargs Command

The xargs command is used to build and execute command lines from standard input. It is useful when you want to process a large number of files or arguments, which cannot be passed directly to command. syntax for using xargs command is as follows −

command | xargs [options] command

In this example, command takes input from standard input and passes it to xargs command, which then passes it to next command. Let's look at an example.

Example: Delete all files in a directory that are older than a specific date

The following command finds all files in current directory that are older than date "20220101" using find command and passes them to xargs command, which then executes rm command to delete files.

find . -type f -mtime +365 | xargs rm

In this command, find command searches for all files in current directory that are older than 365 days (-mtime +365) and passes them to xargs command, which executes rm command to delete files.

Combining Commands with awk Command

The awk command is a versatile tool used for manipulating and processing text data. It can be used to extract specific fields, perform calculations, and even modify files. syntax for using awk command is as follows −

awk [options] 'pattern { action }' file

In this example, pattern specifies conditions to be met, and action specifies action to be taken when pattern is matched. Let's look at an example.

Example: Print second field of a comma-separated file

The following command prints second field of a comma-separated file called "data.csv" using awk command.

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

In this command, -F option specifies field separator as a comma (',') and action {print $2} prints second field of each line.

Combining Commands with sed Command

The sed command is used for stream editing of text files. It can be used to perform search and replace operations, delete lines, and even insert or append text. syntax for using sed command is as follows −

sed [options] 'expression' file

In this example, expression specifies operations to be performed on file. Let's look at an example.

Example: Replace all occurrences of a word in a file

The following command replaces all occurrences of word "example" with word "sample" in a file called "file.txt" using sed command.

sed 's/example/sample/g' file.txt

In this command, s operator stands for substitution, and g option stands for global (replacing all occurrences). expression 's/example/sample/g' specifies substitution to be performed.

Conclusion

In this article, we discussed various techniques for combining and executing multiple Linux commands. Using pipes, semicolons, braces, && and || operators, and tee command, you can perform complex tasks with ease and increase your productivity and efficiency.

It is essential to understand syntax and practical applications of these techniques to make most of your Linux system. With practice and experimentation, you can master art of combining and executing multiple Linux commands and become a proficient Linux user.

Updated on: 23-Mar-2023

510 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements