Linux cat command Example | Linux print file content on terminal

Blogs ❯❯ Linux

Image could not load

Image by pixabay

what is cat Command in Linux ?

Hello everyone, I Hope आप सभी लोग अच्छे हैं। आपने कभी न कभी Linux में cat command का use file content को print करने के लिए तो किया ही होगा। अगर नहीं तो भी कोई बात नहीं। इस blog में हम cat command के purpose , need और कुछ useful practical examples को देखेंगे।

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

Linux cat Syntax

cat [options] [filename]

Linux create file using cat command

cat command को ">" operator के साथ use करके आप किसी file को create भी कर सकते हैं।

For example -

~/learning$ cat > file.txt

जब आप ऊपर दी गयी command run करने के बाद आप जो भी content file में write करना चाहते हैं वो write कर सकते हैं। फिर CTRL+d press करने पर content उस file में save हो जयगा।

~/learning$ cat >file.txt
this is the text
i want to write in the file
here I can write anything that I want
ctrl+d

अब ls command का use करके आप created file को list करा सकते हैं।

~/learning$ ls 
file1.txt
what is ls?

Linux में ls command का use directories या directory contents को list करने के लिए किया जाता है। By default यह सभी visible files और directory को list करता है।

learn more about ls command ...

Linux print file content

Ok , तो current directory में file.txt file create हो चुकी है। अब इस file के अंदर के content को print कराते हैं।

~/learning$ cat file.txt
this is the text
i want to write in the file
here I can write anything that I want

Linux cat options

cat file.txt का use करके file content को print cat command का default nature है , हालाँकि cat command में और भी कई options हैं जिसे आप cat --help run करके देख सकते हैं।

~/learning$ cat --help
Usage: cat [OPTION]... [FILE]...
Concatenate FILE(s) to standard output.

With no FILE, or when FILE is -, read standard input.

  -A, --show-all           equivalent to -vET
  -b, --number-nonblank    number nonempty output lines, overrides -n
  -e                       equivalent to -vE
  -E, --show-ends          display $ at end of each line
  -n, --number             number all output lines
  -s, --squeeze-blank      suppress repeated empty output lines
  -t                       equivalent to -vT
  -T, --show-tabs          display TAB characters as ^I
  -u                       (ignored)
  -v, --show-nonprinting   use ^ and M- notation, except for LFD and TAB
      --help     display this help and exit
      --version  output version information and exit
and some other info

Linux cat --number

file content को line number के साथ show करने के लिए आप cat के साथ --number या -n का use कर सकते हैं।

~/learning$ cat --number file.txt
     1  this is the text
     2  i want to write in the file
     3  here I can write anything that I want

Linux cat --show-tabs

file में use किये गए tab spaces को determine करने के लिए आप --show-tabs या -T option का use कर सकते हैं। तो जहाँ पर भी अपने tab use किया होगा वहां पर ‘^I’ add हो जयगा। हालाँकि content add करते time मैंने कोई tab नहीं लिया था। इसलिए example में output normal है।

~/learning$ cat -T file.txt
this is the text
i want to write in the file
here I can write anything that I want

Print ‘$’ at the End of every Line

output में हर line के end में $ sign show करने के लिए -E या --show-ends option का use किया जाता है।

~/learning$ cat -E file.txt
this is the text$
i want to write in the file$
here I can write anything that I want$

Linux Redirect file content

किसी एक file के content को किसी दूसरी file में content redirect / copy करने के लिए filename के बाद > operator का use किया जाता है। For example -

~/learning$ cat  file.txt > new_file.txt
~/learning$ cat new_file.txt
this is the text
i want to write in the file
here I can write anything that I want

Linux concat multiple's file content

cat command के साथ आप एक साथ एक से ज्यादा files के name pass करके सभी file के content को एक साथ भी show कर सकते हैं।

Example के लिए , new_file.txt file में कुछ नया content write करके दोनों files के content को print कराते हैं।

~/learning$ cat > new_file.txt
content in new_file fot testing
ctrl+d
~/learning$ cat new_file.txt file.txt
content in new_file fot testingthis is the text
i want to write in the file
here I can write anything that I want

I Hope, Linux में आपको cat command के बारे में अच्छे से समझ आ गया होगा। :)

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