close
    search Buscar

    How to delete a file or directory in Linux

    Who I am
    Pau Monfort
    @paumonfort

    Item Feedback:

    content warning

    If you have root access on your Linux PC, you have the power to delete any file or directory. This can be dangerous, however, with tools like rm that allow users to accidentally delete crucial system files. That is why, if you are looking to delete files or directories in Linux, you need to take some precautions.


    There are a few ways to remove directories or files on Linux, using your Linux distribution's file manager or an open terminal (provided you have the right permissions). If you want to know how to delete a file or directory in Linux, here's what you need to do.



    Using a file manager to remove files or directories in Linux

    If you are new to Linux, you may be wary of using the terminal to delete files or folders in Linux. After all, there is no going back once a command like rm off to start deleting files. Rather than risk it, you can use the file manager included with your Linux distribution to delete files or folders. 

    Although Linux file managers differ in design, they should work in much the same way. This guide explores how to do this using Ubuntu's file manager, but the steps are likely to be similar for the file manager included in other distributions.


    1. To get started, open the file manager on your Linux distribution. This shouldn't be too hard to find, with the user icon probably in the form of a document folder. On Ubuntu, this app is called Files.
    2. In your distro's file manager, go to the directory containing the files or subfolders you want to delete. First, select the files or folders you want to delete. Once selected, right-click on them and select Move to Trash, Move to Trash o Delete, depending on your distribution and locale. You can also select the key Canc on the keyboard to achieve the same effect.
    3. Most distributions use a garbage system that allows you to "archive" files before they are deleted, giving you the ability to restore them. It is typically found on the desktop, as an entry in the file manager, or as an app that you can launch in the software menu. To complete the deletion process, navigate to the trash folder, then select the option Empty o Clear. These options may vary depending on the distribution.
    4. Alternatively, select your item (s), then right-click and select Delete from trash to remove them completely.

    How to remove files or directories in Linux using the rm command

    Removing files or folders using the distribution's file manager app works fine, but there are some limitations. For example, if you are not the owner of the file, you will need to open your file manager with root access using the terminal to delete it. Also, you can't use an app like this on a headless (terminal only) Linux installation.



    This is where command comes in rm. This Unix command dates back to 1971 and remains the fastest way to delete files or directories on Linux. Beware, however, of the command rm it has great power, especially if you're running it with sudo or directly as root, so you'll need to be careful using it.

    1. To remove a single file using rm, open a new terminal window (or a remote SSH connection) and type rm file, replacing the fillet with the correct file name. If you're not in the same directory, you'll need to use cd to move to it, or use the full path of the file instead (eg. Rm / path / to / file ).
    2. To remove multiple files, type rm file1 file2, replacing file1 e file2 with the correct file name and file path. You can add more files to this command to remove more than two.
    3. If you want to remove an empty directory on Linux, type rm -d directory, replacing directory with the correct directory path and name. If the directory is not empty and has subfolders or files, type rm -r directory, replacing the directory with the correct name and path. 
    4. You can also remove multiple directories at once by typing rm -r directory1, directory2, etc.
    5. If you want to remove all files or directories that match a partial file or directory name, you can use wildcard characters, such as an asterisk ( * ). To do this, type rm fil * o rm -r dir *, replacing the placeholders thread o dir with your filenames and directory paths.
    6. If the file or directory is protected, you may need to force it to be deleted. To do this, type rm -rf path, replacing the percorso placeholder with the correct directory or file path. This command involves extreme risk, so make sure the path you are using is correct before proceeding.
    7. If you are concerned about using the command rm -rf, you can force rm to ask for confirmation before any file or directory is deleted. To do this, use rm -i path, replacing path with your file name or directory path. For directories, use rm -ir path. For each item, select the key Y on your keyboard, then select Submit to confirm.

    If you want to know more about the possible options and arguments of rm, type man shred in the terminal to view the complete manual.




    Complete deletion of files using the shred command

    While commands like rm and unlink are great for deleting files in Linux, they leave traces. You (or someone else) could use these tracks to recover files from a hard drive or portable drive that you previously deleted. This may not be ideal, especially if you don't want the files to be recovered.

    To get around this, you can first overwrite the files using the command shred. This ensures that all data within the file is overwritten repeatedly before being deleted, ensuring that no traces of the file remain and preventing it from being recovered. For example, you may want to use this command to delete sensitive documents.

    1. To do this, open a terminal window or connect remotely using SSH and type shred file, replacing fillet with your file name and path. If you do not want to provide the path to the file, use cd to change to the directory containing the file or subdirectory you want to remove first.
    2. The command shred has additional parameters you can pass to make sure your file is safely deleted. Eg, shred -f will force deletion of write-protected files, while shred -u will ensure that the file is completely removed once it is destroyed, making sure it no longer appears in the file manager or using the command ls.
    3. If you want to overwrite the file more than the standard 3 times, use shred -n 0, replacing 0 with an alternate numeric value. If you want to hide the shred effect, use shred -z to force the use of a final run that overwrites the file with zeros.
    4. The command shred in it usually does not return any results to the terminal. If you want to see more details on what it is doing as it runs, use shred -v instead.

    As with rm, you can learn more by typing man shred in the terminal before using the command.




    • How to install Linux

    File protection on Linux systems

    When you remove a directory or file in Linux, you are freeing up space on your hard drive for the files that really matter, be it important documents or irreplaceable photos. It doesn't matter if you're using Linux Mint or any other major distribution, as the above steps should work for almost any Linux distro you choose to use.

    If you are worried about data loss, there are some steps you can take to protect your files. Although Linux systems are generally safer, a Linux antivirus can help protect your files from malware that may spread to your local network. You may want to consider automating a file backup using local or cloud storage as well.

    Further Reading:

    • Linux Shell, how to work inside
    • Install ADB and Fastboot quickly with Minimal Tool on Windows PC, Linux and MAC
    • How to install Discord on Ubuntu / Linux
    • Android One, internal memory full but I can't log in to delete it
    • Scarica Sony Mobile Flasher Tool: Flash Xperia Device
    add a comment from How to delete a file or directory in Linux
    Comment sent successfully! We will review it in the next few hours.