Which command is used to redirect the content from one to another file without override?

Redirection can be defined as changing the way from where commands read input to where commands sends output. You can redirect input and output of a command.

For redirection, meta characters are used. Redirection can be into a file (shell meta characters are angle brackets '<', '>') or a program ( shell meta characters are pipesymbol '|').


Standard Streams In I/O Redirection

The bash shell has three standard streams in I/O redirection:

  • standard input (stdin) : The stdin stream is numbered as stdin (0). The bash shell takes input from stdin. By default, keyboard is used as input.
  • standard output (stdout) : The stdout stream is numbered as stdout (1). The bash shell sends output to stdout. Output goes to display.
  • standard error (stderr) : The stderr stream is numbered as stderr (2). The bash shell sends error message to stderr. Error message goes to display.

Redirection Into A File

Each stream uses redirection commands. Single bracket '>' or double bracket '>>' can be used to redirect standard output. If the target file doesn't exist, a new file with the same name will be created.

Overwrite

Commands with a single bracket '>' overwrite existing file content.

  • > : standard output
  • < : standard input
  • 2> : standard error

Note: Writing '1>' or '>' and '0<' or '<' is same thing. But for stderr you have to write '2>'.

Syntax:

Example:

Which command is used to redirect the content from one to another file without override?

Look at the above snapshot, command "cat > sample.txt" has created 'sample.txt' with content 'a, b, c'. Same file 'sample.txt' is created again with command "cat > sample.txt" and this time it overwrites earlier file content and only displays 'd, e, f '.


Append

Commands with a double bracket '>>' do not overwrite the existing file content.

  • >> - standard output
  • << - standard input
  • 2>> - standard error

Syntax:

Example:

Which command is used to redirect the content from one to another file without override?

Look at the above snapshot, here again we have created two files with the same name using '>>' in command "cat >> sample.txt". But this time, content doesn't overwrite and everything is displayed.


Redirection Into A Program

Pipe redirects a stream from one program to another. When pipe is used to send standard output of one program to another program, first program's data will not be displayed on the terminal, only the second program's data will be displayed.

Although the functionality of pipe may look similar to that of '>' and '>>' but has a significance difference. Pipe redirects data from one program to another while brackets are only used in redirection of files.

Example:

Which command is used to redirect the content from one to another file without override?

Look at the above snapshot, command "ls *.txt | cat > txtFile" has put all the '.txt' files into a newly created file 'txtFile'.


When a Linux user types any command into the bash prompt, the terminal usually prints the output of the invoked command so you can read it straight away. However, bash also permits you to “redirect” or save any command’s output in the system.

This article will discuss three different procedures of redirecting the output of the top command to any file.

Method 1: Single File Output Redirection

For utilizing the redirection of bash, execute any script, then define the  > or >>  operator followed by the file path to which the output should be redirected.

  • >>” operator is used for utilizing the command’s output to a file, including the output to the file’s current contents.
  • >” operator is used to redirect the command’s output to a single file and replace the file’s current content.

We can say that technically, this is a file redirection of “stdout,” which is the normal display. Now, we will execute the sample example. The “ls” command displays the content of the current directory’s folders and files after its execution.

Which command is used to redirect the content from one to another file without override?

However, this command will save the output to the specified file in the following example rather than printing it to the terminal.

ls > /home/linuxhint/outputfile

Which command is used to redirect the content from one to another file without override?

Utilize the given command syntax for checking the content of the file.

Now, write out the below-given command for printing the content of the “output file” in the terminal.

$ cat /home/linuxhint/outputfile

Which command is used to redirect the content from one to another file without override?

The operator “>” overwrites the file content with the command execution output. Instead, you can use the “>>” operator for saving the multiple commands output in a single file. For instance, the execution of the given command will add the system information to the specific file.

uname -a >> /path/to/file

$ uname -a >> /home/linuxhint/outputfile

$ cat /home/linuxhint/outputfile

Which command is used to redirect the content from one to another file without override?

Method 2: Redirecting terminal output to a single file

Didn’t like the idea of using the”>” or “>>” operator for redirecting output? Don’t worry! The tee command is here to rescue you.

command | tee /path/to/file

$ ls | tee  /home/linuxhint/outputfile

Which command is used to redirect the content from one to another file without override?

The below-given tee command will overwrite the file content with the command’s output similar to the “>” operator.

$ uname -a | tee -a  /home/linuxhint/outputfile

Which command is used to redirect the content from one to another file without override?

Method 3: The top command

System administrators also use the Linux top command to view real-time system statistics such as load average, system uptime, running tasks, used memory, specific information about each running process, and a summary of threads or processes. By utilizing the -b flag, this command helps to get the information about the currently executing processes in the system. The top command will permit the top to function in batch mode and the -n flag to determine the number of iterations the command should take as output.

$ top -b -n 1 > topfile.txt

Which command is used to redirect the content from one to another file without override?

All of the output resulting from the top command’s execution will be redirected to the specified file. Now, write out the “less” command for checking the content of the file.

Which command is used to redirect the content from one to another file without override?

The -n flag will send the single snapshot of executed command to the specified file. To retrieve only the first iteration, specify the “1” after the “-n” flag.

$ top -b -n 1 > top-iteration.txt

Which command is used to redirect the content from one to another file without override?

Utilize the “cat” command for viewing the running tasks information.

$ cat top-iteration.txt | grep Tasks

Which command is used to redirect the content from one to another file without override?

Conclusion:

In Linux, for redirecting output to a file, utilize the ”>” and ”>>” redirection operators or the top command. Redirection allows you to save or redirect the output of a command in another file on your system. You can use it to save the outputs and use them later for different purposes.

About the author

Which command is used to redirect the content from one to another file without override?

Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.