As we know Linux is slightly different from Windows for various tasks even in deleting files and folders. And here we will see the quick Linux commands to delete a file and directory using the terminal.
However, just like in Windows, system files or the important folders are only accessible by the admin, in Linux, they are under sudo or root users. Therefore, if want to delete any system file on Linux using GUI, then you should log in as root but that is a bit risky because you don’t want to run all your applications under an admin user.
Therefore, under the standard sudo
user, we can use the command terminal to easily delete any files and folders including empty ones.
Note: If you get a permission denied error while using any command given below then use sudo
with that.
In Linux, delete a file using terminal
To delete a file in Linux follow the below-given steps. Here we are using Ubuntu, however, the steps will be the same for all the available distros.
- Open the command terminal, and the shortcut key for doing that is [ Ctrl ] + [ Alt ] + [ T ].
- Switch to the directory where the files are located that you want to delete using a command
cd filename.
For example, a file in the /opt directory, so to switch into that type- cd /opt. - Now, to know whether the file which we want to remove, is in the current directory, we can use
ls
command. - To delete a file, you now type:
rm filename
. - The command
rm
stands for “remove”. For example, here we have a file BaseCamp_473.exe then the command will be rm BaseCamp_473.exe. The file with this name will then be deleted if that is available in the directory. - One thing that needs to be noted, using the command terminal and the rm command, the files will be deleted directly and not going to be moved to the recycle bin.

Delete Linux empty folder using terminal
The process to remove directories is also the same as files, however, the command will slightly be different because if we use rm
command then that will give an error such as “rm: cannot remove ‘test’: Is a directory“. Therefore, using it you cannot delete files in the terminal. Moreover, Linux also differentiates between empty and filled folders, and how to deal with them is given below.
Delete empty directory/folder
- Go to Applications and open Terminal, whereas Ubuntu, Manjaro, and other user-friendly Linux can also use the key combination [ Ctrl ] + [ Alt ] + [ T ] to open it.
- With the help of
cd
command switch to the directory from where you want to remove the empty folder. - To check whether the directory is in the currently selected folder, you can
ls
use the command. - Type in the command
rmdir foldername
, replace the foldername in the command with the directory you want to delete, and then press the [ Enter ] key. - Example for the terminal:
rmdir test
- Remember, the command
rmdir
stands for “remove directory” and only deletes empty directories.

Delete the Linux directory/folder that contains the files
Now, if there is some folder that contains files and folder then we cannot use rmdir
the command, for that simply use rm
with -r
flag will be in use. Moreover, we can even use this for empty directories as well.
- Press the key combination [ Ctrl ] + [ Alt ] + [ T ] to open a terminal.
- Go to the directory from where you want to remove the folder along with its file. To switch between folders we can use
cd
command. - Again just like we did above, first, confirm whether the folder you want to remove is actually there or not using
ls
use the command. - Type in the command
rm -r
, followed by the directory you want to delete, and press [ Enter ]. - For Example in the terminal:
rm -r demo
- The command
rm -r
is the abbreviation for “remove recursive” and deletes filled directories. Sometimes, if any folder is not getting deleted, we can use the -f flag means force, as well. Example–rm -rf foldername

Command to remove file with a dash ‘-‘
To remove a file whose name starts with a ‘-‘, for example ‘-h2s’,
use one of these commands:
rm -- -h2s
rm ./-h2s
Delete everything
This is really a disastrous command because it will remove all essential files on your Linux system and leave nothing but an unbootable panic system, thus this is here just for knowledge, and don’t try it on the system you are working. In short, your Linux will delete all directories on all hard drives and partitions.
sudo rm -rf /
The above command will give a warning, that this will remove everything, thus in case you want to use it then type – sudo rm -rf / --no-preserve-root
List of Flags to use with the rm command
-f, --force ignore nonexistent files and arguments, never prompt
-i prompt before every removal
-I prompt once before removing more than three files, or when removing recursively; less intrusive than -i, while still giving protection against most mistakes
-r, -R, --recursive remove directories and their contents recursively
-d, --dir remove empty directories
-v, --verbose explain what is being done