{"id":10806,"date":"2024-12-12T06:33:56","date_gmt":"2024-12-12T06:33:56","guid":{"rendered":"https:\/\/blog.webystrata.co.uk\/?p=10806"},"modified":"2024-12-12T06:33:56","modified_gmt":"2024-12-12T06:33:56","slug":"how-to-read-and-edit-files-in-linux","status":"publish","type":"post","link":"https:\/\/www.webystrata.co.uk\/blog\/how-to-read-and-edit-files-in-linux\/","title":{"rendered":"How to Read and Edit Files in Linux"},"content":{"rendered":"<h1><a href=\"https:\/\/www.webystrata.co.uk\/reseller-hosting.html\"><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone wp-image-10808 size-full\" title=\"How to Read and Edit Files in Linux\" src=\"https:\/\/blog.webystrata.co.uk\/wp-content\/uploads\/2024\/12\/6802364_26958.jpg\" alt=\"How to Read and Edit Files in Linux\" width=\"2000\" height=\"2000\" srcset=\"https:\/\/www.webystrata.co.uk\/blog\/wp-content\/uploads\/2024\/12\/6802364_26958.jpg 2000w, https:\/\/www.webystrata.co.uk\/blog\/wp-content\/uploads\/2024\/12\/6802364_26958-300x300.jpg 300w, https:\/\/www.webystrata.co.uk\/blog\/wp-content\/uploads\/2024\/12\/6802364_26958-1024x1024.jpg 1024w, https:\/\/www.webystrata.co.uk\/blog\/wp-content\/uploads\/2024\/12\/6802364_26958-150x150.jpg 150w, https:\/\/www.webystrata.co.uk\/blog\/wp-content\/uploads\/2024\/12\/6802364_26958-768x768.jpg 768w, https:\/\/www.webystrata.co.uk\/blog\/wp-content\/uploads\/2024\/12\/6802364_26958-1536x1536.jpg 1536w\" sizes=\"(max-width: 2000px) 100vw, 2000px\" \/><\/a><\/h1>\n<h1>How to Read and Edit Files in Linux<\/h1>\n<p>This guide is only going to show an overview of the commands. To find out more about each command you can run the following:<\/p>\n<div class=\"highlight-bash notranslate\">\n<div class=\"highlight\">\n<pre>man <span class=\"o\">[<\/span>command<span class=\"o\">]<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<p>There are several ways to output a file in <a href=\"https:\/\/www.webystrata.co.uk\/reseller-hosting.html\">Linux<\/a>.<\/p>\n<p>Here are two test files and their content, we\u2019ll be using these in the upcoming examples.<\/p>\n<div class=\"highlight-bash notranslate\">\n<div class=\"highlight\">\n<pre>cat fileone.txt\r\n<\/pre>\n<\/div>\n<\/div>\n<div class=\"highlight-console notranslate\">\n<div class=\"highlight\">\n<pre><span class=\"go\">one<\/span>\r\n<span class=\"go\">two<\/span>\r\n<span class=\"go\">three<\/span>\r\n<span class=\"go\">four<\/span>\r\n<span class=\"go\">five<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<div class=\"highlight-bash notranslate\">\n<div class=\"highlight\">\n<pre>cat filetwo.txt\r\n<\/pre>\n<\/div>\n<\/div>\n<div class=\"highlight-console notranslate\">\n<div class=\"highlight\">\n<pre><span class=\"go\">ONE<\/span>\r\n<span class=\"go\">TWO<\/span>\r\n<span class=\"go\">THREE<\/span>\r\n<span class=\"go\">FOUR<\/span>\r\n<span class=\"go\">FIVE<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<section id=\"cat\">\n<h2><code class=\"docutils literal notranslate\"><span class=\"pre\">cat<\/span><\/code><\/h2>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">cat<\/span><\/code>\u00a0is mostly used today to output a file to the screen.<\/p>\n<div class=\"highlight-console notranslate\">\n<div class=\"highlight\">\n<pre><span class=\"gp\">[root@c7 ~]# <\/span>cat fileone.txt\r\n<span class=\"go\">one<\/span>\r\n<span class=\"go\">two<\/span>\r\n<span class=\"go\">three<\/span>\r\n<span class=\"go\">four<\/span>\r\n<span class=\"go\">five<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<p>It was originally to combine the output of two files.<\/p>\n<div class=\"highlight-console notranslate\">\n<div class=\"highlight\">\n<pre><span class=\"gp\">[root@c7 ~]# <\/span>cat fileone.txt filetwo.txt\r\n<span class=\"go\">one<\/span>\r\n<span class=\"go\">two<\/span>\r\n<span class=\"go\">three<\/span>\r\n<span class=\"go\">four<\/span>\r\n<span class=\"go\">five<\/span>\r\n<span class=\"go\">ONE<\/span>\r\n<span class=\"go\">TWO<\/span>\r\n<span class=\"go\">THREE<\/span>\r\n<span class=\"go\">FOUR<\/span>\r\n<span class=\"go\">FIVE<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<p>It can be used to add content to a file.<\/p>\n<div class=\"highlight-console notranslate\">\n<div class=\"highlight\">\n<pre><span class=\"gp\">[root@c7 ~]# <\/span>cat &gt; filethree.txt\r\n<span class=\"go\">new file content<\/span>\r\n<span class=\"go\">^C<\/span>\r\n<span class=\"gp\">[root@c7 ~]# <\/span>cat filethree.txt\r\n<span class=\"go\">new file content<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<p>After the\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">cat<\/span>\u00a0<span class=\"pre\">&gt;<\/span>\u00a0<span class=\"pre\">filethree.txt<\/span><\/code>\u00a0command, press enter and enter the text you want to add to your file. End this with a new line and\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">ctrl<\/span>\u00a0<span class=\"pre\">+<\/span>\u00a0<span class=\"pre\">c<\/span><\/code>.<\/p>\n<div class=\"admonition note\">\n<p>This command will\u00a0<strong>NOT<\/strong>\u00a0tell you if the file already exists so it\u2019s very easy to overwrite a file. Use with care.<\/p>\n<\/div>\n<\/section>\n<section id=\"less\">\n<h2>`less<\/h2>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">For<\/span>\u00a0<span class=\"pre\">very<\/span>\u00a0<span class=\"pre\">large<\/span>\u00a0<span class=\"pre\">files<\/span>\u00a0<span class=\"pre\">less<\/span>\u00a0<span class=\"pre\">will<\/span>\u00a0<span class=\"pre\">allow<\/span>\u00a0<span class=\"pre\">you<\/span>\u00a0<span class=\"pre\">to<\/span>\u00a0<span class=\"pre\">scroll<\/span>\u00a0<span class=\"pre\">up<\/span>\u00a0<span class=\"pre\">and<\/span>\u00a0<span class=\"pre\">down<\/span>\u00a0<span class=\"pre\">a<\/span>\u00a0<span class=\"pre\">file.<\/span>\u00a0<span class=\"pre\">Search<\/span>\u00a0<span class=\"pre\">the<\/span>\u00a0<span class=\"pre\">file<\/span>\u00a0<span class=\"pre\">with<\/span>\u00a0<span class=\"pre\">the<\/span><\/code>\/searchterm<code class=\"docutils literal notranslate\"><span class=\"pre\">keys.<\/span>\u00a0<span class=\"pre\">Use<\/span>\u00a0<span class=\"pre\">the<\/span><\/code>n` key to keep searching.<\/p>\n<\/section>\n<section id=\"head\">\n<h2><code class=\"docutils literal notranslate\"><span class=\"pre\">head<\/span><\/code><\/h2>\n<p>By default\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">head<\/span><\/code>\u00a0will show the top 10 lines of a file. This can be adjusted with the\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">-n<\/span><\/code>\u00a0flag.<\/p>\n<div class=\"highlight-console notranslate\">\n<div class=\"highlight\">\n<pre><span class=\"gp\">[root@c7 ~]# <\/span>head -n <span class=\"m\">2<\/span> fileone.txt\r\n<span class=\"go\">one<\/span>\r\n<span class=\"go\">two<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<\/section>\n<section id=\"tail\">\n<h2><code class=\"docutils literal notranslate\"><span class=\"pre\">tail<\/span><\/code><\/h2>\n<p>By default\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">tail<\/span><\/code>\u00a0will show the bottom 10 lines of a file. This can adjusted with the\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">-n<\/span><\/code>\u00a0flag.<\/p>\n<div class=\"highlight-console notranslate\">\n<div class=\"highlight\">\n<pre><span class=\"gp\">[root@c7 ~]# <\/span>tail -n <span class=\"m\">2<\/span> fileone.txt\r\n<span class=\"go\">four<\/span>\r\n<span class=\"go\">five<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<p>If your file is changing, you can watch the output. This is best used on logs like mail logs.<\/p>\n<div class=\"highlight-console notranslate\">\n<div class=\"highlight\">\n<pre><span class=\"gp\">[root@c7 ~]# <\/span>tail -f \/var\/log\/maillog\r\n<span class=\"go\">Aug 10 00:00:21 c7 sSMTP[31684]: Unable to locate mail<\/span>\r\n<span class=\"go\">Aug 10 00:00:21 c7 sSMTP[31684]: Cannot open mail:25<\/span>\r\n<span class=\"go\">Aug 10 11:10:44 c7 postfix\/postqueue[23404]: fatal: Cannot flush mail queue - mail system is down<\/span>\r\n<span class=\"go\">Aug 10 11:10:54 c7 postfix\/postfix-script[23546]: starting the Postfix mail system<\/span>\r\n<span class=\"go\">Aug 10 11:10:54 c7 postfix\/master[23548]: daemon started -- version 2.10.1, configuration \/etc\/postfix<\/span>\r\n<span class=\"go\">Aug 11 00:00:07 c7 sSMTP[25365]: Unable to locate mail<\/span>\r\n<span class=\"go\">Aug 11 00:00:07 c7 sSMTP[25365]: Cannot open mail:25<\/span>\r\n<span class=\"go\">Aug 11 11:17:21 c7 postfix\/smtpd[9292]: connect from localhost[::1]<\/span>\r\n<span class=\"go\">Aug 11 11:18:32 c7 postfix\/smtpd[9292]: lost connection after CONNECT from localhost[::1]<\/span>\r\n<span class=\"go\">Aug 11 11:18:32 c7 postfix\/smtpd[9292]: disconnect from localhost[::1]<\/span><\/pre>\n<\/div>\n<\/div>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>How to Read and Edit Files in Linux This guide is only going to show an overview of the commands. To find out more about each command you can run the following: man [command] There are several ways to output a file in Linux. Here are two test files and their content, we\u2019ll be using [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":10808,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-10806","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.webystrata.co.uk\/blog\/wp-json\/wp\/v2\/posts\/10806","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webystrata.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webystrata.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webystrata.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webystrata.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=10806"}],"version-history":[{"count":3,"href":"https:\/\/www.webystrata.co.uk\/blog\/wp-json\/wp\/v2\/posts\/10806\/revisions"}],"predecessor-version":[{"id":10810,"href":"https:\/\/www.webystrata.co.uk\/blog\/wp-json\/wp\/v2\/posts\/10806\/revisions\/10810"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webystrata.co.uk\/blog\/wp-json\/wp\/v2\/media\/10808"}],"wp:attachment":[{"href":"https:\/\/www.webystrata.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=10806"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webystrata.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=10806"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webystrata.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=10806"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}