Tuesday, July 11, 2023
HomeNetworkingThe facility of >, >>, &, &&, and || on Linux

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


A few of the most handy “tips” on Linux depend upon using a handful of particular characters. This put up takes a have a look at quite a lot of them and exhibits how they work.

Utilizing > and >>

Utilizing the > and >> characters could have related however completely different results, and each depend upon how you utilize them in a command. The > character can be utilized to direct output right into a file. For instance, these instructions will put the required textual content right into a file. If the file exists, nevertheless, any former content material can be overwritten. Discover how just one “hi there” stays within the file.

$ echo hi there > world
$ echo hi there > world
$ cat world
hi there

Utilizing >>, however, will add the textual content supplied to the top 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 instructions under will empty a file of its contents. Instructions like this are sometimes used to periodically empty information. with out altering file permissions or possession.

Each instructions proven under have the identical impact, so many Linux customers favor the second simply to avoid wasting typing

$ cat /dev/null > bigfile
$ > bigfile

Right here’s an instance:

$ 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

Utilizing &

The & character is used to run a command within the background, permitting the consumer to maneuver onto different duties whereas the command runs to completion. Right here’s an instance of its use:

$ bigjob &

You may look at backgrounded duties utilizing the jobs command.

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

You may as well ship a operating process to the background through the use of ^z after which the bg command.

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

You may as well convey backgrounded jobs again into the foreground utilizing the fg command. Right here’s an instance:

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

Utilizing && and ||

The && and || characters play particular roles when instructions depend upon the success or failure of earlier instructions.

The && characters will be certain that the command on the best of will probably be run if the command on the left of it succeeds and ensures that the second command isn’t run if the primary command fails. Consider this as one thing of a “if success, then proceed” command or an “and” operator.

Right here’s an instance:

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

The ping command within the above instance was clearly profitable.

The || characters have the alternative impact. If the primary command is profitable, the second will not be run. In different phrases, solely one of many instructions can be run. You may consider it as one thing of an “if” operator – if not the primary command, then the second.

Within the  instance under, the scripts listing didn’t 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 are available very helpful everytime you’re engaged on the Linux command line. An earlier put up on &&, ||, and ! is offered at Demystifying &&, !! and ! on Linux

Copyright © 2023 IDG Communications, Inc.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments