How To Securely Delete Files in Linux

Deleting files on your Linux computer seems easy. However, the system only removes the file, but it isn’t permanently deleted. Typically, you can recover deleted files using Linux data recovery tools. However, if you have sensitive information files, you can use a more secure method so they can’t be viewed or recovered after deletion. This article will show you a few ways to delete files in Linux securely.

The tools you can use to erase files securely in Linux include:

Keep reading to learn how to use these tools to delete files in Linux securely.

Note: Do Not Use Secure Deletion Tools on Solid State Drives (SSDs)

The tools listed above are only for regular spinning hard drives like HDDs. However, you shouldn’t use these secure deletion tools on a solid-state drive (SSD). Users can only write files to an SSD a specific amount of time. When you use secure deletion tools, you only add to the amount of data written on the drive. This could cause the SSD to fail sooner. 

To securely wipe files from an SSD, users should check the manufacturer's manual before proceeding. The manual might recommend using specific tools for the task. Alternatively, consider encrypting the hard drive. This allows you to delete the file normally while ensuring high data security.

Method 1: Securely Delete Files in Linux Using secure-delete

Secure-delete is a tool for Linux secure delete file process. It allows you to securely delete files by overwriting the hard disk space containing them during deletion. There are four tools in the secure-delete set. In this section, we’ll be focusing on the srm tool. The other three commands allow you to overwrite the free space on the disk, overwrite the swap space, and wipe the RAM. Below is a breakdown of the tool:

How To Install secure-delete in Your Linux

Installing secure-delete on your PC can be simple if you know the exact steps. Below, we’ve outlined the steps required to install secure-delete in Ubuntu.

Step 1: Press Ctrl + Alt + T simultaneously to open a terminal window.

Step 2: Type the following command and press Enter to proceed. It works on Ubuntu and other Debian-based Linux distributors like PureOS and Linux Mint.

sudo apt-get install secure-delete

install secure delete in linux

Step 3: You will be required to provide your password. Enter your account password and then press Enter.

The installation process will begin immediately, and once it's complete, you can launch to start the Linux secure delete file process.

How To Securely Delete a File in Linux Using the srm Command

The srm tool is quite similar to the rm command in many ways. It removes and deletes files in Linux. However, the srm command overwrites the file several times with random data before permanently deleting it. To securely delete files in Linux using the srm command, follow the below steps.

Step 1: Press Ctrl + Alt + T to open a terminal window.

Step 2: Now, type the command below to delete files securely in Linux. Replace the path in the command line with the path to the file you would like to delete. Put quotation marks around the entire path if there’s a space in the file or directory name. The syntax for using the srm command is:

Sudo srm /home/lori/Documents/delete-this-file.txt

securely delete files in linux with srm

You don’t need to confirm your options when deleting files using the srm command. Therefore, before running this command, you must confirm you’re deleting the right file. 

The srm command has several options you can use to customize its outcome. Here’s a breakdown of each option:

  • -d: This option works to delete directories. When you run the srm command with this option, it will recursively delete the content in the directories and then remove the directories.
  • -f: This option forces the deletion of files and directories without asking for confirmation. When you use this option, srm will not ask for confirmation before removing files and directories. 
  • -l: This option shows the progress of the deletion. When used, the srm will provide a display bar and the percentage of deleted files.
  • -r: This is for recursively deleting content and their directories. This means when used with the srm command, it doesn’t just delete the directory but any file and subdirectories within it.
  • -v: This helps you discover the version of srm in use
  • -z: When used with the srm command, this option overwrites deleted files with zeros rather than random data. This could be useful in some situations, like deleting files on solid state drives (SSD) where overwriting the drive with random data isn't practical.

Note: Because of the secure deletion process, the srm command takes longer to complete than the rm command. It's also a powerful tool for permanent files and directories. Therefore, you should use the command with caution.

How To Securely Delete a File in Linux Using the sfill Command

If you’re concerned about a file you’ve deleted using the rm command and want to ensure that it isn’t recovered, the best option is sometimes overwriting it. That’s how the sfill command works; it will overwrite all the free space on your hard drive.

As it does this, you'll notice less free space on your hard drive until there's no free space left. When the sfill command completes the process, it will release all your free space back to use. Therefore, if you're running a multi-user system, this could be very disruptive. It is a maintenance task that you should conduct out of hours.

Even when used on a single-user computer, losing hard drive space would mean it’s unusable once sfill uses all the space. It comes with some options that would make it much easier to run this command to delete files securely: they include:

  • -l: This option works to lessen security once used with the sfill command
  • -v: This is the verbose option and displays the progress of the sfill 
  • -z: This overwrites the free space with zeroes rather than random data. 

Below is an example of using the sfill command to securely overwrite all the free space in the /home directory.

sudo sfill -lvz /home

securely delete files in linux with sfill

The sfill command also takes time to complete because it is overwriting the free space on your drive. Therefore, you might want to settle in after running this command. 

How To Securely Delete a File Using the sswap Command

As the previous commands explored, the sswap command is a secure-delete tool for a secure delete Linux process. It overwrites the storage in your swap partition. The process is more complex than others but is broken down below.

Step 1: You need to identify the swap partition. To do this, you’ll need to use the bllkid command.

sudo blkid

check the swap partition

Step 2: Once the list appears, locate the word ‘swap’ and make a note of the block device it is attached to.

check swap block device

Step 3: You must turn off disk write to this swap partition for as long as the overwriting process occurs. You can use the swapoff command displayed below:

sudo swapoff /dev/sda5

