redirection in linux with examples

Case1)

You can use redirection command to creat new file and you can add text to that file

Enter this coomand

Cat  > devops.txt press enter

After entering this command you will enter into  new promt

And write conetnt you want to write

Here iam writing

devops 1

devops 2

devops 3

after writing evrything to save it press

cntrl+d

So now you will out of that promt

If you want to see content of that file you can use

cat devops.txt

you can see above data

case2)

already you have some data now if you enter same coomand cat > devops.txt

u will enter into prompt  now add new content

devops 4

devops 5

devops 6  and press cntrl+d  now cat the file

cat  devops.txt

you can see only  new data so earlier whatever content you have overwritten is overwritten by the new content

 

case3)

if you want to append new content into that file that means whatever you written in second time

that content will added to previous content from next line

for this we have to use this command

cat >> devops.txt

>> is appending symbol new content is added to previous content

So now iam adding new content

Linux 7

Linux 8

Linux 9 and press control+d

Now cat the file cat devops.txt

devops 4

devops 5

devops 6

Linux 7

Linux 8

Linux 9

You can see all the data

So if you want to append to the existing content of the file you have to use  >>

Case 4)

If you want to add the content of two files and transfer it to new file

U can use this command

Cat  first.txt  second.txt > third.txt

So here the content of first.txt and second.txt file will added and transferred into new file third.txt

If the file(third.txt) doesn’t exist it will create new file and send it to third.txt

You can check output with   cat third.txt

If you want to add more than two files

Cat  first.txt  second.txt  third.txt …………..>  final.txt

Case 5)

If you want append content of one file to another file

That means if you want to add content of first file to second file you can use this command

Cat   first.txt  >>  second.txt

Leave a Reply

Your email address will not be published. Required fields are marked *