{"id":12614,"date":"2019-03-28T03:52:41","date_gmt":"2019-03-28T03:52:41","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=12614"},"modified":"2019-03-28T03:52:41","modified_gmt":"2019-03-28T03:52:41","slug":"5-ways-to-empty-or-delete-a-large-file-content-in-linux","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/28\/5-ways-to-empty-or-delete-a-large-file-content-in-linux\/","title":{"rendered":"5 Ways to Empty or Delete a Large File Content in Linux"},"content":{"rendered":"<p>Occasionally, while dealing with files in Linux terminal, you may want to clear the content of a file without necessarily opening it using any\u00a0<a href=\"https:\/\/www.tecmint.com\/linux-command-line-editors\/\" target=\"_blank\" rel=\"noopener\">Linux command line editors<\/a>. 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\u00a0<a href=\"https:\/\/www.tecmint.com\/explanation-of-everything-is-a-file-and-types-of-files-in-linux\/\" target=\"_blank\" rel=\"noopener\">Linux everything is a file<\/a>, 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\u00a0<code>access.log<\/code>\u00a0in the following examples.<\/p>\n<h3>1. Empty File Content by Redirecting to Null<\/h3>\n<p>A easiest way to empty or blank a file content using shell redirect\u00a0<code>null<\/code>\u00a0(non-existent object) to the file as below:<\/p>\n<pre># &gt; access.log\r\n<\/pre>\n<div id=\"attachment_23654\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/12\/Empty-Large-File-in-Linux.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-23654\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/12\/Empty-Large-File-in-Linux.png\" alt=\"Empty Large File Using Null Redirect in Linux\" width=\"469\" height=\"171\" aria-describedby=\"caption-attachment-23654\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-23654\" class=\"wp-caption-text\">Empty Large File Using Null Redirect in Linux<\/p>\n<\/div>\n<h3>2. Empty File Using \u2018true\u2019 Command Redirection<\/h3>\n<p>Here we will use a symbol\u00a0<code>:<\/code>\u00a0is a shell built-in command that is essence equivalent to the\u00a0<code>true<\/code>\u00a0command and it can be used as a no-op (no operation).<\/p>\n<p>Another method is to redirect the output of\u00a0<code>:<\/code>\u00a0or\u00a0<code>true<\/code>\u00a0built-in command to the file like so:<\/p>\n<pre># : &gt; access.log\r\nOR \r\n# true &gt; access.log\r\n<\/pre>\n<div id=\"attachment_23656\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/12\/Empty-Large-File-Using-Linux-Commands.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-23656\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/12\/Empty-Large-File-Using-Linux-Commands.png\" alt=\"Empty Large File Using Linux Commands\" width=\"591\" height=\"320\" aria-describedby=\"caption-attachment-23656\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-23656\" class=\"wp-caption-text\">Empty Large File Using Linux Commands<\/p>\n<\/div>\n<h3>3. Empty File Using cat\/cp\/dd utilities with \/dev\/null<\/h3>\n<p>In Linux, the\u00a0<code>null<\/code>\u00a0device 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\u00a0<code>\/dev\/null<\/code>\u00a0device 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\u00a0<code>\/dev\/null<\/code>\u00a0to it (file) as input using\u00a0<a href=\"https:\/\/www.tecmint.com\/13-basic-cat-command-examples-in-linux\/\" target=\"_blank\" rel=\"noopener\">cat command<\/a>:<\/p>\n<pre># cat \/dev\/null &gt; access.log\r\n<\/pre>\n<div id=\"attachment_23658\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/12\/Empty-File-Using-cat-Command.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-23658\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/12\/Empty-File-Using-cat-Command.png\" alt=\"Empty File Using cat Command\" width=\"511\" height=\"163\" aria-describedby=\"caption-attachment-23658\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-23658\" class=\"wp-caption-text\">Empty File Using cat Command<\/p>\n<\/div>\n<p>Next, we will use\u00a0<a href=\"https:\/\/www.tecmint.com\/progress-monitor-check-progress-of-linux-commands\/\" target=\"_blank\" rel=\"noopener\">cp command<\/a>\u00a0to blank a file content as shown.<\/p>\n<pre># cp \/dev\/null access.log\r\n<\/pre>\n<div id=\"attachment_23659\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/12\/Empty-File-Content-Using-cp-Command.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-23659\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/12\/Empty-File-Content-Using-cp-Command.png\" alt=\"Empty File Content Using cp Command\" width=\"526\" height=\"213\" aria-describedby=\"caption-attachment-23659\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-23659\" class=\"wp-caption-text\">Empty File Content Using cp Command<\/p>\n<\/div>\n<p>In the following command,\u00a0<code>if<\/code>\u00a0means the input file and\u00a0<code>of<\/code>\u00a0refers to the output file.<\/p>\n<pre># dd if=\/dev\/null of=access.log\r\n<\/pre>\n<div id=\"attachment_23660\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/12\/Empty-File-Content-Using-dd-Command.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-23660\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/12\/Empty-File-Content-Using-dd-Command.png\" alt=\"Empty File Content Using dd Command\" width=\"542\" height=\"212\" aria-describedby=\"caption-attachment-23660\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-23660\" class=\"wp-caption-text\">Empty File Content Using dd Command<\/p>\n<\/div>\n<h3>4. Empty File Using echo Command<\/h3>\n<p>Here, you can use an\u00a0<a href=\"https:\/\/www.tecmint.com\/echo-command-in-linux\/\" target=\"_blank\" rel=\"noopener\">echo command<\/a>\u00a0with 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\r\n<\/pre>\n<div id=\"attachment_23661\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/12\/Empty-File-Using-echo-Command.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-23661\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/12\/Empty-File-Using-echo-Command.png\" alt=\"Empty File Using echo Command\" width=\"453\" height=\"298\" aria-describedby=\"caption-attachment-23661\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-23661\" class=\"wp-caption-text\">Empty File Using echo Command<\/p>\n<\/div>\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\u00a0<a href=\"https:\/\/www.tecmint.com\/echo-command-in-linux\/\" target=\"_blank\" rel=\"noopener\">echo command<\/a>\u00a0above into the file, and view the file contents using the\u00a0<a href=\"https:\/\/www.tecmint.com\/13-basic-cat-command-examples-in-linux\/\" target=\"_blank\" rel=\"noopener\">cat command<\/a>, is prints an empty line (empty string).<\/p>\n<p>To send a null output to the file, use the flag\u00a0<code>-n<\/code>\u00a0which tells\u00a0<strong>echo<\/strong>\u00a0to not output the trailing newline that leads to the empty line produced in the previous command.<\/p>\n<pre># echo -n \"\" &gt; access.log\r\n<\/pre>\n<div id=\"attachment_23662\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/12\/Empty-File-Using-Null-Redirect.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-23662\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/12\/Empty-File-Using-Null-Redirect.png\" alt=\"Empty File Using Null Redirect\" width=\"480\" height=\"155\" aria-describedby=\"caption-attachment-23662\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-23662\" class=\"wp-caption-text\">Empty File Using Null Redirect<\/p>\n<\/div>\n<h3>5. Empty File Using truncate Command<\/h3>\n<p>The\u00a0<strong>truncate<\/strong>\u00a0command helps to\u00a0<a href=\"https:\/\/www.tecmint.com\/parted-command-to-create-resize-rescue-linux-disk-partitions\/\" target=\"_blank\" rel=\"noopener\">shrink or extend the size of a file<\/a>\u00a0to a defined size.<\/p>\n<p>You can employ it with the\u00a0<code>-s<\/code>\u00a0option 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<div id=\"attachment_23663\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/12\/Truncate-File-Content-in-Linux.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-23663\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/12\/Truncate-File-Content-in-Linux.png\" alt=\"Truncate File Content in Linux\" width=\"476\" height=\"159\" aria-describedby=\"caption-attachment-23663\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-23663\" class=\"wp-caption-text\">Truncate File Content in Linux<\/p>\n<\/div>\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<p><a href=\"https:\/\/www.tecmint.com\/empty-delete-file-content-linux\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Occasionally, while dealing with files in Linux terminal, you may want to clear the content of a file without necessarily opening it using any\u00a0Linux 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. Caution: Before &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/28\/5-ways-to-empty-or-delete-a-large-file-content-in-linux\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;5 Ways to Empty or Delete a Large File Content in Linux&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-12614","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12614","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=12614"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12614\/revisions"}],"predecessor-version":[{"id":12616,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12614\/revisions\/12616"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=12614"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=12614"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=12614"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}