rm command is used for deleting files and directories in Linux.
rm /file_path/file_name rm file_name
First command is deleting with absolute path has been provided. In second command file name is given assuming file is in working directory.
rm test rm /test/test1
Delete multiple files
Deleting multiple you need to provide names of file you want to delete along with rm command
rm file1 file2 file3 file4
rm test1.txt test2.txt test3.txt test4.txt
Deleting multiple files with same extension can be done easily using wildcard character.
Interactively delete files from Linux system
Using rm command to delete files from Linux has a limitation that it does not ask for confirmation before deleting a file. To get a message before deleting use –i switch with rm command.
rm –i file_name
rm -i test.txt
Delete a directory with all files within
rm command can delete directories with all files within it. For deleting directory rm along with –r switch and file name.
rm –r directory
The above command will delete all files from test directory and finally remove test directory. It will ask for confirmation before deleting of all files from directory. If you don’t want prompt to appear before deleting of all files use –f (force) switch.
rm –rf directory
Use of rmdir command to delete directories
If you have an empty directory you can also use rmdir command. The difference in rmdir from rm command is rm directory will delete all file and directories within when you used with –r switch but rmdir requires empty directory else it will not execute.