The rm command removes each given FILE on the command line. By default, it does not remove directories. 


The syntax to delete the file for rm command is as follows:


rm file

To delete five files named file1, file2, file3, file4, and file5, run:


rm file1 file2 file3 file4 file5

To display the name of each file before removing it:


rm -v file

Use the ls command to list files:


ls -l
ls -ld dirname

To list all files and directories for given directory name, try to find command:


find dirname
find /path/to/dir/ -ls
find /path/to/dir2/ -type d ls
find /path/to/dir2/ -type f ls


Remove the listed directories and their contents recursively

To delete all files and its directories, run:


rm -r dir1
rm -r -v dirname2

You can force the rm command to ignore nonexistent files and missing operands, and never prompt the user:


rm -r -f myDirectory
rm -r -f -v /path/to/dir2/


How to prompt whether to remove each file with rm

Pass the -i or -I as follows:


rm -i fileNameHere
rm -i -r dirName
rm -I fileName

The -I option to the rm command prompts whether to proceed with the command, if more than three files are named or if a recursive removal is requested. Ignore any previous -f option.


rm vs rmdir command to delete directories

The rm command can delete both files and directories (empty or non-empty). The rmdir command can only remove empty directories. The syntax for rmdir command as follows:


rmdir dirnamehere
rmdir -v dir1

To delete non-empty directories try the rm command:


rm -rf dirName


Getting help on the rmdir command

Type the following command at the shell prompt:


man rmdir
rmdir --help


Getting help on rm command

Issue the following man command:


man rm
rm --help
  

Related services

KVM VPS hosting

NVMe SSD and full root access, with Free Basic Managed Support.

Unmanaged dedicated servers

full root access and free IPMI KVM, with Free Basic Managed Support.

Managed dedicated servers

our engineers look after the server for you, from $135 a month.

Was this answer helpful?

Still stuck? Open a ticket