
{"id":30213,"date":"2026-04-20T14:03:54","date_gmt":"2026-04-20T12:03:54","guid":{"rendered":"https:\/\/contabo.com\/blog\/?p=30213"},"modified":"2026-04-24T14:08:16","modified_gmt":"2026-04-24T12:08:16","slug":"linux-cat-command","status":"publish","type":"post","link":"https:\/\/contabo.com\/blog\/linux-cat-command\/","title":{"rendered":"Linux cat Command: Syntax, Options, Real Examples"},"content":{"rendered":"\n<p>The linux cat command is one of those tools you use every single day without thinking about it. Then one day you realize you only know 20% of what it does. Here&#8217;s the other 80%.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-what-is-the-linux-cat-command\">What Is the Linux cat Command?<\/h2>\n\n\n\n<p>cat stands for concatenate. Its original purpose was to join files together, but it&#8217;s become the go-to command for quickly viewing file contents. It reads one or more files and writes them to standard output, which you can view in the terminal or redirect elsewhere.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-where-cat-fits-in-daily-linux-work\">Where cat Fits in Daily Linux Work<\/h3>\n\n\n\n<p>You&#8217;ll reach for cat when you need to quickly read a config file, check a log snippet, or verify a file was written correctly. For longer files, less or more are better. cat is for short files where you want the whole thing at once without paging.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-cat-command-syntax\">cat Command Syntax<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>cat &#91;OPTIONS] &#91;FILENAME...]<\/code><\/pre>\n\n\n\n<p>Without a filename, cat reads from standard input until you press Ctrl+D. With one or more filenames, it reads and prints them in order.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-how-to-use-the-cat-command\">How to Use the cat Command<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-display-file-contents\">Display File Contents<\/h3>\n\n\n\n<p>The most common use case:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat \/etc\/hostname<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>cat \/var\/log\/syslog<\/code><\/pre>\n\n\n\n<p>For multiple files, cat displays them sequentially in the order provided.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-concatenate-multiple-files\">Concatenate Multiple Files<\/h3>\n\n\n\n<p>This is what cat was actually designed for. Combine files into one:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat file1.txt file2.txt > combined.txt<\/code><\/pre>\n\n\n\n<p>The &gt; redirects the combined output to a new file. Without it, the combined content prints to the terminal.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-add-line-numbers-with-n\">Add Line Numbers with -n<\/h3>\n\n\n\n<p>The -n flag numbers every output line, which is useful when referencing specific lines in config files or scripts:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat -n \/etc\/nginx\/nginx.conf<\/code><\/pre>\n\n\n\n<p>The -b flag numbers only non-blank lines if you want to skip counting empty ones.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-suppress-blank-lines-with-s\">Suppress Blank Lines with -s<\/h3>\n\n\n\n<p>The -s flag squeezes multiple consecutive blank lines into a single blank line. Useful with verbose config files or log outputs that have excessive whitespace:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat -s verbose_config.txt<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-show-non-printing-characters-with-a\">Show Non-Printing Characters with -A<\/h3>\n\n\n\n<p>The -A flag shows tabs as ^I and line endings as $ characters. This is invaluable when debugging Windows-formatted files uploaded to a Linux server, where hidden carriage returns (^M) break scripts:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat -A script.sh<\/code><\/pre>\n\n\n\n<p>If you see ^M at the end of every line, the file has Windows line endings. Fix it with dos2unix.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-create-and-append-to-files-with-cat\">Create and Append to Files with cat<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-create-a-new-file\">Create a New File<\/h3>\n\n\n\n<p>Use cat with redirection and a heredoc to create a file interactively:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat > newfile.txt<\/code><\/pre>\n\n\n\n<p>Type your content, then press Ctrl+D to save and exit. The file gets created with whatever you typed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-append-to-an-existing-file\">Append to an Existing File<\/h3>\n\n\n\n<p>Use &gt;&gt; instead of &gt; to append without overwriting:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat >> existing.txt<\/code><\/pre>\n\n\n\n<p>Type new content, Ctrl+D to finish. This adds to the end of the file.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-cat-with-pipes-and-redirects\">cat with Pipes and Redirects<\/h2>\n\n\n\n<p>cat works well as the start of a pipeline when you want to feed a file into another command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat access.log | grep \"404\" | wc -l<\/code><\/pre>\n\n\n\n<p>That said, many commands accept filenames directly (grep &#8220;404&#8221; access.log), making the cat unnecessary. It&#8217;s a style choice for readability versus efficiency.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-when-not-to-use-cat-common-misuse\">When NOT to Use cat: Common Misuse<\/h2>\n\n\n\n<p>The &#8216;useless use of cat&#8217; is a real thing in Linux scripting circles. Piping cat into commands that can read files directly adds a pointless process fork:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>cat file.txt | grep pattern\u00a0 \u2192\u00a0 use grep pattern file.txt instead<\/li>\n\n\n\n<li>cat file.txt | head -20\u00a0 \u2192\u00a0 use head -20 file.txt instead<\/li>\n\n\n\n<li>cat file.txt | wc -l\u00a0 \u2192\u00a0 use wc -l file.txt instead<\/li>\n<\/ul>\n\n\n\n<p>These aren&#8217;t wrong, but they&#8217;re unnecessary overhead. On large files or in tight loops, it adds up.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-faq-linux-cat-command\">FAQ: Linux cat Command<\/h2>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1777032453680\"><strong class=\"schema-faq-question\">What does cat do in Linux?<\/strong> <p class=\"schema-faq-answer\">cat reads one or more files and writes their contents to standard output. You can view files in the terminal, concatenate multiple files, or redirect output to new files.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1777032461505\"><strong class=\"schema-faq-question\">How do I display line numbers with cat?<\/strong> <p class=\"schema-faq-answer\">Use cat -n filename. This numbers every line in the output. Use -b instead to number only non-blank lines.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1777032470599\"><strong class=\"schema-faq-question\">How do I concatenate files in Linux?<\/strong> <p class=\"schema-faq-answer\">Run: cat file1.txt file2.txt > combined.txt. This merges both files into combined.txt in the order specified.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1777032479341\"><strong class=\"schema-faq-question\">How is cat different from less or more?<\/strong> <p class=\"schema-faq-answer\">cat dumps the entire file at once. less and more paginate the output so you can scroll through it. Use cat for short files, less for long ones.<\/p> <\/div> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>The linux cat command is one of those tools you use every single day without thinking about it. Then one day you realize you only know 20% of what it does. Here&#8217;s the other 80%. What Is the Linux cat Command? cat stands for concatenate. Its original purpose was to join files together, but it&#8217;s [&hellip;]<\/p>\n","protected":false},"author":44,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[18],"tags":[],"ppma_author":[3402],"class_list":["post-30213","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Milan Ivanovic","author_link":"https:\/\/contabo.com\/blog\/author\/milan\/"},"uagb_comment_info":0,"uagb_excerpt":"The linux cat command is one of those tools you use every single day without thinking about it. Then one day you realize you only know 20% of what it does. Here&#8217;s the other 80%. What Is the Linux cat Command? cat stands for concatenate. Its original purpose was to join files together, but it&#8217;s&hellip;","authors":[{"term_id":3402,"user_id":0,"is_guest":1,"slug":"contabro","display_name":"ContaBro","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g","0":null,"1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":""}],"_links":{"self":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts\/30213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/users\/44"}],"replies":[{"embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/comments?post=30213"}],"version-history":[{"count":1,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts\/30213\/revisions"}],"predecessor-version":[{"id":30214,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts\/30213\/revisions\/30214"}],"wp:attachment":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/media?parent=30213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/categories?post=30213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/tags?post=30213"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=30213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}