[ad_1]
Some of the most easy “tricks” on Linux rely on the use of a handful of distinctive people. This submit will take a appear at a variety of them and reveals how they perform.
Working with > and >>
Employing the > and >> figures will have equivalent but distinctive results, and both depend on how you use them in a command. The > character can be utilised to direct output into a file. For example, these commands will place the specified text into a file. If the file exists, having said that, any former information will be overwritten. Observe how only a person “hi there” remains in the file.
$ echo good day > globe $ echo howdy > entire world $ cat earth good day
Using >>, on the other hand, will increase the textual content furnished to the finish of a file. If the file doesn’t exist, the command will make it.
$ echo "My Report" > report $ day >> report $ cat report My Report Sat Jul 8 11:49:48 AM EDT 2023
The commands under will vacant a file of its contents. Instructions like this are generally used to periodically vacant data files. with out altering file permissions or ownership.
Both commands demonstrated below have the similar influence, so many Linux users prefer the second one particular just to help save some typing
$ cat /dev/null > bigfile $ > bigfile
Here’s an case in point:
$ 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 Jul 8 11:51 bigfile
Making use of &
The & character is made use of to operate a command in the background, allowing the person to shift onto other tasks even though the command operates to completion. Here’s an case in point of its use:
$ bigjob &
You can analyze backgrounded duties using the work command.
$ work [1]+ Working bigjob & $ fg %1 bigjob
You can also send a managing process to the track record by using ^z and then the bg command.
$ bigjob ^Z [1]+ Stopped bigjob $ bg [1]+ bigjob &
You can also bring backgrounded work opportunities back again into the foreground employing the fg command. Here’s an illustration:
$ bigjob & [1] 4092 $ jobs [1]+ Operating bigjob & $ fg %1 bigjob
Using && and ||
The && and || characters enjoy unique roles when commands count on the success or failure of prior commands.
The && people will make certain that the command on the appropriate of it will be run if the command on the still left of it succeeds and ensures that the 2nd command isn’t run if the very first command fails. Think of this as one thing of a “if good results, then continue” command or an “and” operator.
Here’s an case in point:
$ ping 192.168..1 && echo router is reachable router is reachable
The ping command in the above instance was plainly successful.
The || people have the opposite result. If the initial command is effective, the second will not be run. In other words and phrases, only 1 of the commands will be run. You can think of it as a thing of an “if” operator – if not the to start with command, then the 2nd.
In the case in point under, 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 occur in really useful whenever you’re operating on the Linux command line. An earlier publish on &&, ||, and ! is obtainable at Demystifying &&, !! and ! on Linux
Copyright © 2023 IDG Communications, Inc.
[ad_2]
Source hyperlink