wc Command In Linux | Linux count characters in line or file
Linux traceroute Command : traceroute Command In Linux
Linux netstat Command : Linux Networking
Private and Public IP Addresses In Hindi
What is DHCP In Hindi? DHCP kya hai?
What is Hub? and How Hub works?
If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
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 कर सकते हैं।
">"
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
Well , आगे बढ़ने से पहले हम यह समझ लेते हैं कि मैंने अभी क्या किया ?
सबसे पहले मैंने touch
command का use करके learning directory में एक file testfile.txt create की है।
उसके बाद echo
command का use करके dummy text को testfile.txt file में write किया है।
finally , cat
command का use करके testfile.txt file में मौजूद content को bash में print कराया है।
Linux में touch command operating system के लिए एक standard program है,
जिसका use करके file के timestamp create , change और modify करने के लिए किया जाता है। इसका अलावा इसका use नई file को create
करने के लिए भी किया जाता है।
learn more about touch command ...
बैसे तो echo command का use किसी भी text को bash में print करने के लिए किया जाता है। for example -
$ echo "Hello everyone !" Hello everyone !
लेकिन echo
को >
और >>
operators के साथ use करके आप किसी file में content भी write कर सकते हैं। जैसा कि अभी आपने देखा।
Linux में cat
की full form concatenate है। cat command का use करके आप files create कर सकते हैं , file content को print कर सकते हैं, files को concatenate कर सकते हैं और file output redirection कर सकते हैं।
learn more about cat command ...
तो जैसा कि आपने अभी पढ़ा भी कि >
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 समझ गए होंगे गए होंगे।
यह जरूर नहीं है कि 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
अब 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.
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 किया जाता है।
Loading ...
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