rm command is used for deleting files and directories in Linux.
Syntax
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.
Example
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
Syntax
rm file1 file2 file3 file4
Example
rm test1.txt test2.txt test3.txt test4.txt
Deleting multiple files with same extension can be done easily using wildcard character.
Syntax
rm *.extension
Example
rm *.txt
[adsenseyu1]
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.
Syntax
rm –i file_name
Example
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.
Syntax
rm –r directory
Example
rm-r test
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.
Syntax
rm –rf directory
Example
rm-rf test
[adsenseyu2]
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.
Syntax
rmdir directory_name
Example
rmdir test