In this article, I will teach you. How to delete a file or directory in Linux? If you have root access to your Linux Operating System, you have the ability to delete any file or directory you want. This can be harmful, nonetheless, with tools like rm allowing users to carelessly erase crucial system files accidentally. That’s why, if you’re making an attempt to delete files or directories in Linux, you’ll want to take some precautions.
There are a number of ways you can remove directories or files on Linux, using either your Linux distribution’s file manager or an open terminal (so long as you have the right permissions). If you want to know how to delete a file or directory in Linux, right here’s what you’ll need to do.
Here are the best 3 ways to delete a file or directory in Linux:
1. File Manager to Remove Files or Directories in Linux
If you’re a Linux beginner, you might be cautious about using the terminal to delete files or folders in Linux. After all, there’s no turning back once you set commands like rm off to start deleting files. Rather than danger this, you can use the file manager packaged along with your Linux distribution to delete files or folders as an alternative.
Whereas Linux file managers do vary in design, they need to work in a lot the same way. This information explores how to do this using Ubuntu’s file manager, however, the steps are more likely to be similar for the file manager included in other distributions.
Step 1. To start out, open the file manager on your Linux distro. This shouldn’t be too hard to search out, with the user icon likely in the shape of a document folder. On Ubuntu, this app is named Files.
Step 2. In your distro’s file manager, navigate to the directory containing the files or subfolders you want to delete. Then you can select the files or folders you want to delete. Once you are selected, right-click on them and select Move to Trash, Move to Bin, or Delete, depending on your distribution and locale. You can also select the Delete key on your keyboard to achieve the same impact.
Step 3. Most distributions operate a trash system that permits you to “store” files before they’re deleted, giving you the possibility to restore them. This is typically found on the desktop, as an entry in your file manager, or as an app, you can launch in your software menu. To finish the deletion process, enter the trash folder, then select the Empty or Erase option. These options may vary, relying on your distribution.
Finally, select your item (or items), then right-click and select delete from Trash to completely remove them instead.
2. Remove Files or Directories in Linux Using the Command
Removing files or folders using your distribution’s file manager app works fine, however, there are some limitations. For example, if you’re not the owner of the file, you’ll need to open your file manager with root access using the terminal to delete it. You can also use an app like this on a headless (terminal-only) Linux installation.
That’s where the rm command comes in. This Unix command dates back to 1971 and remains the quickest strategy to delete files or directories on Linux. Be warned, however—the rm command has great power, particularly if you’re running it with sudo or as the root user directly so that you’ll need to maintain using it.
Step 1. To remove a single file using rm, open a new terminal window (or remote SSH connection) and type rm file, replacing the file with the correct file name. If you’re not in the same directory, you’ll need to make use of cd to move to it first, or use the full file path (eg.
rm /path/to/file) as a substitute.
Step 2. To remove multiple files, type rm file1 file2, replacing file1 and file2 with the right file name and file path. You can add extra files to this command to remove more than two.
Step 3. If you want to remove an empty directory on Linux, type rm -d directory, replacing the directory with the right directory name and path. If the directory isn’t empty and has sub-folders or files, type rm -r directory, replacing the directory with the right title and path.
Step 4. It’s also possible to remove a number of directories at once by typing rm -r directory1, directory2, and so on.
Step 5. If you want to remove all files or directories that match a partial file or directory name, you can use wildcards, such as an asterisk (*). To do this, type rm fil* or rm -r dir*, replacing the placeholders fil or dir with your personal file names and directory paths.
Step 6. If the file or directory is protected, you may need to force its deletion. To do this, type the rm -rf path, replacing the placeholder path with the right directory or file path. This command carries extreme danger, so ensure that the path you’re using is correct before you proceed.
Step 7. If you’re worried about using the rm -rf command, you can force rm to ask for confirmation before each file or directory is erased. To do this, use the rm -i path, replacing the path with your personal file name or directory path. For directories, use the rm -ir path instead. For every entry, select the Y key on your keyboard, then select Enter to confirm.
If you want to study more about the possible rm options and arguments, type man shred in the terminal to view the complete manual.
3. Totally Erasing Files Using the shred Command
While commands like rm and unlink are nice for deleting files in Linux, they do leave traces. You (or someone else) could use these traces to recover files from a hard drive or portable storage that you’ve simply before deleted. This might not be perfect, especially if you don’t want the files to be retrieved.
To get through this problem, you can overwrite files first using the shred command. This makes sure that all data inside the file is overwritten repeatedly before its deleted, making certain no traces of the file are left and preventing it from being retrieved. You may want to use this command to erase sensitive documents, for example.
Step 1. To perform this, open a terminal window or make a connection remotely using SSH and type shred file, replacing the file with your personal file name and path. If you don’t want to provide the path to the file, use cd to travel to the directory containing the file or sub-directory you want to remove first.
Step 2. The shred command has further parameters you can pass to make sure your file is safely deleted. For example, shred -f will force the deletion of write-protected files, while
shred -u will make sure the file is totally removed once shredded, ensuring it no longer appears on your file manager or use the ls command.
Step 3. If you wish to overwrite the file more than the standard 3 times, use shred -n 0, replacing 0 with an alternate numerical value. If you wish to hide the shred effect, use shred -z to force it to make use of the last run that overwrites the file with zeros.
Step 4. The shred command doesn’t sometimes output any results to the terminal. If you want to see extra detail about what it’s doing as it runs, use shred -v instead
Whenever you remove a directory or file in Linux, you’re freeing up space on your hard drive for the files that really matter, whether or not it’s necessary documents or irreplaceable photos. It doesn’t matter whether or not you’re using Linux Mint or another major distribution, because the steps above should work for almost any Linux distro you choose to make use of.
When you’re worried about data loss, there are some steps you’ll be able to take to protect your files. While Linux systems are typically more secure, a Linux antivirus may also help you to keep maintain your files secure from malware that could spread throughout your local network. It’s possible you’ll want to consider automating a file backup using local or cloud storage, too.
Check more daily updates to get subscribe to our website and also you can get informational articles and much more etc.