How to redirect using cat command in linux | unix | with examples
![]() |
redirection using cat command |
what is redirection?
Redirection simply means capturing output from a file command or program and sending it as an input to another file command or program.
And let's see what are the options we have for the redirection. So, For example, you want to transfer some output
Output > file
So I will just write output here and then you want to redirect it to a file. So, in this case, we want to send some output to a file and the arrow (>) indicates the direction of your output. So you want to send the output to the file this arrow (>) means the direction of your output.
Create a file using cat command
So let's see how we can use it so in the last session I've shown you how we can use the cat command in Linux. So we are going to use this cat command to create a file and then transferring our stream to this file. Just say cat here and then this (>) angle bracket this means we want to redirect our input or whatever output we are going to type in the terminal to a file. And let's name a file For example "test.txt" and when I press ENTER.
![]() |
linux cat command create a file |
$cat > file name
![]() |
file |
Here you will see there is a new file created right as soon as you press Enter. Now whatever you write here then this content will be written to the file. after finished your content you just need to press "ctrl + D" to indicate that this is the end of your file and then you are out of this cat command.
![]() |
linux cat command print output |
$cat file name
Overwrite the content of the file
![]() |
overwrite a file using cat command |
$cat > file name
What I want to do is I want to once again transfer something else to this "test.txt" file so once again I will just do cat with this angle bracket (>) and then the filename and then this time I want to add some content then I will just press Ctrl + D and now you will see that this "test.txt" file contains new content and earlier whatever content we have written to the file is overwritten by the new content.
Append the content of the file
![]() |
linux command cat append |
$cat >>file name
You want to append to the file. you want that this file should not be overwritten but it should be whatever you want to add to a file it will be added from the next line.
For example, what we do in that case we give the same command with two angle brackets (>>) so this is for appending to the existing content of a file. When you give two angle brackets (>>) and press Enter.
Concatenate two or more files
![]() |
cat command to concatenate |
$cat file1 file2 > output file
Sometimes you want to add two files and then transfer the content to the new file. I have these two files "list1.txt" and "list2.txt" For example I want to add the content of these two files and transfer it to a new file so what I can do is I can just write "cat list1.txt list2.txt > output.txt"
So this is the command so I want to join these two files and then transfer the content of the concatenation of these two files to our new file. When I press ENTER the new file will be created.
Conclusion
In this way, you can use redirection in Linux. So it's not particularly related to the "cat command" you can do the same thing with For example I will just write "ls -l > out.txt" and press Enter and when I see the content of out.txt then it gives me the output of ls command.
So I hope you enjoyed this tutorial please rate comment and bye for now.
HAVE A NICE DAY