Linux shred Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the shred command in Linux to securely delete files by overwriting their contents multiple times. This ensures that the deleted files cannot be recovered using common data recovery techniques. You will start by creating a sample file and then learn how to use the shred command to overwrite and remove the file. Additionally, you will explore various options available with the shred command, such as specifying the number of overwrite iterations and adding a final overwrite with zeros.

Next, you will learn how to use the shred command to securely delete multiple files at once. The shred command is a powerful tool for ensuring the complete and permanent deletion of sensitive data, preventing it from being recovered by unauthorized parties.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/BasicFileOperationsGroup -.-> linux/rm("`File Removing`") linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") linux/SystemInformationandMonitoringGroup -.-> linux/dd("`File Converting/Copying`") subgraph Lab Skills linux/rm -.-> lab-422917{{"`Linux shred Command with Practical Examples`"}} linux/sudo -.-> lab-422917{{"`Linux shred Command with Practical Examples`"}} linux/dd -.-> lab-422917{{"`Linux shred Command with Practical Examples`"}} end

Introduction to the shred Command

In this step, you will learn about the shred command in Linux, which is used to securely delete files by overwriting their contents multiple times. This ensures that the deleted files cannot be recovered using common data recovery techniques.

First, let's create a sample file to work with:

echo "This is a sample file to be shredded." > sample_file.txt

Example output:

The shred command works by overwriting the contents of a file with random data multiple times before deleting it. This makes it much more difficult for the file to be recovered, even with specialized data recovery tools.

To use the shred command, simply run the following:

sudo shred sample_file.txt

Example output:

The shred command has several options that allow you to customize its behavior. Some common options include:

  • -n, --iterations=N: Overwrite the file N times instead of the default 3 times.
  • -z, --zero: Add a final overwrite with zeros to hide shredding.
  • -u, --remove: Truncate and remove the file after overwriting.
  • -v, --verbose: Show progress.

For example, to overwrite a file 5 times and then remove it, you can use the following command:

sudo shred -n 5 -u sample_file.txt

Example output:

In the next step, you will learn how to use the shred command to securely delete files.

Securely Deleting Files with shred

In this step, you will learn how to use the shred command to securely delete files on your system.

First, let's create a few sample files to work with:

touch file1.txt file2.txt file3.txt

Example output:

To securely delete a single file using shred, run the following command:

sudo shred -u file1.txt

The -u option tells shred to remove the file after overwriting it.

Example output:

You can also delete multiple files at once using shred:

sudo shred -u file2.txt file3.txt

Example output:

The shred command overwrites the file contents multiple times before deleting the file, making it much more difficult to recover the data.

If you want to see the progress of the shredding process, you can use the -v (verbose) option:

sudo shred -vuz file1.txt

Example output:

The -z option adds a final overwrite with zeros to hide the shredding.

In the next step, you will learn how to use shred to overwrite and wipe entire disk partitions.

Overwriting Disk Partitions with shred

In this final step, you will learn how to use the shred command to overwrite and wipe entire disk partitions.

Note: This step will overwrite data on your disk partitions. Make sure you have backed up any important data before proceeding.

First, let's list the available disk partitions on your system:

sudo fdisk -l

Example output:

Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: Virtual disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x1234abcd

Device     Boot Start     End Sectors  Size Id Type
/dev/sda1        2048 41943039 41941992   20G 83 Linux

In this example, the disk partition we want to overwrite is /dev/sda1.

To securely overwrite the entire partition, run the following command:

sudo shred -vfz /dev/sda1

The options used are:

  • -v: Verbose mode, to show the progress of the shredding process.
  • -f: Force overwriting, even if the file appears to be a terminal.
  • -z: Add a final overwrite with zeros to hide shredding.

Warning: This command will completely overwrite the contents of the /dev/sda1 partition. Make sure you have backed up any important data before running this command.

Example output:

The shred command will overwrite the entire partition multiple times, making the data on the partition unrecoverable.

This concludes the lab on the shred command. You have learned how to securely delete files and overwrite disk partitions using this powerful tool.

Summary

In this lab, you learned about the shred command in Linux, which is used to securely delete files by overwriting their contents multiple times. You created a sample file and used the shred command to overwrite and delete it, exploring various options such as the number of overwrite iterations, adding a final zero overwrite, and removing the file after shredding. You also learned how to securely delete multiple files at once using shred. The lab covered the basics of using the shred command to ensure that deleted files cannot be recovered using common data recovery techniques.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like