How to overwrite a file to hide file contents, and make original contents unrecoverable in Linux?

To overwrite file contents and make them unrecoverable in the Linux system, we use the shred command through the terminal. Unlike the standard rm command which only removes file entries from the filesystem, shred performs secure deletion by overwriting the actual data multiple times.

shred − The shred command is used to securely delete files and devices. This command overwrites a file to hide its contents and optionally deletes the file, making it extremely difficult to recover using any software recovery tools in Linux/Unix systems.

When we remove files using the rm command, the file data remains on disk and can potentially be recovered using specialized software. However, files processed with the shred command are unrecoverable because shred overwrites the file contents three times by default with multiple random patterns, effectively destroying the original data.

Syntax

The general syntax of the shred command is as follows −

shred [OPTION]... FILE...

Command Options

Option Description
-f, --force Change permissions to allow writing if necessary
-n, --iterations=N Overwrite file contents N times instead of the default (3)
--random-source=FILE Get random bytes from specified file
-s, --size=N Shred only the specified number of bytes
-u, --remove Remove file after overwriting
-v, --verbose Show detailed progress information
-z, --zero Add final overwrite with zeros to hide shredding
--help Display help information and exit

Examples

Basic File Shredding

To overwrite the contents of a file and make it unrecoverable −

$ shred file.txt

After executing this command, the file 'file.txt' becomes unrecoverable while still remaining in the filesystem.

Verbose Output

To overwrite file contents and display detailed progress information −

$ shred --verbose file.txt
shred: file.txt: pass 1/3 (random)...
shred: file.txt: pass 2/3 (random)...
shred: file.txt: pass 3/3 (random)...

Shredding Specific Bytes

To overwrite only a specific number of bytes from the beginning of the file −

$ cat file.txt
Hey, welcome to tutorialspoint...
$ shred -s 2 file.txt
$ cat file.txt
y, welcome to tutorialspoint...

Complete Secure Deletion

To overwrite file contents and remove the file completely −

$ shred -u -v file.txt

The -u option removes the file after shredding, while -v shows the progress.

Custom Iterations with Zero Fill

To perform 5 overwrite passes followed by a final zero fill −

$ shred -n 5 -z -v file.txt

Key Points

  • Security Level − shred performs multiple overwrite passes (default 3) with random patterns

  • SSD Limitations − On solid-state drives, wear leveling may reduce shred effectiveness

  • Filesystem Dependencies − Works best on traditional filesystems; some modern filesystems may limit effectiveness

  • Complete Removal − Use -u option to remove the file entry after shredding

Conclusion

The shred command provides secure file deletion by overwriting file contents multiple times with random patterns, making data recovery extremely difficult. While the rm command only removes file references, shred actually destroys the underlying data, providing true secure deletion for sensitive files.

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

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements