This tutorial explains Linux “rm” command, options and its usage with examples.
This command is used to delete files. rm removes each specified file. By default, it does not remove directories.
If a file is unwritable, the standard input is a tty, and the -f or –force option is not given, rm prompts the user for whether to remove the file. If the response is not affirmative, the file is skipped.
rm [OPTION]… FILE…
Attempt to remove directories as well as other types of files.
Attempt to remove the files without prompting for confirmation, regardless of the file’s permissions. If the file does not exist, do not display a diagnostic message or modify the exit status to reflect an error. The -f option overrides any previous -i options.
Request confirmation before attempting to remove each file, regardless of the file’s permissions, or whether or not the standard input device is a terminal. The -i option overrides any previous -f options.
Request confirmation once if more than three files are being removed or if a directory is being recursively removed. This is a far less intrusive option than -i yet provides almost the same level of protection against mistakes.
Overwrite regular files before deleting them. Files are overwritten three times, first with the byte pattern 0xff, then 0x00, and then 0xff again, before they are deleted. Specifying this flag for a read only file will cause rm to generate an error message and exit. The file will not be removed or overwritten.
Attempt to remove the file hierarchy rooted in each file argument. The -R option implies the -d option. If the -i option is specified, the user is prompted for confirmation before each directory’s contents are processed (as well as before the attempt is made to remove the directory). If the user does not respond affirmatively, the file hierarchy rooted in that directory is skipped.
Equivalent to -R.
1. To remove the file “accounts.txt” in the current directory you would type
$ rm accounts.txt
2. To delete a directory named “cases” with all its contents you would enter
$ rm -r cases
This assumes that the directory “cases” is a subdirectory of the current directory.
3. In order to delete a file that is not in the current directory you can specify the full path. For example,
$ rm /home/newuser/info
would delete the file “info” in the directory “/home/newuser/”.
4. You can selectively delete a subset of files using the wildcard character “*”. For example,
$ rm *.txt
would remove all files that end with “.txt”.
Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.
- Apply for Linux Internship
- Buy Linux Books
- Buy Information Technology Books
- Practice Programming MCQs
- Apply for Programming Internship