What is command grouping in Linux and how to use it ?

Do you know how to group multiple commands together and redirect their output collectively to a file ?

It is a very small tip, however many of us dont know what is command grouping in Linux and how to use it.

Command Grouping and Redirection to a file

### Grouping two command: date and uptime
### Redirecting their output in one go to a file called hello.txt
$ (date; uptime) > hello.txt
$ cat hello.txt 
Sun Jul 29 23:12:38 PDT 2018
23:12  up 10 days,  4:30, 5 users, load averages: 1.26 1.23 1.25

$ date
Sun Jul 29 23:12:46 PDT 2018
$ uptime
23:12  up 10 days,  4:30, 5 users, load averages: 1.37 1.26 1.26
$ 

Usage of Command Grouping
If you have observed above if we redirect without command grouping we need to redirect each command output individually to a file.

With command grouping we can redirect all commands output of the group to a single file in one go.

In a similar fashion, we can use command grouping in shell scripts to automate our tasks.

Leave a Reply

Your email address will not be published.