Skip to main content

Linux Command Line Tutorial For Beginners - I/O Redirection

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

Comments

Post a Comment

Popular posts from this blog

Linux Command Line Tutorial For Beginners - Introduction

Hi Friends, Welcome to Blog , Welcome to Linux . Now first question you may ask yourself why we should learn command line, even if we have GUI ? So, Simple answer is that GUI ( Graphical User Interface ) is helpful in many task but not useful in all task, and when you learn command, you will observe that you are able to do your day to day task or any task related to programming much faster that you can do with GUI . By using command you can able to see what happening in a background when you given a something task. Before dive into the world of Linux you should know two terms i.e., Shell and Terminal. Now what is Shell? Shell is a simply a program that takes inputs from the keyboard and gives that to the operating system to perform task.It is also called as CLI ( Command Line Interface ), Now the second term is Terminal, terminal is tool which is used to pass a shell command, this is a program that open up a window and lets interacts with shell. There are different operating...

Linux Command Line Tutorial For Beginners - ls command

ls is a Linux shell command that lists directory contents which may be files or directories. First of all lets open Terminal, Alt+Ctrl+T ls command uses following syntax:          ls [option] [file or directory name] if we give simple ls command it will show us all directories and files present in home directory, as when we open terminal by default we are in home directory. Now if we want to see what is in Downloads directory then we simply give command as:                          ls Downloads Note one thing i.e., command line is case sensitive so we should write name of directory or file as it is. In same way you can able to see contents of any directory. If we want to see content of root directory then we will write command as: As we know / is called as root directory.      ...