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

LinuxOpen SourceOperating System

To overwrite and file contents in the Linux system, we use the shred command using the terminal.

shred – The shred command is used to securely delete files and devices. This command overwrites a file to hide file contents, and optionally delete the file so that it is very difficult to recover the file for any software in the Linux/Unix system.

As usual, to remove files from the system, we use the rm command using the terminal. After removal files through the rm command, it may be recoverable using the software whereas after removing files through the shred command files are unrecoverable because the shred command overwrites the files three times with multiple patterns.

Syntax

The general syntax of the shred command is as follows −

shred [OPTION]... FILE...

Brief description of option available in the shred command.

Sr.No.
Option & Description
1
-f, --force
Change the permissions to allow writing
2
-n, --iteration=N
Overwrite contents of the file N times instead of the default
3
--random-source=FILE
Get random bytes from another file
4
-s, --size=N
Shred this many bytes
5
-u
Remove file after overwriting
6
--remove[=HOW]
Just like -u option but give control how to delete file
7
-v
Show what is happening
8
-z, --zero
Overwrite file with zeros to hide shredding
9
--help
Display this help and exit
10
--version
Output version information and exit

Example

Overwrite the contents of the file and make it unrecoverable in the Linux system.

To overwrite the contents of the file and make it unrecoverable, we use the shred command in the Linux/Unix system using the terminal as shown below.

$ shred file.txt

After executing this command, the file ‘file.txt’ is unrecoverable.

Example

Overwrite the contents of the file and print what is being done in the Linux system.

To overwrite the contents of the file and print what is being done, we use --verbose option with the shred command in the Linux/Unix system using the terminal as shown in below.

vikash@tutorialspoint:~/shadow$ 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)...

Example

Overwrite only specific byte of the file in the Linux system.

To overwrite only specific byte of the file, we use -s option with the shred command in the Linux/Unix system as shown below.

vikash@tutorialspoint:~/shadow$ cat file.txt
Hey, welcome to tutorialspoint...
vikash@tutorialspoint:~/shadow$ shred -s 2 file.txt
vikash@tutorialspoint:~/shadow$ cat file.txt
y, welcome to tutorialspoint...

Display the help of the shred command and exit.

To check more information about the shred command, we use the –help option with the shred command in the Linux system as shown below.

$ shred --help

Conclusion – In this article, we learned to overwrite a file to hide its contents and optionally delete it using the shred command with available options and suitable examples. The rm command also used toremove the file in the Linux system.

raja
Published on 01-Jul-2021 15:09:31
Advertisements