In this Blog, we will learn how we can use redirection in Linux.
First of all What is redirection means?
It means capturing output in a file using command and send it again as a input to program, file or command.
So, lets see what are the options and command we have to use for redirection.
Open Terminal Alt+Ctrl+T,
Syntax for redirection is:
[output] > file_name
Here > indicates the direction of output where we are going to save our output.
In our previous blog we had learned how to use cat command, and here we are going to use cat command for creating a file and transferring data to that file.
So, lets use first command only cat which will save the input whatever we given to it.
cat > [file_name]
Note one thing after > whatever file name we are giving, if that file is not exists at all then it will create file and save the output to it.
To end the cat command we will use Ctrl+d.
You can verify that output is saved or not by giving command:
cat [file_name]
Now, suppose one file is created and we have saved something in that file and now if you want to save another output in same file then the content will get over written.
If you want to save previous result as well as new result or output in same file that concept is called as append.
So, for append the data we will use following command:
cat >> [file_name]
What we done, we simply put >> in place of > which indicates append mode of file saving.
Now, you can see that content is not over written.
Now, if you want to add two files content and save that content to new file, for that we will use command as:
cat [1st file name] [2nd file name] > [new file name]
You can see that whatever content present in text1.txt and text.txt is saved to new file textnew.txt file.
Suppose one file contain something and another file also contain something, you are interested to save content of one file in another file and if you give command as:
cat [1st file] [2nd file] > [2nd file]
This will not work because output of 2nd file is input to itself, which is not possible. But, to do so, we can use append mode here i.e, use >>
cat [1st file] >> [2nd file]
For understanding the concept I have used cat command, you can use any command for redirection.
Now, come to question
What is the difference between cat -b and cat -n command ?
Answer this question and check your understanding level.
Answer of previous question is:
cat -s [file_name] will align or separate all lines by two line space. No matter how much space is in between two lines.
For any query and suggestions you may contact with me or you may put comment in comment box.
Thank You.
Contact Details:
Phone: +91-8208826234
E-mail: shivampawar1038@gmail.com
First of all What is redirection means?
It means capturing output in a file using command and send it again as a input to program, file or command.
So, lets see what are the options and command we have to use for redirection.
Open Terminal Alt+Ctrl+T,
Syntax for redirection is:
[output] > file_name
Here > indicates the direction of output where we are going to save our output.
In our previous blog we had learned how to use cat command, and here we are going to use cat command for creating a file and transferring data to that file.
So, lets use first command only cat which will save the input whatever we given to it.
cat > [file_name]
Note one thing after > whatever file name we are giving, if that file is not exists at all then it will create file and save the output to it.
To end the cat command we will use Ctrl+d.
You can verify that output is saved or not by giving command:
cat [file_name]
Now, suppose one file is created and we have saved something in that file and now if you want to save another output in same file then the content will get over written.
If you want to save previous result as well as new result or output in same file that concept is called as append.
So, for append the data we will use following command:
cat >> [file_name]
What we done, we simply put >> in place of > which indicates append mode of file saving.
Now, you can see that content is not over written.
Now, if you want to add two files content and save that content to new file, for that we will use command as:
cat [1st file name] [2nd file name] > [new file name]
You can see that whatever content present in text1.txt and text.txt is saved to new file textnew.txt file.
Suppose one file contain something and another file also contain something, you are interested to save content of one file in another file and if you give command as:
cat [1st file] [2nd file] > [2nd file]
This will not work because output of 2nd file is input to itself, which is not possible. But, to do so, we can use append mode here i.e, use >>
cat [1st file] >> [2nd file]
For understanding the concept I have used cat command, you can use any command for redirection.
Now, come to question
What is the difference between cat -b and cat -n command ?
Answer this question and check your understanding level.
Answer of previous question is:
cat -s [file_name] will align or separate all lines by two line space. No matter how much space is in between two lines.
For any query and suggestions you may contact with me or you may put comment in comment box.
Thank You.
Contact Details:
Phone: +91-8208826234
E-mail: shivampawar1038@gmail.com
Very Helpful Blog nice work Shivam
ReplyDeleteThank you Tanajee Yedage
DeleteKeep it up
ReplyDeleteThank you Shriram Chavan
Delete
ReplyDeleteAwesome Work..
Thank you Pranav Thorawade
Delete👌🏻👌🏻👌🏻
ReplyDeleteThank You Yogesh Pawar
DeleteIt is very helpful !! .
ReplyDeleteThank You Hrishikesh Salunkhe
DeleteGreat stuff ! Superb buddy !
ReplyDeleteThank You.
Delete