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 example

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.

Examples

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: 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 5: 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 6: 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.

Key Points

  • File system limitations shred may not be effective on journaling file systems (like ext4 with journaling), RAID systems, or SSDs with wear leveling.

  • Directory handling shred cannot securely delete directories themselves, as it is designed for regular files only.

  • SSD considerations On SSDs, use the blkdiscard command or manufacturer tools for secure erasure due to wear leveling algorithms.

  • Performance impact Multiple overwrites can be time-consuming on large files or slow storage devices.

Conclusion

The shred command is a powerful tool for ensuring that sensitive data is permanently deleted from traditional hard drives. However, it's important to understand its limitations on modern file systems and storage technologies. For critical security requirements, consider using multiple layers of data protection including encryption and specialized secure deletion tools.

Updated on: 2026-03-17T09:01:38+05:30

753 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements