shred Command in Linux with Examples
When you delete a file from Linux or from any Operating System, then the file is not deleted permanently from the hard disk. When a file is deleted, it first gets moved to the trash and as soon as you clear off the trash the files get deleted for the file system. But the file is still there on your hard drive, and it could be recovered. When you delete a file permanently or delete it from the trash, the pointer pointing to the file leaves the address of it and the data of the file is sent to a sector in hard disk and is considered as unallocated space and it can be recovered easily. The file gets permanently deleted when the OS writes over the sector of the file which was considered as unallocated. So, in order to delete a file completely from a hard disk "shred" is used in Linux. This command overwrites the contents of a file multiple times, using patterns chosen to maximize the destruction of the residual data, making it harder for even very expensive hardware probing to recover it.
Syntax of the `shred` command in Linux
shred [OPTION] FILE
[OPTIONS] represents the various parameters and flags that can be used to modify the behavior of the Shred command
FILE refers to the file or files you wish to shred.
Options available in `shred` Command
Options | Description |
---|---|
-n, --iterations=N | This option allows you to specify the number of times the file will be overwritten during the shredding process. By default, Shred performs 3 iterations. |
-u, --remove | This option instructs Shred to remove the file after the shredding process is complete. |
-v, --verbose | When using this option, Shred provides detailed information about the shredding process. |
-z, --zero | This option adds a final overwrite of all zeros to the file after the shredding process is complete. This helps to hide the fact that the file has been shredded. |
-f, --force | This option forces Shred to shred files that have read-only permissions or are otherwise protected. |
-r, --random-source=FILE | With this option, you can specify a file as the source of random data for overwriting the file being shredded. |
Examples and Usage of the `shred` command in Linux
1. Overwrite the contents of the file multiple times to make it unrecoverable.
shred filename.txt
The `filename.txt` will be overwritten during the shredding process.

It will change the file data in such a way that it would be really hard to get the old file back.
Note: In this case, The name of the file is filename.txt you may change it as per your need.
2. `-n, --iterations=N` option in `shred` command
To change the number of times a file is to be overwritten. By default, Shred performs 3 iterations but with this option we can define the number of iterations.
shred -n 10 filename.txt

This command will overwrite the file 10 times.
Note: In this case, the number of times the file is to be shredded is set to be 10 and the name of the file is filename.txt you may change these as per your need.
3. `-u, --remove` option in `shred` command
To remove the file after the shredding process is complete.
shred -u filename.txt

This will overwrite the file many times and will delete it as well.
Note: In this case, The name of the file is filename.txt you may change it as per your need.
4. To overwrite some specific bytes of text only.
shred -s 5 filename.txt

This will overwrite the first 5 bytes of the file.
Note: In this case, The name of the file is filename.txt and the number of bytes is 5, you may change these as per your need.
5. `-v, --verbose` option in `shred` command
To run shred command with verbose mode or to get how many times the file is overwritten
shred -v filename.txt

It will display every time it overwrites the file.
Note: In this case, The name of the file is filename.txt you may change it as per your need.
6. `-f, --force` option in `shred` command
To change permissions to allow writing if necessary while using shred command.
shred -f filename.txt

When you run shred command with -f option it will write the file even by changing the permissions if necessary.
Note: In this case, The name of the file is filename.txt you may change it as per your need.
7. `-z, --zero` option in `shred` command
To add a final, overwrite with zeros to hide shredding.
shred -z filename.txt`

After completing the shredding, it will overwrite the file with zeros to hide shredding.
Note: In this case, The name of the file is filename.txt you may change it as per your need.
8. `--version` option in `shred` command
To get basic details and version of shred command.
shred --version

This will display the version of the shred command present in your system along with some copyright details.
Conclusion
In this article we have discussed about `shred` command in Linux which provides a reliable and effective way for securely deleting files that can't be recovered further. We learn that overwriting file content multiple times ensures that our data cannot be easily retrieved. We have also discussed various options in `shred` command.