This comprehensive Linux guide expects that you run the following commands as root user but if you decide to run the commands as a different user then ensure that the user has sudo access and that you precede each of the privileged commands with sudo

In this tutorial, we will show you how to delete Files and Directories using Command Line/Terminal in Linux. We will use rm command to delete files and directories. The rm command (short for remove) is a Linux command which is used to delete files from a file system.

Deleting Files

Caution:
Be extra careful when removing files with the rm command, because once the file is deleted it cannot be recovered.

In this tutorial, we are using commands with examples. So, replace the “path” with the file path, “filename” with the name of the file and “.ext” with the extension of the file.

Deleting a Single File

To delete a single file, run the command like this:

root@codesposts:~$ rm /path/filename.ext

If the file you are trying to delete is write-protected, you will be prompted with a warning that whether you want to delete the file or not. Type y and press Enter to delete the file.

Deleting Multiple Files

To delete more than one files, run the command like this:

root@codesposts:~$ rm filename1.ext filename2.ext filename3.ext

Deleting All Files Having a Specific Extension

To delete all the files with the specific extension, run the command like this:

root@codesposts:~$ rm /path/*.ext

All files having the extension “ext” will be deleted.

Confirming Before Deletion Of Files

To be extra careful while deleting the files, we use -i option with the rm command. -i stands for “Inquire”, when it is used, you will be asked to confirm with yes/no before deleting the file. For this, run the command like this:

root@codesposts:~$ rm -i /path/filename.ext

This option can also be used while deleting multiple files or deleting files with specific extension as described above.

Force Deletion (Deleting Without Any Prompt)

To avoid any prompt while deleting files, we use -f option with the rm command. -f stands for “Force”, when this command is used to make the deletion process quick. This will avoid any prompt even if the files are write-protected. For this, run the command like this:

root@codesposts:~$ rm -f /path/filename.ext

This option can also be used while deleting multiple files or deleting files with specific extension as described above.

Deleting Files With Report After Deletion

To get a report of the deleted file after the deletion, we use -v option with the rm command. -v stands for “Verbose”, when this option is used, You will get a report after the deletion. For this, run the command like this:

root@codesposts:~$ rm -v /path/filename.ext

Output:
removed '/path/filename.ext'

This option can also be used while deleting multiple files or deleting files with specific extension as described above.

Combining Different Options

You can combine the above described options with the rm command.

root@codesposts:~$ rm -fv /path/filename.ext

Deleting Files Starting With A Same String

If you want to delete some files having names starting with the same string, e.g. log1 log2 log3 log4 , you can use rm command like below:

root@codesposts:~$ rm -f log{1..5}.ext

Deleting Files Without Knowing The Extension

If you don’t know the extension of the file, or there are different files with the same name but different extensions. You can delete them by using the rm command as below:

root@codesposts:~$ rm -i /path/filename.*

This command will delete all the files having the name “filename” regardless of the extension.

Deleting Large Number Files

If your are trying to delete large number of files using rm command then you will get an error message ‘Argument list too long’

root@codesposts:~$ rm *.txt

-bash: /bin/rm: Argument list too long

To solve this issue, you can use the command given below:

root@codesposts:~$ find ~/path/ -type f -exec rm {} \;

Deleting Files Having Names Starting With Hyphen Symbol (-)

If you try to delete a file having name that starts with a hyphen symbol (-), e.g. -log.txt, you will get an error.

root@codesposts:~$ rm -log.txt

rm: invalid option -- 'l'

To avoid this error, you can use the command below:

root@codesposts:~$ rm ./\ -log.txt

OR

root@codesposts:~$ rm -- \ -log.txt

Deleting Directories

To remove/delete the directories (folders) using the rm command, you can follow the instructions given below.

In this tutorial, we are using commands with examples. So, replace the “path” with the file path, “directoryName” with the name of the Directory.

Deleting An Empty Directory

To delete an empty directory using rm command, run the command like this:

root@codesposts:~$ rm -d /path/directoryName

Deleting Non-Empty Directory

To delete a non-empty directory, you can run the command with the option -r. It stands for “Recursive”, it deletes all the sub-directories and files recursively.

root@codesposts:~$ rm -r /path/directoryName

Deleting Multiple Directories

Just like Deleting multiple files, you can delete multiple directories as well.

root@codesposts:~$ rm -r directoryName1 directoryName2 directoryName

Deleting Directory without Prompt

To delete a directory without having any prompt, we can use the option -f with the option -r.

root@codesposts:~$ rm -rf /path/directoryName