Linux Write to a File in Bash

Blogs ❯❯ Linux

Image could not load

image by pixabay

Bash scripting के साथ काम करते समय हम जो भी जरूरी work करते हैं उनमें से एक है files को read और write करना। इस blog में, हम यह समझेंगे कि Bash में files को कैसे write किया जाता है या किसी file में new content को append कैसे किया जाता है।

***

Bash में file को read और write करने के कई तरीके हैं। सबसे easy तरीका है ">" और ">>" का use करके file में write करना। ">" और ">>" का use करके आप Linux में किसी भी file में content को overwrite या पहले से exist content में new content append कर सकते हैं।

Linux Write or Overwrite Content to a File

">" operator का use करके आप किसी भी file में content को overwrite कर सकते हैं। इस operator का use करके write गया content file के पुराने content को replace कर देता है।

Syntax :

echo "test" > filename

Example के लिए मैं current directory में एक test.txt file बनाकर उसमे कुछ dummy content write करने की कोशिश करूंगा।

~/learning$ touch testfile.txt ~/learning$ echo "Hey ! my name is Rahul" > testfile.txt ~/learning$ cat testfile.txt Hey ! my name is Rahul
What did I just do?

Well , आगे बढ़ने से पहले हम यह समझ लेते हैं कि मैंने अभी क्या किया ?

  1. सबसे पहले मैंने touch command का use करके learning directory में एक file testfile.txt create की है।

  2. उसके बाद echo command का use करके dummy text को testfile.txt file में write किया है।

  3. finally , cat command का use करके testfile.txt file में मौजूद content को bash में print कराया है।

what is touch ?

Linux में touch command operating system के लिए एक standard program है,
जिसका use करके file के timestamp create , change और modify करने के लिए किया जाता है। इसका अलावा इसका use नई file को create करने के लिए भी किया जाता है।

learn more about touch command ...

what is echo ?

बैसे तो echo command का use किसी भी text को bash में print करने के लिए किया जाता है। for example -

$ echo "Hello everyone !"
Hello everyone !

लेकिन echo को > और >> operators के साथ use करके आप किसी file में content भी write कर सकते हैं। जैसा कि अभी आपने देखा।

what is cat ?

Linux में cat की full form concatenate है। cat command का use करके आप files create कर सकते हैं , file content को print कर सकते हैं, files को concatenate कर सकते हैं और file output redirection कर सकते हैं।

learn more about cat command ...

> operator overwrite content to a file

तो जैसा कि आपने अभी पढ़ा भी कि > operator write किया जाने वाला content file में पहले से मजूद content को replace कर देता है। Example के लिए हम फिर से testfile.txt file में कोई new dummy text write करके देखते हैं।

~/learning$ cat testfile.txt
Hey ! my name is Rahul
~/learning$ echo "New dummy text" > testfile.txt
~/learning$ cat testfile.txt
New dummy text

I Hope, अब शायद आप > का use समझ गए होंगे गए होंगे।

Linux append content to a file

यह जरूर नहीं है कि content को file में replace ही करें , कभी कभी file में new content को append करने की जरूरत भी होती है। new content को file में append करने के लिए >> operators का use किया जाता है।

For example -

~/learning$ echo "this is new dummy text and it will not replace old text" >> testfile.txt
ubuntu@ip-172-31-90-35:~/learning$ cat testfile.txt
New dummy text
this is new dummy text and it will not replace old text

Linux Write formatted content to a file

अब echo के साथ एक problem है , कि अगर आप write किये जाने वाले content में कही line break करना चाहें तो वो नहीं कर सकते हैं। echo के use करके write किया गया content हमेशा एक line में ही write होगा।

For Example , नीचे दिए गए example में मैंने content में \n का use line break करने के लिए किया है , But output में कोई line break नहीं होगी।

~/learning$ echo "Shakespeare said - \n We know what we are, but know not what we may be." > testfile.txt
~/learning$ cat testfile.txt
Shakespeare said - \n We know what we are, but know not what we may be.

Linux में formatted content write करने के लिए printf का use किया जाता है।

Example -

~/learning$ printf "Shakespeare said - \n We know what we are, but know not what we may be." > testfile.txt
~/learning$ cat testfile.txt
Shakespeare said -
 We know what we are, but know not what we may be.

what is printf

printf का use भी echo की तरह किसी text को bash में print करने के लिए किया जाता है। लेकिन printf content को format करके print करता है। format करने का मतलब है आप text में new line (\n) , single quotes (' ') , double quotes (" ") etc use कर सकते।

For example :

$ printf "Shakespeare said - \n \"We know what we are, but know not what we may be."\"
Shakespeare said -
 "We know what we are, but know not what we may be."u

I Hope, आपको समझ आ गया होगा कि Linux में किस तरह से किसी file में में new content को write / append किया जाता है।

Recent Blogs

Loading ...

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook