Linux: Delete files & folders using command line terminal

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.

  1. Open the command terminal, and the shortcut key for doing that is [ Ctrl ] + [ Alt ] + [ T ].
  2. 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. 
  3. Now, to know whether the file which we want to remove, is in the current directory, we can use ls command.
  4. To delete a file, you now type: rm filename.
  5. 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.
  6. 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.
In Linux delete file using terminal

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

  1. 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.
  2. With the help of cd command switch to the directory from where you want to remove the empty folder.
  3. To check whether the directory is in the currently selected folder, you can lsuse the command.
  4. 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.
  5. Example for the terminal: rmdir test
  6. Remember, the command rmdir stands for “remove directory” and only deletes empty directories.
Command to delete folder in Linux

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.

  1. Press the key combination [ Ctrl ] + [ Alt ] + [ T ] to open a terminal.
  2. 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.
  3. Again just like we did above, first, confirm whether the folder you want to remove is actually there or not using lsuse the command.
  4. Type in the command rm -r, followed by the directory you want to delete, and press [ Enter ].
  5. For Example in the terminal: rm -r demo
  6. The command rm -ris 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. Examplerm -rf foldername
command to delete directory or folder with files in Linux

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

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.