{"id":6960,"date":"2018-10-01T13:27:20","date_gmt":"2018-10-01T10:27:20","guid":{"rendered":"https:\/\/www.howtoforge.com\/linux-shred-command\/"},"modified":"2018-10-01T13:27:20","modified_gmt":"2018-10-01T10:27:20","slug":"linux-shred-command-tutorial-for-beginners-5-examples","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/linux-shred-command-tutorial-for-beginners-5-examples\/","title":{"rendered":"Linux shred Command Tutorial for Beginners (5 Examples)"},"content":{"rendered":"<p>Regardless of the OS you use, if you aren&#8217;t into computer forensics or are a pro\/enthusiast in general, you&#8217;ll likely be surprised to know that when you delete a file, the file contents aren&#8217;t actually deleted from the hard disk immediately.<\/p>\n<p>However, there are tools that make sure the data becomes unrecoverable. In Linux, there&#8217;s a command line utility &#8211; dubbed <strong>shred<\/strong> &#8211; that&#8217;s built for this purpose.<\/p>\n<p>In this tutorial, we will discuss shred using some easy to understand examples. But before we do that, it&#8217;s worth mentioning that all examples in this article have been tested on an Ubuntu 18.04 LTS machine.<\/p>\n<h2 id=\"linux-shred-command\">Linux shred command<\/h2>\n<p>The shred command overwrites a file in order to make original contents unrecoverable. Following is the command&#8217;s syntax:<\/p>\n<p class=\"command\">shred [OPTION]&#8230; FILE&#8230;<\/p>\n<p>And here&#8217;s how the tool&#8217;s man page explains it:<\/p>\n<pre readability=\"7\">shred - overwrite a file to hide its contents, and optionally delete it<p>Overwrite the specified FILE(s) repeatedly, in order to make it harder for even very expensive <br\/>hardware probing to recover the data.<\/p><\/pre>\n<p>Following are some Q&amp;A-styled examples that should give you a good idea on how the shred command works.<\/p>\n<h2 id=\"q-how-shred-command-works\">Q1. How shred command works?<\/h2>\n<p>If you just want to use it to overwrite a file, then all you got to do is to execute &#8216;shred&#8217; with file name as input.<\/p>\n<p class=\"command\">shred [filename]<\/p>\n<p>For example, I used it on file1.txt:<\/p>\n<p class=\"command\">shred file1.txt<\/p>\n<p>And the following screenshot clearly shows the file&#8217;s content got overwritten by this tool.<\/p>\n<p><a class=\"fancybox\" id=\"img-shred-basic-usage\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/shred-basic-usage.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/10\/linux-shred-command-tutorial-for-beginners-5-examples.png\" alt=\"Use shred on a file\" width=\"347\" height=\"396\" title=\"\"><\/a><\/p>\n<h2 id=\"q-how-to-change-number-of-times-file-is-overwritten\">Q2. How to change number of times file is overwritten?<\/h2>\n<p>By default, shred overwrites a file 3 times. However, if you want, you can change this number using the -n command line option.<\/p>\n<p>For example, to make sure the tool overwrites the file &#8216;file1.txt&#8217; 10 times, run the following command:<\/p>\n<p class=\"command\">shred -n 10 file1.txt<\/p>\n<h2 id=\"q-how-to-make-sure-shred-deletes-the-file-as-well\">Q3. How to make sure Shred deletes the file as well?<\/h2>\n<p>If you want shred to not only overwrite, but delete the file as well, then use the -u command line option.<\/p>\n<p>For example, I tried running the shred command in the following way:<\/p>\n<p class=\"command\">shred -u file1.txt<\/p>\n<p>And could confirm that the file actually got deleted.<\/p>\n<p><a class=\"fancybox\" id=\"img-shred-u-option\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/shred-u-option.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/10\/linux-shred-command-tutorial-for-beginners-5-examples-1.png\" alt=\"Overwrite and delete file with shred\" width=\"374\" height=\"72\" title=\"\"><\/a><\/p>\n<h2 id=\"q-how-to-selectively-overwrite-using-shred\">Q4. How to selectively overwrite using shred?<\/h2>\n<p>Suppose the requirement is to only overwrite a set number of bytes from the file. This can be done using the -s command line option, which requires you to pass the number of bytes as input.<\/p>\n<p>For example, my file contained the following text:<\/p>\n<pre>Howtoforge<br\/>FAQForge<br\/>Linux<\/pre>\n<p>And I wanted shred to overwrite only 10 bytes. So for this, I used shred in the following way:<\/p>\n<p class=\"command\">shred -s10 file1.txt<\/p>\n<p>And here&#8217;s how the operation affected the file contents:<\/p>\n<pre>?uTw?????<br\/>FAQForge<br\/>Linux<\/pre>\n<p>So you can see only the first 10 bytes were overwritten with garbage by the shred command.<\/p>\n<h2 id=\"q-how-to-tell-shred-the-way-to-delete\">Q5. How to tell shred the way to delete?<\/h2>\n<p>We have already discussed the -u command line option that tells shred to delete the file in addition to overwriting it. But there&#8217;s another option &#8211; dubbed &#8216;remove&#8217; &#8211; that allows you to tell shred the way you want it to delete the file.<\/p>\n<p>Following is how the man page explains the &#8216;remove&#8217; option:<\/p>\n<pre readability=\"10\">--remove[=HOW]<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0 like -u but give control on HOW to delete<p>\u00a0Delete\u00a0 FILE(s)\u00a0 if\u00a0 --remove (-u) is specified.\u00a0 The default is not to<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 remove the files because it is common to operate on device\u00a0 files\u00a0 like<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/dev\/hda,\u00a0 and those files usually should not be removed.\u00a0 The optional<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 HOW parameter indicates how to remove a directory\u00a0 entry:\u00a0 'unlink'\u00a0 =&gt;<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 use\u00a0 a\u00a0 standard\u00a0 unlink call.\u00a0 'wipe' =&gt; also first obfuscate bytes in<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 the name.\u00a0 'wipesync' =&gt; also sync each obfuscated byte to\u00a0 disk.\u00a0\u00a0 The<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 default mode is 'wipesync', but note it can be expensive.<\/p><\/pre>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>As you may agree, the shred command is an important tool if your work involves dealing with &#8211; especially deleting &#8211; sensitive data. Here, in this tutorial, we have discussed some command line options shred offers. Once you&#8217;re done practicing these, head to the tool&#8217;s\u00a0<a href=\"https:\/\/linux.die.net\/man\/1\/shred\" target=\"_blank\" rel=\"noopener noreferrer\">man page<\/a> to know more.<\/p>\n<div class=\"authorbox\" readability=\"15\">\n<img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/10\/linux-shred-command-tutorial-for-beginners-5-examples.jpg\" alt=\"Himanshu Arora\" title=\"\"> <\/p>\n<p><strong>About Himanshu Arora<\/strong><\/p>\n<p>Himanshu Arora has been working on Linux since 2007. He carries professional experience in system level programming, networking protocols, and command line. In addition to HowtoForge, Himanshu&#8217;s work has also been featured in some of world&#8217;s other leading publications including Computerworld, IBM DeveloperWorks, and Linux Journal.<\/p>\n<\/div>\n<div>\n<p><b>Share this page:<\/b><\/p>\n<p>\n<a href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.howtoforge.com%2Flinux-shred-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/10\/linux-shred-command-tutorial-for-beginners-5-examples-2.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-shred-command%2F&amp;text=Linux+shred+Command+Tutorial+for+Beginners+%285+Examples%29&amp;via=howtoforgecom&amp;related=howtoforgecom\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/10\/linux-shred-command-tutorial-for-beginners-5-examples-3.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/howtoforgecom\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/10\/linux-shred-command-tutorial-for-beginners-5-examples-4.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/plus.google.com\/share?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-shred-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/10\/linux-shred-command-tutorial-for-beginners-5-examples-5.png\" height=\"20\" alt=\"\" title=\"\"><\/a>\n<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Regardless of the OS you use, if you aren&#8217;t into computer forensics or are a pro\/enthusiast in general, you&#8217;ll likely be surprised to know that when you delete a file, the file contents aren&#8217;t actually deleted from the hard disk immediately. However, there are tools that make sure the data becomes unrecoverable. In Linux, there&#8217;s [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-6960","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/6960","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=6960"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/6960\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=6960"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=6960"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=6960"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}