turn off write disk

Step 4: Now, you can use the sswap command. Like other tools, you can use the -v (verbose) option and the -l (lessen security) option with the sswap command. The command to run is:

sudo sswap -llv /dev/ada5

securely delete files in linux with sswap

Once you run this command, sswap starts going through your swap partition and overwrites everything. It takes little time as other commands.

Once complete, you must reinstate the swap partition as an active space. You can do this using the swapon command.

sudo swapon /dev/sda5

activate swap partition

How To Securely Delete a File in Linux Using the sdmem Command

The secure-delete package also contains the sdmem command that wipes your computer’s Random Access Memory (RAM) chips. This tool is essential because malware attacks sometimes result in data retrieval from your RAM chips. You can use the sdmem command with options like the -v (verbose) option and the -ll (lessen security) option. The syntax for this command is:

sudo sdmem -vll

securely delete files in linux with sdmem

When you run this command, the terminal window will become filled with asterisks. This indicates that the sdmem command is working its way through and clearing your RAM chips. 

sdmem deletion process

Method 2: Securely Delete Files in Linux Using shred

The shred command is another efficient option to delete files in Linux securely. You can use the shred command to overwrite a file multiple times with randomly generated data to make it unrecoverable. Afterward, you can choose to delete the file. 

The shred command does the overwriting task for you so that no one can recover the deleted file from Linux. You can find this command in all Linux distributions, and we tested it on Ubuntu, Manjaro, and Fedora to confirm. 

How To Check if shred Is Installed in Your Linux

Shred is usually installed by default on Linux distributions. However, it’s always best to check and find its installation path. To check if Shred is installed on your Linux, follow the steps below:

Step 1: Press Ctrl + Alt + T open a terminal window

Step 2: Type the command below

whereis shred

check shred location

Step 3: Now, press Enter to confirm, and Linux will inform you where shred is located if you can find it on your PC.

How To Securely Delete a File in Linux Using shred

In this example, we’ll be deleting a file called poem.txt. The syntax to securely delete this file in Linux using the shred command is:

shred -vzu -n5 poem.txt

securely delete files in linux with shred

where:

  • -v meansverbose, which gives a detailed output of the process.
  • -z: replaces the final pass with zeroes rather than random data to hide shredding.
  • -u: This removes the file after the shredding. This means users don’t have to remove the file using the rm command after the process.
  • -n: This option changes the number of passes. In the example above, we set it to 5.

Method 3: Securely Delete Files in Linux Using Wipe

Another option for the Linux secure delete file process is the wipe command line utility. This tool writes special patterns 34 times to file. Eight of these special patterns are entirely random. The wipe tool is one of the most thorough methods of securely deleting files in Linux, and it can take some time due to its thoroughness. 

The wipe command allows you securely erase data from the hard disk permanently. It erases files from the magnetic memory and then rewrites the space repeatedly. It will also wipe away the caches, which makes the data impossible to recover. 

How To Install wipe in Your Linux

Installing wipe in your Linux is relatively easy; follow the steps outlined below to get it done.

Step 1: Press Ctrl + Alt + T to launch a terminal window.

Step 2: Enter the command below. Note that this command works on Ubuntu and all Debian-based Linux distributions like PureOS and Linux Mint.

sudo apt-get install wipe

install wipe in linux

Step 3: Now, press Enter to confirm the command.

Step 4: You must provide your account password to proceed with the installation process. Type in the password and then press Enter to proceed.

How To Securely Delete a File in Linux Using wipe

Now that you have wipe installed in your Linux, follow the steps below to securely delete a file using this tool.

Step 1: Open a terminal by pressing Ctrl + Alt + T simultaneously

Step 2: On the terminal, enter the command below and replace the path in the command with the path to the file you want to delete. 

wipe /home/lori/Documents/delete-this-file.txt

securely delete files with wipe

If there’s a space in the directory name or file name, add quotation marks around the path.

Step 3: If the file you are deleting is large, use the quick mode option (-q). This overwrites the file four times by default. To use this option, type the command below:

wipe -q /home/lori/Documents/delete-this-file.txt

quick mode for deleting large files

Step 4: You can specify the number of passes you want to take a quick wipe. Use the- Q option if you want something more than four. 

Method 4: Securely Delete Files in Linux Using dd

The dd command is mostly used to copy and convert files. However, you can also use it for the secure delete Linux process by overwriting your hard drive with zeros. You should note that the dd command will not zero a drive presently in use. 

The syntax for using dd is:

dd if:<source> of=<target> [options]

An example of using this file is running the command

sudo dd if=/dev/urandom of=<deletefile.txt> bs=1M count =1

Where:

  • if=dev/urandom is an option that tells the dd command to read random data from the /dev/urandom device
  • of=<deletefile.txt> is an option that tells the dd command to write the output of this specific file.
  • bs=1M is an option that informs dd to utilize a block size of 1 megabyte
  • count=1 is an option that tells dd to write only a single block of data. 

Once you run the command and it is complete, you can verify that the file has been deleted using a recovery tool to try and recover the file. Like other methods reviewed in this article, the dd command is irreversible, and the data will be overwritten. Therefore, confirm the file you would like to overwrite before proceeding.

Summing Up

Linux provides several easy ways to remove files, but they’re not entirely secure. Like the process to securely delete files on Windows, these tools only take a few minutes to complete. If you use a public computer and don’t want someone else to access your files, use the methods above, and just like the processes to securely delete files on Mac, your files will be gone unrecoverable in no time. 

You May Also Like