rm - Command Usage Examples in Linux
Posted by Superadmin on February 27 2017 05:03:42

“rm” Command Usage Examples in Linux

This tutorial explains Linux “rm” command, options and its usage with examples.

rm – remove files or directories

 

DESCRIPTION

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.

SYNOPSIS

rm [OPTION]… FILE…

OPTIONS :

-d 
Attempt to remove directories as well as other types of files.

-f 
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.

-i 
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.

-I
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.

-P 
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.

-R
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.

-r 
Equivalent to -R.

EXAMPLES

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”.