Learning Linux terminal is easy, but mastering it is a bit hard. In many situations, you come across commands that mystify you because they contain different operators. Operators are characters or set of characters which offer different functionalities.
For example, one of the most used sets of operators in Linux is direction operators. Direction operators redirect the input or output of a command to a file or any other command.
There are two approaches for redirection; input redirection, and output redirection. For input redirection, we use less-than “<” sign and for output redirection greater-than “>” sign which are also termed as angled brackets.
Understanding operators is a bit troublesome. Adding one character to an operator can change its functionality altogether. Many Linux users face a similar situation while using “>” and “>>” operators in terminal. Both are output direction operators. So, what is the difference? Well, this write-up is all about discussing how these two operators differ. Let’s begin.
Difference Between “>” and “>>” in Linux
As discussed in the introductory part, both operators are output direction operators. The main difference is mentioned below:
“>“: Overwrites the existing file, or creates a file if the file of the mentioned name is not present in the directory.
“>>“: Appends the existing file, or creates a file if the file of the mentioned name is not present in the directory.
While making modifications in a file and you want to overwrite the existing data, then use the “>” operator. If you want to append something to that file, use the “>>” operator. Let’s understand it with an example. I am executing the following command in terminal:
You will notice that a text file will be created in the directory with the text “Welcome to LinuxHint”. To check, type“ls”:
To read the file type:
Let’s run the same command but with different text:
Now, open read the file using:
The new text has overwritten the previous text.
Let’s use “>>” operator:
It will also create a file by the name of “my_file_2.txt” in the current directory. Type “ls” to verify it:
To read this file, use:
Now, let’s change the text:
Since we are using a file that has already been created; to check what changes “>>” operator made, execute:
As it can been seen that instead of overwriting the existing text, the “>>” operator appended the text.
Some commands in Linux can cause confusion, especially to new users, because they contain operators. Operators are a bit tricky to understand because each operator can have different functionality. In this guide, we learned the difference between “>” and “>>” operators.
The “>” is an output operator that overwrites the existing file, while “>>” is also an output operator but appends the data in an already existing file. Both operators are often used to modify the files in Linux.