How do you delete a directory or file on Linux?
If you’re new to Linux, you may be wondering how to delete a file. There are many ways to delete files on Linux. In this guide, we’ll cover the terminal method and a few GUI methods.
Delete a file on Linux with the terminal
To delete a file on Linux with the terminal, you’ll need a terminal window. Press Ctrl + Alt + T on the keyboard to open one up. Alternatively, search for “Terminal” in the app menu and launch it.
With the terminal window open, use the ls command to view the contents of your home folder.
ls
From here, you’ll see all the folders in your home directory. Use the cd command to move into the folder that has the file in which you wish to delete. For example, if you have a file you’d want to delete in the “Documents” folder, do the following.
cd ~/Documents/ rm name-of-file
Alternatively, if you wish to remove multiple files at once, you can. To remove the files, use the rm command followed by the name of each file you want to delete from your computer.
rm name-of-file-1 name-of-file-2
Lastly, if you wish to delete each file of a specific file type, you can use the rm command with the wildcard function. For example, to delete all JPG files from a directory, run:
rm *.jpg
Delete a file on Linux with the Gnome file manager
If you’re using Linux, there’s a big possibility that you’re using the Gnome Shell desktop environment. The Gnome Shell desktop uses the Gnome file manager. Here’s how you can delete a file using it.
To delete a file with the Gnome file manager, start by opening up “Files.” You can open “Files” by searching for “Files” in the app menu. With the “Files” app open, browse for the folder that holds the file you wish to delete.
Once you’ve entered the folder that has the file you wish to delete, select it with the mouse. Then, right-click on it, and select the “Move to trash” button. Alternatively, hold down Shift and right-click on it to permanently delete the file.
Delete a file on Linux with the KDE file manager
If you use Kubuntu, the KDE release of OpenSUSE, or another Linux OS that uses KDE Plasma, you’ll use the KDE file manager. Here’s how you can delete a file using it.
To start, launch the KDE file manager. You can launch it by searching for “Dolphin” in the app menu. Once it is open, browse for the folder that holds the file you wish to delete.
Select it with the mouse when you’ve located the file you wish to delete in Dolphin. Then, right-click on it with the mouse and select the “Move to Trash” option. Or, if you want to permanently delete it, hold down Shift and select the “Delete” option in the right-click menu.
How to delete a directory on Linux
Deleting a directory on Linux can be done in many ways, similar to deleting a file. Here’s how if you’re new to Linux and don’t know how to delete directories.
Delete a directory on Linux with the terminal
Deleting a directory on Linux with the terminal can be done with two commands. If the directory you wish to delete is empty, use the rmdir command.
For example, to delete the ‘test’ folder, which is empty, do the following.
rmdir test
If the directory you wish to delete on Linux is filled with files, you will need to use the rm command with the -rf command switch. For example, if the ‘test’ folder has files, you can delete the folder with the command below.
rm -rf test
Delete a directory on Linux with the Gnome file manager
Deleting a directory in the Gnome file manager is like deleting a file. To get rid of a directory, do the following. First, open up the Gnome file manager. You can do this by searching for “Files” in the app menu.
With the Gnome file manager open and ready to use, browse for the directory in which you’d like to delete. When you’ve located it, select it with the mouse. Right-click on the directory and choose the “Move to Trash” option.
Want to delete it rather than send it to the trash permanently? Hold down the Shift button while right-clicking and select the “Delete” option in the right-click menu.
Delete a directory on Linux with the KDE file manager
To delete a directory on Linux with the KDE file manager, start by opening it up. You can open the KDE file manager by searching for “Dolphin” in the app menu.
With the KDE file manager open, locate the directory you wish to delete from the system. Right-click on the file and select the “Move to Trash” button when you’ve found it. Or, hold down the Shift key and select the “Delete” button to delete your directory permanently.