{"id":75,"date":"2021-06-26T14:26:28","date_gmt":"2021-06-26T14:26:28","guid":{"rendered":"https:\/\/bestunixhosting.in\/blog\/?p=75"},"modified":"2021-06-29T04:52:01","modified_gmt":"2021-06-29T04:52:01","slug":"how-to-empty-or-delete-a-large-file-content-in-linux","status":"publish","type":"post","link":"https:\/\/bestunixhosting.in\/blog\/how-to-empty-or-delete-a-large-file-content-in-linux\/","title":{"rendered":"How to Empty or Delete a Large File Content in Linux"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_69_1 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/bestunixhosting.in\/blog\/how-to-empty-or-delete-a-large-file-content-in-linux\/#How_to_Empty_or_Delete_a_Large_File_Content_in_Linux\" title=\"How to Empty or Delete a Large File Content in Linux\">How to Empty or Delete a Large File Content in Linux<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/bestunixhosting.in\/blog\/how-to-empty-or-delete-a-large-file-content-in-linux\/#1_Empty_File_Content_by_Redirecting_to_Null\" title=\"1. Empty File Content by Redirecting to Null\">1. Empty File Content by Redirecting to Null<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/bestunixhosting.in\/blog\/how-to-empty-or-delete-a-large-file-content-in-linux\/#2_Empty_File_Using_%E2%80%98true_Command_Redirection\" title=\"2. Empty File Using \u2018true\u2019 Command Redirection\">2. Empty File Using \u2018true\u2019 Command Redirection<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/bestunixhosting.in\/blog\/how-to-empty-or-delete-a-large-file-content-in-linux\/#3_Empty_File_Using_catcpdd_utilities_with_devnull\" title=\"3. Empty File Using cat\/cp\/dd utilities with \/dev\/null\">3. Empty File Using cat\/cp\/dd utilities with \/dev\/null<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/bestunixhosting.in\/blog\/how-to-empty-or-delete-a-large-file-content-in-linux\/#4_Empty_File_Using_echo_Command\" title=\"4. Empty File Using echo Command\">4. Empty File Using echo Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/bestunixhosting.in\/blog\/how-to-empty-or-delete-a-large-file-content-in-linux\/#5_Empty_File_Using_truncate_Command\" title=\"5. Empty File Using truncate Command\">5. Empty File Using truncate Command<\/a><\/li><\/ul><\/nav><\/div>\n<h4><span class=\"ez-toc-section\" id=\"How_to_Empty_or_Delete_a_Large_File_Content_in_Linux\"><\/span>How to Empty or Delete a Large File Content in Linux<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>While dealing with files in Linux <a href=\"https:\/\/www.putty.org\" target=\"_blank\" rel=\"noopener\">terminal<\/a> and want to clear the content of a file without necessarily opening it using any <a href=\"https:\/\/www.bestunixhosting.in\/web-hosting\/linux-shared-hosting\/\">Linux<\/a> command line editors. How can this be achieved? In this article, we will go through several different ways of emptying file content with the help of some useful commands.<\/p>\n<p><strong>Caution<\/strong>: Before we proceed to looking at the various ways, note that because in Linux everything is a file, you must always make sure that the file(s) you are emptying are not important user or system files. Clearing the content of a critical system or configuration file could lead to a fatal application\/system error or failure.<\/p>\n<p>With that said, below are means of clearing file content from the command line.<\/p>\n<p><strong>Important<\/strong>: For the purpose of this article, we\u2019ve used file <code>access.log<\/code> in the following examples.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"1_Empty_File_Content_by_Redirecting_to_Null\"><\/span>1. Empty File Content by Redirecting to Null<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>A easiest way to empty or blank a file content using shell redirect null (non-existent object) to the file as below:<\/p>\n<pre># access.log<\/pre>\n<figure id=\"attachment_157\" aria-describedby=\"caption-attachment-157\" style=\"width: 424px\" class=\"wp-caption aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"wp-image-157 size-full\" src=\"https:\/\/i0.wp.com\/chennaiwebhosting.in\/blog\/wp-content\/uploads\/2021\/06\/access-log.jpg?resize=424%2C166&#038;ssl=1\" alt=\"How to Empty or Delete a Large File Content in Linux\" width=\"424\" height=\"166\" \/><figcaption id=\"caption-attachment-157\" class=\"wp-caption-text\">Empty or Delete a Large File Content in Linux<\/figcaption><\/figure>\n<h3><\/h3>\n<h4><span class=\"ez-toc-section\" id=\"2_Empty_File_Using_%E2%80%98true_Command_Redirection\"><\/span>2. Empty File Using \u2018true\u2019 Command Redirection<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>Here we will use a symbol : is a shell built-in command that is essence equivalent to the true command and it can be used as a no-op (no operation).<\/p>\n<p>Another method is to redirect the output of : or true built-in command to the file like so:<\/p>\n<pre># : access.log\r\nOR \r\n# true access.log\r\n<\/pre>\n<h4><span class=\"ez-toc-section\" id=\"3_Empty_File_Using_catcpdd_utilities_with_devnull\"><\/span>3. Empty File Using cat\/cp\/dd utilities with \/dev\/null<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>In Linux, the null device is basically utilized for discarding of unwanted output streams of a process, or else as a suitable empty file for input streams. This is normally done by redirection mechanism.<\/p>\n<p>And the <code>\/dev\/null<\/code> device file is therefore a special file that writes-off (removes) any input sent to it or its output is same as that of an empty file.<\/p>\n<p>Additionally, you can empty contents of a file by redirecting output of <code>\/dev\/null<\/code> to it (file) as input using cat command:<\/p>\n<pre># cat \/dev\/null &gt; access.log\r\n<\/pre>\n<p>Next, we will use cp command to blank a file content as shown.<\/p>\n<pre># cp \/dev\/null access.log\r\n<\/pre>\n<p>In the following command, if means the input file and of refers to the output file.<\/p>\n<pre># dd if=\/dev\/null of=access.log\r\n<\/pre>\n<h4><span class=\"ez-toc-section\" id=\"4_Empty_File_Using_echo_Command\"><\/span>4. Empty File Using echo Command<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>Here, you can use an echo command with an empty string and redirect it to the file as follows:<\/p>\n<pre># echo \"\" &gt; access.log\r\nOR\r\n# echo &gt; access.log<\/pre>\n<p><strong>Note<\/strong>: You should keep in mind that an empty string is not the same as null. A string is already an object much as it may be empty while null simply means non-existence of an object.<\/p>\n<p>For this reason, when you redirect the out of the echo command above into the file, and view the file contents using the cat command, is prints an empty line (empty string).<\/p>\n<p>To send a null output to the file, use the flag -n which tells <strong>echo<\/strong> to not output the trailing newline that leads to the empty line produced in the previous command.<\/p>\n<pre># echo -n \"\" &gt; access.log<\/pre>\n<h4><span class=\"ez-toc-section\" id=\"5_Empty_File_Using_truncate_Command\"><\/span>5. Empty File Using truncate Command<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>The <strong>truncate<\/strong> command helps to shrink or extend the size of a file to a defined size.<\/p>\n<p>You can employ it with the -s option that specifies the file size. To empty a file content, use a size of 0 (zero) as in the next command:<\/p>\n<pre># truncate -s 0 access.log\r\n<\/pre>\n<p>That\u2019s it for now, in this article we have covered multiple methods of clearing or emptying file content using simple command line utilities and shell redirection mechanism.<\/p>\n<p>These are not probably the only available practical ways of doing this, so you can also tell us about any other methods not mentioned in this guide via the feedback section below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to Empty or Delete a Large File Content in Linux While dealing with files in Linux terminal and want to clear the content of a file without necessarily opening it using any Linux command line editors. How can this be achieved? In this article, we will go through several different ways of emptying file [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":80,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[5,7,4],"tags":[22,21],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/i0.wp.com\/bestunixhosting.in\/blog\/wp-content\/uploads\/2021\/06\/empty-file.jpg?fit=789%2C260&ssl=1","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/bestunixhosting.in\/blog\/wp-json\/wp\/v2\/posts\/75"}],"collection":[{"href":"https:\/\/bestunixhosting.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bestunixhosting.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bestunixhosting.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bestunixhosting.in\/blog\/wp-json\/wp\/v2\/comments?post=75"}],"version-history":[{"count":5,"href":"https:\/\/bestunixhosting.in\/blog\/wp-json\/wp\/v2\/posts\/75\/revisions"}],"predecessor-version":[{"id":81,"href":"https:\/\/bestunixhosting.in\/blog\/wp-json\/wp\/v2\/posts\/75\/revisions\/81"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/bestunixhosting.in\/blog\/wp-json\/wp\/v2\/media\/80"}],"wp:attachment":[{"href":"https:\/\/bestunixhosting.in\/blog\/wp-json\/wp\/v2\/media?parent=75"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bestunixhosting.in\/blog\/wp-json\/wp\/v2\/categories?post=75"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bestunixhosting.in\/blog\/wp-json\/wp\/v2\/tags?post=75"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}