The >, >>, &, && and || characters are extremely useful whenever you're working on the Linux command line. 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 Read more Linux tips: Bash scripting tips that can save time on Linux Many ways to use the echo command on Linux Using aliases on Linux Using the script command on Linux to record command line activity Using the Linux apropos command – even if you have to fix it first Related content how-to Getting started on the Linux (or Unix) command line, Part 4 Pipes, aliases and scripts make Linux so much easier to use. By Sandra Henry-Stocker Nov 27, 2023 4 mins Linux how-to Getting started on the Linux (or Unix) command line, Part 3 Our Linux cheat sheet includes some of the most commonly used commands along with brief explanations and examples of what the commands can do. By Sandra Henry Stocker Nov 21, 2023 6 mins Linux how-to Getting started on the Linux (or Unix) command line, Part 2 Commands that provide help are essential. Here's a look at some of the help you can get from the Linux system itself. By Sandra Henry Stocker Nov 20, 2023 5 mins Linux how-to Getting started on the Linux (or Unix) command line, Part 1 This series of posts will help Linux/Unix newbies to feel comfortable on the command line. By Sandra Henry Stocker Nov 16, 2023 8 mins Linux Podcasts Videos Resources Events NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe