shred Linux Command: Syntax, Options, Examples

November 28, 2024

Introduction

When you delete a file in Linux, the operating system does not remove the file from the hard drive. It deletes the pointers to the file and marks the occupied space as reusable while the actual data remains intact.

Users can recover the deleted file until it is overwritten by other data. The shred command prevents the recovery of deleted files in Linux by overwriting the file with random data.

In this tutorial, you will learn how to use the shred command in Linux.

Prerequisites

Linux shred Command Syntax

The shred command is a part of the coreutils package, which is included in Linux by default. Open the terminal and enter the following command to check if shred is installed:

shred --version

In this example, the shred version is 9.4

Checking the shred version in Linux.

If the shred utility is not found, install it using the command for your Linux distribution:

Linux DistributionCommand to Install shred
Debian/Ubuntusudo apt install coreutils
Fedora/Rocky/CentOSsudo dnf install coreutils
Arch Linuxsudo pacman -S coreutils
openSUSEsudo zypper install coreutils

The basic shred command syntax is:

shred [options] [filename]
  • Options. Used to customize the shredding process, like setting the number of overwrites, file size, and output. Multiple options can be combined.
  • Filename. The name of the file you want to shred. If the file is in a different directory, you can specify its full or relative path.

shred is not a reliable method for erasing data on solid-state drives (SSDs) because wear leveling prevents overwriting of the same physical memory cells. Use SSD-specific secure erase commands or tools to erase data on solid-state drives.

Linux shred Command Options

The following table lists shred command options:

OptionDescription
-nSpecifies the number of overwrites (default value is 3).
-uOverwrite and delete.
-sAmount of bytes to shred.
-vShow detailed progress information.
-fForce shred command.
-zOverwrite with zeroes to hide shredding.
--versionshred version information.
--helpDisplay help with usage information.

shred vs. rm Command

The shred command conducts a series of overwrite operations to destroy data on the disk and significantly reduce the chance of data recovery. After the file has been overwritten, users can also delete the file with the -u option.

Use shred to prevent anyone from recovering sensitive files, such as an old passwd file containing outdated credentials.

shredrm
Use CaseOverwrites and securely deletes data.Deletes file references.
Data RecoveryVery difficult.Possible if not overwritten.
SpeedSlower due to the overwrite process.Fast.

The rm command removes the reference to the file in the file system, but the actual data remains on the disk until overwritten. It is a fast and convenient option if you are not concerned about unauthorized data recovery.

Linux shred Command Examples

The following section shows how to use shred and its options through practical examples.

Overwrite a File

The primary function of the shred command is to overwrite a file multiple times to destroy the data. To shred a file, use the following syntax:

shred [filename]

Replace filename with the exact name of the file or the file's path. If the file name contains spaces, enclose it in quotation marks. For example, the following command shreds the passwords test file:

shred "passwords test"
Example of using the shred command in Linux.

You can use the cat command to confirm the file's contents are unreadable after shredding.

Note: shred is not an effective command for overwriting log-structured or journaled file systems (e.g., JFS, ReiserFS, XFS, Ext3) commonly used in systems like AIX, Solaris, or RAID configurations.

Run shred with Verbose Mode

Verbose mode allows you to display additional extended information about the shredding process in the terminal. The -v option displays detailed progress information, including the number of overwrite passes:

shred -v [filename]
Running shred with verbose mode.

The output shows the progress of each pass in a new line.

Overwrite a File X Times

By default, shred overwrites files 3 times. The -n option allows users to specify how many times the file is overwritten:

shred -n [number] [filename]

In this example, the passwords file is overwritten 10 times:

shred -n 10 -v passwords
shred file in Linux multiple times.

The -v option displays the shredding progress in the terminal.

Overwrite and Delete a File

shred overwrites files but does not delete them by default. This is because the command is often used to overwrite entire device files and partitions, where deletion is redundant.

Important: Specify the exact partition number when overwriting and deleting a partition. If no partition number is specified, shred deletes the entire drive instead of a single partition.

Use the -u option to delete individual files after overwriting them:

shred -u [filename]

For example, the following command deletes the passwords file:

shred -uv passwords
Overwriting and deleting a file using shred in Linux.

The -v option is used to show the shredding progress.

Selectively Overwrite Bytes of Text

The -s option enables users to overwrite a specific portion of a file expressed in bytes. Suffixes like K (kilobytes), M (megabytes), and G (gigabytes) are also accepted.

The syntax is:

shred -s [number_of_bytes] [filename]

To overwrite the first 10 bytes of the passwords text file, enter:

shred -s 10 passwords
Selectively overwriting data using shred.

Only the first password in the file is shredded.

Change Permissions to Allow Writing if Necessary

The -f option forces file permissions to allow writing, even if the file is write-protected:

shred -f [filename]

Hide Shredding

The -z option overwrites a file with zeros after shredding. This action makes it less evident that the file was securely deleted. The syntax is:

shred -z [filename]

For example, to overwrite the passwords file with zeroes and display the process in the terminal, enter:

shred -zv passwords 
Overwriting a file with zeroes using the shred command in Linux.

Because the -z option was used, the final 4th pass overwrites the file with zeroes to hide the shredding activity.

Display Help

To view all available options and shred command details, use the --help flag:

shred --help
Shred command manual in Linux.

The list also includes tips and examples for effectively using the shred command.

Conclusion

This tutorial showed how to use the shred command to erase files from a file system permanently and the different options to customize the shredding process.

If you do not need to delete files, hiding files and folders in Linux can help keep sensitive or cluttered data out of sight.

Was this article helpful?
YesNo
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
Ubuntu File Manager and Alternatives
July 20, 2023

The default file manager in Linux is Nautilus. However, other file managers offer additional, more advanced features. Learn about Nautilus and its alternatives in this article.
Read more
How to Truncate Files in Linux
June 13, 2023

This guide shows several different methods for truncating a file in Linux. See concrete examples and get started with file management in Linux.
Read more
Linux Commands Cheat Sheet: With Examples
November 2, 2023

A list of all the important Linux commands in one place. Find the command you need, whenever you need it or download our Linux Commands Cheat Sheet and save it for future reference.
Read more
How to Use sed Command to Delete a Line
July 20, 2023

This article explains how to use sed to delete a line in a text file. Also, find out how to use patterns and conditions to delete multiple lines of text with a single sed command.
Read more