Open In App

shred Command in Linux with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

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.

To-overwrite-the-contents-of-the-file-multiple-times-to-make-it-unrecoverable

To-overwrite-the-contents-of-the-file-multiple-times-to-make-it-unrecoverable

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
To-change-the-number-of-times-a-file-is-to-be-overwritten

To-change-the-number-of-times-a-file-is-to-be-overwritten

 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
To-overwrite-and-delete-a-file-as-well

To-overwrite-and-delete-a-file-as-well

 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
To-overwrite-some-specific-bytes-of-text-only

To-overwrite-some-specific-bytes-of-text-only

 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
To-run-shred-command-with-verbose-mode

To-run-shred-command-with-verbose-mode

 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
To-change-permissions-to-allow-writing-if-necessary-while-using-shred-command

To-change-permissions-to-allow-writing-if-necessary-while-using-shred-command

 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`
To-add-a-final-overwrite-with-zeros-to-hide-shredding

To-add-a-final-overwrite-with-zeros-to-hide-shredding

 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
To-get-basic-details-and-version-of-shred-command

To-get-basic-details-and-version-of-shred-command

 This will display the version of the shred command present in your system along with some copyright details.

Frequently Asked Questions

 What is the use of the shred command?

The shred command is used to securely delete files by overwriting their contents with random data. It helps to ensure that the original data cannot be easily recovered.

 How do I shred a directory in Linux?

The shred command is designed to shred individual files, not directories. If we want to securely delete all files within a directory, we can use the combination of the `find` and `shred` commands.
For Example:

find /path/to/directory -type f -exec shred {} \

This command will find all files within the specified directory and its subdirectories, and shred each file individually.
`find` utility to locate all files within the specified directory and its subdirectories.
`-type f` option ensures that only regular files are selected.
`-exec` option is used to execute the shred command on each file found.
`{}` placeholder represents the current file being processed.
`\;` denotes the end of the -exec command.

What is the difference between `shred` and `delete` command in Linux?

The “delete” command in Linux (often represented by “rm”) which is used to remove files and directories. It does not securely delete the files which can potentially be recovered. 

The “shred” command in Linux overwrites the contents of files with random data, making it much more difficult to recover the original data after deletion also.

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.



Last Updated : 10 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads