- 3 Ways to Permanently and Securely Delete ‘Files and Directories’ in Linux
- 1. Shred – Overwrite a File to Hide Content
- 2. Wipe – Securely Erase Files in Linux
- 3. Secure-deletetion Toolkit for Linux
- 4. sfill -Secure Free Disk/Inode Space Wiper
- 5. sswap – Secure Swap Wiper
- 6. sdmem – Secure Memory Wiper
- If You Appreciate What We Do Here On TecMint, You Should Consider:
- How To Delete File In Linux?
- rm Command Syntax
- rm Command Help
- Delete Single File with rm Command
- Delete Multiple Files with rm Command
- Delete Files According To Their Extensions/Types with rm Command
- Delete Files Recursively
- Delete File with Prompt Before Every Removal
- Print Verbose Output About Delete Operation
- Delete empty Directories or Folders with rmdir Command
- Read File Names From Text File For Delete or Removal
- Delete File Names Starts with Dash —
- Delete Files By Reading Their Names From A File/List
- Delete Files By Finding them with find Command
- How to Delete Files and Directories in Linux
- Deleting a file
- Deleting multiple files
- Remove files interactively
- Force remove files
- Removing empty directories
- Deleting non-empty directories
- Deleting multiple directories recursively
- Forcefully and recursively remove directories
- Wrapping up
3 Ways to Permanently and Securely Delete ‘Files and Directories’ in Linux
In most cases the means we use to delete a file from our computers such as using Delete key, Trash files or rm command, which do not permanently and securely remove the file from the hard disk (or any storage media).
The file is simply hidden from users and it resides somewhere on the hard disk. It can be recovered by data thieves, law enforcement or other threats.
Assuming a file contains classified or secret content such as usernames and passwords of a security system, an attacker with the necessary knowledge and skills can easily recover a deleted copy of the file and access these user credentials (and you can probably guess the aftermath of such as scenario).
In this article, we will explain a number of command line tools for permanently and securely deleting files in Linux.
1. Shred – Overwrite a File to Hide Content
shred overwrites a file to hide its contents, and can optionally delete it as well.
In the command below, the options:
- -z – adds a final overwrite with zeros to hide shredding
- -v – enables display of operation progress
- -u – truncates and removes file after overwriting
- -n – specifies number of times to overwrite file content (the default is 3)
shred – overwrite a file to hide its contents
You can find more usage options and information in the shred man page:
2. Wipe – Securely Erase Files in Linux
A Linux wipe command securely erases files from magnetic memory and thereby making it impossible to recover deleted files or directory content.
First, you need to install wipe tool in order to it, run the appropriate command below:
The following command will destroy everything under the directory private.
where the flags used:
- -r – tells wipe to recurse into subdirectories
- -f – enables forced deletion and disable confirmation query
- -i – shows progress of deletion process
Wipe – Securely Erase Files in Linux
Note: Wipe only works reliably on magnetic memory, therefore use the other methods for solid state disks (memory).
Read through the wipe man page for additional usage options and instructions:
3. Secure-deletetion Toolkit for Linux
Secure-delete is a collection of secure file deletion tools, that contains srm (secure_deletion) tool, which is used to remove files securely.
First you need to install it using the relevant command below:
Once installed, you can use srm tool to remove files or directories securely on a Linux system as follows.
where the options used:
- -v – enables verbose mode
- -z – wipes the last write with zeros instead of random data
srm – Securely Delete Files in Linux
Read through the srm man page for more usage options and information:
4. sfill -Secure Free Disk/Inode Space Wiper
sfill is a part of secure-deletetion toolkit, is a secure free disk and inode space wiper, it deletes files on free disk space in a secure method. sfill checks the the free space on the specified partition and fills it with random data from /dev/urandom.
The command below will execute sfill on my root partition, with the -v switch enabling verbose mode:
Assuming you created a separate partition, /home to store normal system users home directories, you can specify a directory on that partition to apply sfill on it:
The are a few limitations of sfill that you can read about in the man page, where you can also find additional usage flags and instructions:
Note: These following two tools (sswap and sdmem) in the secure-deletetion toolkit are not directly relevant for the scope of this guide, however, we will explain them for knowledge purpose and future use.
5. sswap – Secure Swap Wiper
It is a secure partition wiper, sswap deletes data present on your swap partition in a secure manner.
Caution: remember to unmount your swap partition before using sswap! Otherwise your system might crash!
Simply determine you swap partition (and check if paging and swapping devices/files are turned on using swapon command), next, disable paging and swapping devices/files with swapoff command (renders swap partition unusable).
Then run sswap command on the swap partition:
sswap – Secure Swap Wiper
Make an effort to read through the sswap man page for more usage options and information:
6. sdmem – Secure Memory Wiper
sdmem is a secure memory wiper, it is designed to remove data present in your memory (RAM) in a secure manner.
It was originally named smem, but because on Debain systems there exists another package called smem – report memory consumption on per-process and per-user basis, the developer decided to rename it sdmem.
For more usage information, read through the sdmem man page:
That’s it! In this article, we reviewed a number command line tools for permanently as well as securely deleting files in Linux. As usual, offer your thoughts or suggestions about the post via the comment form below.
If You Appreciate What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
We are thankful for your never ending support.
Источник
How To Delete File In Linux?
Deleting files in Linux can be sometimes tricky. We have a tool named rm which is the shortcut for the word remove. In this tutorial, we will look at how to remove or delete a file in Linux with different examples and ways.
rm Command Syntax
rm command syntax is the same as the most the Linux command. We can provide options before specifying the file and directories we cant to operate or delete.
- OPTINS states the behavior of the rm command as we will see below in detail.
- FILENAME is the file or directory name we want to delete or operate.
rm Command Help
rm command help information can be printed to the console with the —help command which is like below. Help information will provide some popular options and usages.
Delete Single File with rm Command
We will start with simple steps just deleting a single file. We will just specify the file name we want to delete. In order to remove the file successfully, we should have privileges to modify a file. For example, if we try to remove the file owned by root with a regular user we will get an error and would not delete the file. In this example, we will delete the file named foo.txt
Delete Multiple Files with rm Command
We have the ability to delete multiple files in a single rm command. We will just put file names we want to delete by separating them with space. In this example, we will delete file names foo.txt and bar.txt but we can add more if we need it.
Delete Files According To Their Extensions/Types with rm Command
Linux bahs provides the glob or asterisk in order to specify the files with a specific name or extension. We can use glob * in order to specify a specific extension like *.txt , *.pdf , *.tmp etc. We can use this extension or name specification in order to delete specific files. In this example, we will delete all *.deb extensions.
We can also specify names like deleting all files which name starts with pof like below.
Delete Files Recursively
rm command provides the ability to delete or remove files recursively. Recursive removal will check subdirectories for files to remove with the directories. We will remove the directory name ndiff with all sub-directories and files in this example. We will use -R option for the recursive operation.
Delete File with Prompt Before Every Removal
While removing files and directories we may need approval for each file to delete. In this case, we can use -i option which will prompt to accept or deny deletion of the given file.
Print Verbose Output About Delete Operation
While deleting files and directories we may want to see details of the removal operation. rm command provides a verbose option which will list information about each deletion of file or directory. We will use -v option for this.
Delete empty Directories or Folders with rmdir Command
In some cases, we need to delete empty folders. rm without options will not work in this case as we can see this in the following screenshot. We case use rmdir command to remove an empty directory or folder.
Read File Names From Text File For Delete or Removal
Another interesting use case for rm command is providing file or directory names from a list like a text file. We will use xargs command to-read the list and redirect to the rm command.
Delete File Names Starts with Dash —
Another interesting case is dash or — problem where file or directory names starting with dash . As we know Linux commands options are specified with dash . So how can rm recognize file name from option? We will use — or double dash were to specify the file or directory name start. For example we have a file named -file.txt and we want to remove. We will use the following command. As we can see the file name is specified after double dash. Options are specified before the double dash.
Delete Files By Reading Their Names From A File/List
In some cases, we may need to read a list that contains the file names we want to delete. This is generally a simple text file where each file name with their path is specified line by line. We can use xargs command to redirect the list contents to the rm command which will delete them one by one. In this example, we will read the list file names file-list.txt .
Delete Files By Finding them with find Command
find is a very useful command which is used to find files and folders. find command also provides some options like running commands on the search results. We can also remove or delete files found by the find command. In this example, we will delete files that are older than 3 days.
Источник
How to Delete Files and Directories in Linux
You may, in some cases, need to delete directories and files that you do not need to free up space on your hard drive. In this brief tutorial, you will learn how to delete files and directories in Linux.
To remove files, the rm command is used. A word of caution, using the rm command to delete files and directories in Linux is irreversible. Therefore, extra caution should be taken.
Deleting a file
To delete a single file using the rm command, use the syntax below
For example, we will create a text file file1.txt using the touch command. To delete the file run
Output
Deleting multiple files
To remove many files in a single command using the rm command, use the syntax
Once again, we will create 3 files — file1.txt file2.txt file3.txt using the touch command. To remove 3 files — — in one command run
Output
This result can also be achieved by making use of the wildcard symbol ( * ). In the example below, The wildcard matches and deletes all files with the extension .txt
Remove files interactively
If you wish to confirm whether to remove files before deleting them, use the -i flag as shown
Output
Force remove files
To remove files forcefully, especially ones that have been write-protected, use the -f flag as shown below
Output
Removing empty directories
If you want to remove an empty directory, use the -d flag with the rm command
Let’s create an empty directory and call it data
To remove the directory run
Output
The same result can be achieved using the rmdir command. This is short for remove directory. The syntax is as shown below.
In our second example, we are going to create a new directory and call it linux . We will then remove it using the rmdir command.
Output
Deleting non-empty directories
If you want to remove non-empty directories together will all files, use the -r flag as shown
In this example, we have a directory called distros containing 5 files namely centos, debian, fedora and mint.
To remove the entire directory alongside its contents run
Deleting multiple directories recursively
If you wish to delete or remove multiple directories at once use the syntax below
For instance, to remove 3 directories namely data1 data2 & data3 run
Output
Forcefully and recursively remove directories
If you want to recursively and forcefully remove directories without being prompted, use the combination of -rf flag as shown below
Wrapping up
Using the examples in this article, you now know how to delete a file in Linux safely. We value your feedback. Drop us a comment in the comment section. Keep it locked for more informative and insightful articles.
Источник