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 How to find files on Linux There are many options you can use to find files on Linux, including searching by file name (or partial name), age, owner, group, size, type and inode number. By Sandra Henry Stocker Jun 24, 2024 8 mins Linux opinion Linux in your car: Red Hat’s milestone collaboration with exida With contributions from Red Hat and critical collaborators, the safety and security of automotive vehicles has reached a new level of reliability. By Sandra Henry Stocker Jun 17, 2024 5 mins Linux how-to How to print from the Linux command line: double-sided, landscape and more There's a lot more to printing from the Linux command line than the lp command. Check out some of the many available options. By Sandra Henry Stocker Jun 11, 2024 6 mins Linux how-to Converting between uppercase and lowercase on the Linux command line Converting text between uppercase and lowercase can be very tedious, especially when you want to avoid inadvertent misspellings. Fortunately, Linux provides a handful of commands that can make the job very easy. By Sandra Henry Stocker Jun 07, 2024 5 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe