Want to know how you can delete a file or a folder on your Linux machine? Maybe you have some unnecessary files that you want to remove from your system.

In this article, we will discuss everything related to deleting files and folders in Linux. We will also provide brief information on the various flags and options that you can use while deleting files and directories on your computer.

How to Delete a File in Linux

By default, Linux systems provide you with a way to delete files and directories using the terminal. Unlink, rm, and rmdir are built-in utilities that allow a user to clean their system storage by removing files that are no longer needed (rm stands for remove while rmdir denotes remove directory).

To delete a file using the unlink command, type:

        unlink filename
    

When you press Enter, the system will remove the hard link of the specified file with the storage. Note that you won't be able to delete multiple files using the unlink command. The rm command gets the upper hand in such situations.

To delete a single file using rm, type:

        rm filename
    

With rm, you will have to confirm the deletion of write-protected files by typing in y or yes. This is a security mechanism in Linux as most of the system files are write-protected and Linux confirms if the user wants to delete them. File and folder encryption on Linux is also possible if you are serious about protecting your system.

While deleting a write-protected file, you will see a prompt similar to the one below.

        rm: remove write-protected regular empty file 'filename'?
    

You can also pass multiple filenames separated with the Space character in order to remove more than one file.

        rm filename1 filename2 filename3
    

To delete all the files that have a specific extension, you can implement regular expressions in the rm command.

        rm *.txt
    

The aforementioned command will remove all the text files in the current working directory.

If you want to confirm the deletion of each file in a directory, use the -i flag with rm. The -i flag stands for interactive and will allow you to choose whether you want to delete the file or not. You will have to type y/yes or n/no to confirm your choice.

        rm -i *.txt
    

To delete files without the confirmation prompt, use the -f flag with the rm command. The -f stands for force or forcibly.

        rm -f filename1 filename2 filename3
    

There are various other rm options that you can use. You can also chain multiple options together to increase the efficiency of your command. For example, combining -i and -v together will display a prompt before deleting any specified file in verbose mode.

        rm -iv *.docx
    

Related: How to Create a New File in Linux

Removing Directories and Folders

On Linux, there are two command choices when it comes to deleting folders. You can either use the rmdir command or the rm command.

However, there is a slight difference between these two commands. With rmdir, you can only delete empty directories. If you have a folder that contains multiple files, you are bound to use the rm command.

To delete an empty folder using the rmdir command:

        rmdir /directory
    

If there's an empty directory that you want to remove, use the -d flag with the rm command. The -d flag stands for directory.

        rm -d /directory
    

Deleting multiple directories with the rm command is easy as well. Pass the name of the folders separated with the space character.

        rm -r /dir1 /dir2 /dir3
    

To delete a non-empty directory (folders containing files), use the -r option with the command. The -r flag or recursive flag will delete all the files and sub-folders of the specified directory recursively.

        rm -r /directory
    

Like files on Linux, if the directory is write-protected, rm will display a prompt that will ask you to confirm the removal again. To bypass the prompt, use the -f flag with the command.

        rm -rf /directory
    

You can also chain multiple options together while deleting folders. Also, it is possible to use regular expressions while deleting Linux directories.

File Management on Linux

Knowing how to organize storage on your computer by deleting files and folders is essential. You might bump into a situation where your file manager doesn't allow you to delete files and folders graphically. In such cases, getting rid of the files using the terminal is the only appropriate choice.

Sometimes, you might want to move a file to some other directory instead of deleting it completely from your system. Linux provides the mv command to change the location of files and folders on your system storage.