The power of >, >>, &, &&, and || on Linux


Some of the most convenient “tricks” on Linux depend on the use of a handful of special characters. This post takes a look at a number of them and shows how they work.

Using > and >>

Using the > and >> characters will have similar but different effects, and both depend on how you use them in a command. The > character can be used to direct output into a file. For example, these commands will put the specified text into a file. If the file exists, however, any former content will be overwritten. Notice how only one “hello” remains in the file.

$ echo hello > world
$ echo hello > world
$ cat world
hello

Using >>, on the other hand, will add the text provided to the end of a file. If the file doesn’t exist, the command will create it.

$ echo "My Report" > report
$ date >> report
$ cat report
My Report
Sat Jul  8 11:49:48 AM EDT 2023

The commands below will empty a file of its contents. Commands like this are often used to periodically empty files. without altering file permissions or ownership.

Both commands shown below have the same effect, so many Linux users prefer the second one just to save some typing

$ cat /dev/null > bigfile
$ > bigfile

Here’s an example:

$ ls -l bigfile
-rw-rw-r-- 1 shs shs 1309432546 Jul  8 11:51 bigfile
$ > bigfile
$ ls -l bigfile
-rw-rw-r-- 1 shs shs 0 Jul  8 11:51 bigfile

Using &

The & character is used to run a command in the background, allowing the user to move onto other tasks while the command runs to completion. Here’s an example of its use:

$ bigjob &

You can examine backgrounded tasks using the jobs command.

$ jobs
[1]+  Running                 bigjob &
$ fg %1
bigjob

You can also send a running task to the background by using ^z and then the bg command.

$ bigjob
^Z
[1]+  Stopped                 bigjob
$ bg
[1]+ bigjob &

You can also bring backgrounded jobs back into the foreground using the fg command. Here’s an example:

$ bigjob &
[1] 4092
$ jobs
[1]+  Running                 bigjob &
$ fg %1
bigjob

Using && and ||

The && and || characters play special roles when commands depend on the success or failure of previous commands.

The && characters will ensure that the command on the right of it will be run if the command on the left of it succeeds and ensures that the second command isn’t run if the first command fails. Think of this as something of a “if success, then continue” command or an “and” operator.

Here’s an example:

$ ping 192.168.0.1 && echo router is reachable
router is reachable

The ping command in the above example was clearly successful.

The || characters have the opposite effect. If the first command is successful, the second will not be run. In other words, only one of the commands will be run. You can think of it as something of an “if” operator – if not the first command, then the second.

In the  example below, the scripts directory did not exist, so the mkdir command was run.

$ [ -d scripts ] || mkdir scripts
$ ls -ld scripts
drwxrwxr-x 2 shs shs 4096 Jul  8 12:24 scripts

Wrap-up

The >, >>, &, &&, and || operators come in very handy whenever you’re working on the Linux command line. An earlier post on &&, ||, and ! is available at Demystifying &&, !! and ! on Linux

Copyright © 2023 IDG Communications, Inc.



Source link