The data of a file after deletion is sent to the unallocated space of the hard disc. One can easily recover the original data. If the operating system overwrites the original contents of a file before deleting it, the original content of a file can not be recovered. This means the permanent deletion of the file. The shred command in Linux serves the purpose of overwriting the contents of a file before deleting it.
A. Description
The shred command in Linux is a tool to overwrite the contents of a file before deletion to make it harder to recover the file’s original data.
B. Syntax
The shred command in Linux is a command that takes OPTIONS and FILE as its argument. The syntax for shred commands is as below.
shred [OPTION]... FILE...
C. Options
One or more options can be added to the syntax of the shred command to modify the command. I have listed some valuable options below. If you do not find your desired option here, you can look for it on the man (manual) page. To go to the man page, type the following command and press ENTER.
man shred
Useful Options
- -f/–force: It changes permissions to allow writing if necessary.
- -n/–iterations=N: It overwrites N times instead of the default (3).
- –random-source=FILE: It gets random bytes from FILE.
- -s/–size=N: It shreds this many bytes from the beginning.
- -u: It deallocates and removes the file after overwriting.
- –remove[=HOW]: It is Like -u but gives control on HOW to delete.
- -v, –verbose: It shows the progress of the process.
- -x/–exact: It does not round file sizes up to the next entire block; this is the default for non-regular files.
- -z/–zero: It adds a final overwrite with zeros to hide shredding.
- –help: It displays this help and exit.
- –version: It prints version information and exit.
Practical Examples of the “shred” Command in Linux
The shred command in Linux is a handy tool to overwrite the original contents of a file. The shred command in Linux has many practical applications, and a few of them are outlined below.
Example 1: Overwrite the Contents of the File Multiple Times to Make It Unrecoverable
One can easily change a file’s original contents by overwriting them so that the original contents can not be recovered using the shred command in Linux. Here at first, I will print the contents of a text file named file1.txt then, I will overwrite the contents of file1.txt so that the original contents can not be recovered. To accomplish this, proceed as outlined in the following steps.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Type the following command in the command prompt to display the contents of the file1.txt file.
cat file1.txt
➌ Hit the ENTER key now.
➍ Now, type the following command in the prompt.
shred file1.txt
➎ Now press ENTER.
➏ Type the following command in the command prompt.
cat file1.txt
➐ Again, press the ENTER button.
Output >
The following image shows the implementation of overwriting the file1.txt file numerous times using the shred command in Linux, and the random characters are printed on the terminal.
Example 2: Overwrite Multiple Files Using the “shred” Command in Linux
You can overwrite multiple files using the shred Command in Linux. Here I have two files named file9_1.txt and file9_1.txt. I will overwrite these two files using the shred command. To do this, follow the steps outlined below.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Use the following command in the terminal to print the contents of file9_1.txt and file9_1.txt files.
cat file9_1.txt file9_2.txt
➌ Tap the ENTER key right now.
➍ Type the following command in the terminal.
shred file9_1.txt file9_2.txt
➎ Press ENTER button.
➏ Copy the following command into the terminal to print the contents of file9_1.txt and file9_1.txt files.
cat file9_1.txt file9_2.txt
➐ Hit the ENTER button.
Output >
The following image depicts that the shred command in Linux has overwritten multiple files, so random characters replace the contents of the two files.
Example 3: Changing the Number of Times a File is to be Overwritten Using the “shred” Command in Linux
Using the shred command in Linux, you can easily specify the number of times you want to overwrite its contents(by default, overwriting is done 3 times). I have a file named file2.txt. Now, I will print the contents of the file2.txt file on the terminal and then overwrite the file2.txt file 15 times. To achieve this, follow the procedure listed below.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Type the following command in the command prompt to display the contents of the file2.txt file.
cat file2.txt
➌ Hit the ENTER key now.
➍ Type the following command in the prompt.
shred -n 15 file2.txt
➎ Now press ENTER.
➏ Type the following command in the command prompt.
cat file2.txt
➐ Hit the ENTER key now.
Output >
The following image illustrates the completion of overwriting the file2.txt file 15 times using the shred command in Linux.
Example 4: Overwrite and Then Delete a file Using the “shred” Command in Linux
The shred command in Linux can overwrite a file’s contents and then delete the file in the same command line using the -u option. Here, I have a file named file3.txt. Now, I will overwrite the contents of the file3.txt file and delete the file eventually. To do this, go through the procedure listed below.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Type the following command in the command prompt to list the contents of the current directory.
ls -l
➌ Hit the ENTER key now.
➍ Type the following command in the command prompt.
shred -u file3.txt
➎ Now press ENTER.
➏ Type the following command in the command prompt to list the contents of the current directory.
ls -l
➐ Hit the ENTER key now.
Output >
The following image shows the overwriting and then deleting of the file3.txt file using the shred command in Linux with -u options. That is why file3.txt is missing in the directory.
Example 5: Overwrite Some Specific Bytes of Text Using the “shred” Command in Linux
With the help of the -s option, the shred command can overwrite some specific bytes of a file’s contents. Here, I have a file named file4.txt. I will overwrite the first 7 bytes of the file4.txt file. To accomplish this, check out the following steps.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Enter the following command in the command prompt to display the contents of the current directory.
cat file4.txt
➌ Press the ENTER Button.
➍ Now, enter the following command in the command prompt to overwrite the first 7 bytes of the file4.txt file.
shred -s 7 file4.txt
➎ Hit the ENTER key right now.
➏ To display the present contents of the file4.txt file, enter the following command in the terminal.
cat file4.txt
➐ Again, hit the ENTER key right now.
Output >
The following image shows the overwriting of the first 7 bytes of the file4.txt file using the shred command in Linux with -s options.
Example 6: Get the Numbers Of Overwriting Using the “shred” Command in Linux
You can find the number of overwriting done on a file using the -v option with the shred command in Linux. Here, I have a file named ncal.txt. Now, I will find the number of overwriting of the contents of the ncal.txt file. To do this, follow these procedures:
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Insert the following command into the terminal
shred -v ncal.txt
➌ Actuate the ENTER key right now.
Output >
The following image shows the number of overwriting of the ncal.txt file using the shred command in Linux with -v options.
Example 7: Add a Final Overwrite with Zeros To Hide Shredding Using the “shred” Command in Linux
One can easily change a file’s original contents by overwriting them and replacing them with null at the final overwrite to hide shredding. Here I have a file named file6.txt. I will overwrite the contents of file6.txt and replace them with null at last overwrite to hide shredding. To do this, follow the steps mentioned below.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Type the following command in the command prompt to display the contents of the file6.txt file in the terminal.
cat file6.txt
➌ After that, activate the ENTER key.
➍ Apply the following command in the command line.
shred -z file6.txt
➎ Now press ENTER Button.
➏ Apply the following command in the command line.
shred -z file6.txt
➐ Then, activate the ENTER key.
➑ Apply the following command in the command line.
cat file6.txt
➒ Activate the ENTER key again.
Output >
Overwriting the file6.txt file numerous times and replacing them with null at the final overwrite is done as depicted in the image using the shred command in Linux. And so, nothing is printed on the terminal.
Example 8: Change Permissions To Allow Writing If Necessary While Using the “shred” Command in Linux
The -f option of the shred command in Linux changes permission while necessary to overwrite. In this example, first, I will withdraw the writing permission of the “file7.txt” file for all, then overwrite the file7.txt file with the -f option. To achieve this, follow the steps below.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Copy the following command into the terminal to print the contents of the file7.txt on the terminal.
cat file7.txt
➌ Press the ENTER key right now.
➍ Now type the following command in the terminal to withdraw the writing permission from all, then press ENTER.
chmod a-w file7.txt
➎ Type the default shred command to overwrite the contents of the file7.txt file, then press ENTER.
shred file7.txt
➏ Then type the following command and then press ENTER.
shred -f file7.txt
➐ To set the reading permission for all, type the command written below on the terminal and then press ENTER.
chmod a+r file7.txt
➑ To print the present contents of the file7.txt file, type the following command.
cat file7.txt
➒ Finally, Tap the ENTER button.
Output >
The following image illustrates the completion of overwriting the file7.txt using the shred command in Linux.
Example 9: Get Basic Details and the Version of the “shred” Command in Linux
The –version option of the shred command in Linux gives the version of the shred command currently present in the system, along with other copyright information. Here I will print the version of the shred command now present in my system, along with copyright information. To achieve this, follow the steps outlined below.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Use the following command in the terminal.
shred --version
➌ Press the ENTER key now.
Output >
The version of the shred command currently present in the system and other copyright information is printed in the terminal using the –version option of the shred command in Linux, as depicted in the image.
Conclusion
In this article, I have demonstrated the overwriting of the contents of a file in various ways using the shred command in Linux. I hope you’ll be competent enough to explore more things with the help of these practical examples.
Similar Readings
- The “dd” Command in Linux [7+ Practical Examples]
- The “du” Command in Linux [14 Practical Examples]
- The “mount” Command in Linux [15 Practical Examples]
- The “mke2fs” Command in Linux [10 Practical Examples]
- The “mkfs” Command in Linux [4+ Practical Examples]
FUNDAMENTALS A Complete Guide for Beginners