Thursday 19 May 2011

rm command


Brief: This command is used to delete an existing file without taking confirmation. By default it does not delete directories but rm command can also be used with -r parameter to delete folderes/files recursively in a directory.

Syntax: rm filename.extension

Command: rm NewFile.txt

Description:

The rm command will delete an existing file in present working directory. If the user is working in /home/username/ directory then NewFile.txt file can be deleted using below command.
rm NewFile.txt

User can also specify the path where the existing file needs to be deleted in the rm command.
For ex: Assume that user is presently working in  /home/username/newfolder , but  wants to delete a file with name log.txt in /home/class/ then following command will do.
rmdir /home/class/log.txt
Here it is assumed that home and class directory already exist. If any folder in the path mentioned does not exist then an error will be shown.

Important Note: rm command can be used to recursively delete the directories/files from a leading directory.


Arguments:

-f : rm command cannot be used to delete the write-protected files and this is where -f paramater comes into the picture
If a file is write-protected then this command can be used to force the deletion of a file.
command: rmdir -f  marks.txt
The above command will make sure that file mkdir.txt is deleted.
If a directory is write-protected and we attempt to delete the file using -f parameter then file will not be deleted and no error message will be displayed.
command: rmdir -f /mysql/databasefile.txt
if mysql is write-protected directory then the databasefile.txt will not be deleted

-i : This parameter makes sure that linux system takes the confirmation from the user before deleting the file.
Command: rmdir -i myFile.txt
After the command is entered linux prompts the confirmation message on command prompt. If user enters 'Y' or 'y' value then file is deleted. Any other value entered will not delete the file.

-r or -R : It is the most important paramter in this command and is used to delete directories and sub-directory within an existing directory.
command: rmdir -r class
Assume that directory name 'class' contains directory name 'test' and test contains file 'myfile.txt'. Then above command will delete directory 'class', ' test' and 'myfile.txt'.


Important Examples:

1. Delete one or more file in a single command. Try below command
    rm /home/user/myfile1.txt /home/user/teacher.txt
    output: student and teacher file gets deleted

2. Delete all file starting with  name a in present working directory
    rm a*

3. Delete all files with extension .txt in present working directory
    rm *.txt

No comments:

Post a Comment