Photo from Pexels by Lord Welhim Esaga
After learning my first dozen Linux commands or so, in the back of my mind, I would wonder if there were easier, faster, or better ways to run commands.
Instead of running two commands one after the other, could I combine them somehow? Was there a way to see only part of a command’s output?
After I did more research and got more experience, I began to figure out how to combine certain commands to get the results I wanted.
OVERVIEW
In this article, we’ll go over some common command combinations that will serve as tools for you to use at your disposal as you make your own command combos.
NOTE: System Specifics
All the following commands were tested on a Ubuntu 20.04 VM. If you are using a different system, your results may vary.
Outline
- In-line Execution
- Using “;”
- Using “&&”
- Piping
- Using “|“
- The “grep” Command
- The “tee” Command
- Redirects
- Using “>”
- Using “>>”
Prerequisites
- 3 Quick Ways Rookies Can Learn Linux Commands Strictly From The CLI
IN-LINE EXEUTION
These next sections are about stringing two, or more, commands together. This can be useful if you find yourself running the same sequence of commands repeatedly.
Using “;”
The semicolon can be used in between two commands to execute one after the other. For example, executing the whoami
and hostname
commands within the same line:
penguin@ubuntu20 # whoami ; hostname
penguin
ubuntu20
You’ll notice that the output of the previous commands will execute and display sequentially.
NOTE: Arrow Keys
You can press the “up” and “down” arrow keys to cycle through previous commands you’ve already executed. This is incredibly helpful when running the same or similar commands repeatedly.
What if one of the commands fails? Each command will execute anyway. For example, if we execute a command we know will fail, such as cat /tmp/foobar.txt
(because /tmp/foobar.txt
doesn’t exist on this system), the second command will run anyway:
penguin@ubuntu20 # cat /tmp/foobar.txt ; hostname
cat: /tmp/foobar.txt: No such file or directory
ubuntu20
Using “&&”
Similarly, the &&
operator can be used to string two or more commands together; however, the subsequent commands only run if the previous commands were successful. For example, if we use the commands from the previous example, the output will be identical:
penguin@ubuntu20 # whoami && hostname
penguin
ubuntu20
This is because the execution of whoami
was successful (without error). However, if we first execute a command we know will fail, the second command won’t run:
penguin@ubuntu20 # cat /tmp/foobar.txt && hostname
cat: /tmp/foobar.txt: No such file or directory
Here, notice the absence of the secondary output: “ubuntu20”.
PIPING
“Piping” – i.e., using |
– is for using a previous command’s output as input for the next command. To use |
, you need to be familiar with commands that can work together.
Using “|”
Just like the previous operators, |
goes in between the two commands you’ll be running.
The “grep” Command
Let’s suppose you are running the lscpu
command, which displays many lines about your computer’s processor:
penguin@ubuntu20 $ lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
Address sizes: 36 bits physical, 48 bits virtual
CPU(s): 12
On-line CPU(s) list: 0-11
Thread(s) per core: 2
Core(s) per socket: 6
Socket(s): 1
Vendor ID: AuthenticAMD
CPU family: 23
Model: 96
…
Perhaps you only want to see the lines with “CPU” in it, you can use grep
to do so; however, it needs to be used in conjunction with |
:
penguin@ubuntu20 $ lscpu | grep CPU
CPU op-mode(s): 32-bit, 64-bit
CPU(s): 12
On-line CPU(s) list: 0-11
CPU family: 23
CPU MHz: 3000.000
CPU max MHz: 3000.0000
NOTE: Case-insensitive
If you want grep
to be case-insensitive, you can use the -i
option: lscpu | grep -i cpu
.
The “tee” Command
The tee
command can be used to place the output of a command into a file. For example, we can put the previous lspu
command’s output into a file: “lscpu_output.txt”.
penguin@ubuntu20 $ lscpu | tee lscpu_output.txt
WARNING: Create/Overwrite
While using tee
, If the file you specify does not exist, it will be created; however, if the file you specify already exists, it will be overwritten.
Then, you can check it’s contents with cat
:
penguin@ubuntu20 $ cat lscpu_output.txt
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
Address sizes: 36 bits physical, 48 bits virtual
CPU(s): 12
On-line CPU(s) list: 0-11
Thread(s) per core: 2
Core(s) per socket: 6
Socket(s): 1
Vendor ID: AuthenticAMD
CPU family: 23
Model: 96
…
You can even pipe all three of the previous commands together to get a file that only displays the “CPU” lines of lscpu
into a new file:
penguin@ubuntu20 $ lscpu | grep CPU | tee lscpu_output_grep.txt
penguin@ubuntu20 $ cat lscpu_output_grep.txt
CPU op-mode(s): 32-bit, 64-bit
CPU(s): 12
On-line CPU(s) list: 0-11
CPU family: 23
CPU MHz: 3000.000
CPU max MHz: 3000.0000
REDIRECTS
Redirect operators can place the output of a command into a file (similar to the tee
command).
Using “>”
Similar to the previous examples, you can use >
to place a command’s output into a file:
penguin@ubuntu20 $ lscpu > lscpu_output.txt
WARNING: Create/Overwrite
Just like tee
, when using >
, If the file you specify does not exist, it will be created. If the file you specify already exists, it will be overwritten.
Using “>>”
While a single >
will create/overwrite a file, >>
will append to a file. To exhibit this, we’ll first create a file with the date
command:
penguin@ubuntu20 $ date > date.txt
penguin@ubuntu20 $ cat date.txt
Mon May 10 12:01:01 CDT 2021
Now, we can add to that same file with >>
:
penguin@ubuntu20 $ date >> date.txt
penguin@ubuntu20 $ cat date.txt
Mon May 10 12:01:01 CDT 2021
Mon May 10 12:02:12 CDT 2021
Here, you can see that the original timestamp is retained while the new line is appended beneath it. You can keep executing these commands repeatedly to keep adding lines:
penguin@ubuntu20 $ date >> date.txt && cat date.txt
Mon May 10 12:01:01 CDT 2021
Mon May 10 12:02:12 CDT 2021
Mon May 10 12:03:23 CDT 2021
penguin@ubuntu20 $ date >> date.txt && cat date.txt
Mon May 10 12:01:01 CDT 2021
Mon May 10 12:02:12 CDT 2021
Mon May 10 12:03:23 CDT 2021
Mon May 10 12:04:55 CDT 2021
CONCLUSION
Now that you know how to put some commands together, you can experiment with your own combinations. For example, try printing the top ten and bottom ten lines of a large text file using the head
and tail
commands.