How To Use shred Linux Command?


The shred command in Linux is a powerful tool that allows users to permanently delete files and make them unrecoverable. This command is particularly useful when you want to ensure that sensitive data is completely removed from a system. In this article, we will explore how to use the shred command, complete with examples and outputs.

Understanding the shred Command

Before we delve into the examples, it's important to understand what the shred command does. When you delete a file in Linux using the rm command, the file is not actually removed from the disk. Instead, the space that the file occupied is marked as available for reuse, but the data remains until it is overwritten by new data. This means that with the right tools, the deleted file can be recovered.

The shred command, on the other hand, overwrites the file's data before it is deleted, making it much more difficult (if not impossible) to recover. By default, shred overwrites the file three times, but this can be adjusted using the −n option.

Basic Usage of shred

The basic syntax of the shred command is as follows −

shred [options] file_name

Here's a simple one −

shred document.txt

In this example, the shred command will overwrite document.txt three times. After the command is executed, the data in document.txt will be replaced with random bits of data.

shred Command Options

The shred command comes with several options that allow you to customize its behavior −

  • −n − This option allows you to specify the number of times the data should be overwritten. For example, shred −n 5 document.txt will overwrite document.txt five times.

  • −u − This option tells shred to remove the file after overwriting it. By default, shred does not remove the file, it only overwrites the data. To overwrite and then remove a file, you would use shred −u document.txt.

  • −z − This option adds a final overwrite with zeros to hide shredding. This can be useful if you don't want someone to know that the file has been shredded. For example, shred −z document.txt will overwrite document.txt three times, and then a final time with zeros.

  • −v − This option enables verbose mode, which shows the progress of the operation. For example, shred −v document.txt will display the progress of the shredding process.

Here's an example that uses all of these options −

shred −n 5 −u −z −v document.txt

This command will overwrite document.txt five times, remove the file, overwrite it one final time with zeros, and display the progress of the operation.

Let's dive into some more examples with their respective outputs

Example 1: Basic shred Command

Command

shred file1.txt

This command will overwrite file1.txt three times with random data. There won't be any output in the terminal unless an error occurs.

Example 2: Specifying Number of Overwrites

Command

shred −n 10 file2.txt

This command will overwrite file2.txt ten times with random data. Again, there won't be any output unless an error occurs.

Example 3: Overwriting and Removing a File

Command

shred −u file3.txt

This command will overwrite file3.txt three times and then remove it. There won't be any output unless an error occurs.

Example 4: Overwriting with Zeros

Command

shred −z file4.txt

This command will overwrite file4.txt three times with random data and then a final time with zeros. There won't be any output unless an error occurs.

Example 5: Verbose Mode

Command

shred −v file5.txt

Output

shred: file5.txt: pass 1/3 (random)...
shred: file5.txt: pass 2/3 (random)...
shred: file5.txt: pass 3/3 (random)...

This command will overwrite file5.txt three times with random data and display the progress of the operation.

Example 6: Using All Options

Command −

shred −n 5 −u −z −v file6.txt

Output

shred: file6.txt: pass 1/6 (random)...
shred: file6.txt: pass 2/6 (random)...
shred: file6.txt: pass 3/6 (random)...
shred: file6.txt: pass 4/6 (random)...
shred: file6.txt: pass 5/6 (random)...
shred: file6.txt: pass 6/6 (000000)...
shred: file6.txt: removing
shred: file6.txt: renamed to 00000000000000
shred: 00000000000000: removed

This command will overwrite file6.txt five times with random data, remove the file, overwrite it one final time with zeros, and display the progress of the operation.

Example 7: Shredding Multiple Files

Command

shred −v file1.txt file2.txt file3.txt

Output

shred: file1.txt: pass 1/3 (random)...
shred: file1.txt: pass 2/3 (random)...
shred: file1.txt: pass 3/3 (random)...
shred: file2.txt: pass 1/3 (random)...
shred: file2.txt: pass 2/3 (random)...
shred: file2.txt: pass 3/3 (random)...
shred: file3.txt: pass 1/3 (random)...
shred: file3.txt: pass 2/3 (random)...
shred: file3.txt: pass 3/3 (random)...

This command will overwrite file1.txt, file2.txt, and file3.txt three times each with random data and display the progress of the operation.

Example 8: Shredding a Directory (Not Recommended)

Command

shred −v −u directory_name/*

Output

shred: directory_name/file1.txt: pass 1/3 (random)...
shred: directory_name/file1.txt: pass 2/3 (random)...
shred: directory_name/file1.txt: pass 3/3 (random)...
shred: directory_name/file1.txt: removing
shred: directory_name/file1.txt: renamed to 00000000000000
shred: 00000000000000: removed
...

This command will overwrite all files in directory_name three times with random data, remove the files, and display the progress of the operation. Please note that shred cannot be used to securely delete directories themselves, as it is designed for regular files.

Example 9: Shredding a File with More Overwrites and Zeros

Command

shred −n 10 −z −v file4.txt

Output

shred: file4.txt: pass 1/11 (random)...
shred: file4.txt: pass 2/11 (random)...
...
shred: file4.txt: pass 10/11 (random)...
shred: file4.txt: pass 11/11 (000000)...

This command will overwrite file4.txt ten times with random data, overwrite it one final time with zeros, and display the progress of the operation.

Example 10: Shredding a File with Random Data Only

Command

shred −n 5 −v file5.txt

Output

shred: file5.txt: pass 1/5 (random)...
shred: file5.txt: pass 2/5 (random)...
shred: file5.txt: pass 3/5 (random)...
shred: file5.txt: pass 4/5 (random)...
shred: file5.txt: pass 5/5 (random)...

This command will overwrite file5.txt five times with random data and display the progress of the operation.

Please note that the actual output may vary depending on the version of the shred command and the file system you are using.

Conclusion

The shred command is a powerful tool for ensuring that sensitive data is permanently deleted. However, it's important to note that shred may not be effective on some file systems, including some types of journaling file systems, RAID−based file systems, and file systems that cache in temporary locations. Always ensure that you understand the limitations of the tools you're using, and consider multiple layers of security to protect sensitive data.

Updated on: 13-Jul-2023

290 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements