How to delete a file from a set of files in Linux using cmd
Linux provides its users with rm and unlink commands to delete files using the command prompt. However, while working with these commands, one needs to be careful as write permission on that directory.unlink command also returns ‘0’ upon successful completion of the command. If the file is write-protected, rm commands prompt for confirmation.
If the file we wish to delete is not in the current working directory, we may specify the path to the file. Alternatively, we may redirect to the directory where we wish to delete and then delete directly. If the file we wish to delete is in the current working directory, we may directly specify the file’s name with its extension.
rm
rm is an abbreviation of remove, a command-line tool that permits the user from removing files from the system. The rm command allows single and multiple file deletion, where filenames follow one another after a space. The syntax of the command is as follows:
rm [option] FILE1 //file is in current working directory
rm [option] ./path/to/FILE1 //if file is not in current working directory
OR
rm [option] FILE1 FILE2 FILE3 FILEN
where:
- FILE1,…FILEN represents the names of the file you wish to delete, e.g., filename.txt
Tip: If your file contains a
hyphen -in the beginning, use thermcommand in a slightly different manner fromrm -- -FILE1
Options, as mentioned in the syntax above, allow us to modify the behavior of the same command. Some available options include:
--versionsuccessfully returns the version ofrmactive on the current system
rm --version
--helpdisplays the help related to the command
rm --help
Verbose optionor-vprovides more information and insight into the performed actions-iis also referred to asInteractive Deletion. When the following option is used before deleting a file, the command will prompt the user for confirmation. For successful deletion, the user must press theYkey followed by theEnterkey. If any other key is pressed, the deletion operation would be unsuccessful, i.e., the file won’t be deleted from the system.-fis also referred to asForce Deletion. When the rm command is used with the following option, it overrides the file’s write protection, or other minor protections, and deletes it forcefully. It does not, however, function for write-protected directories.-r, also referred to asRecursive Deletion, enables the user to recursively delete all files and sub-directories (regardless of whether the directory is empty or not) in the parent directory. It deletes everything it finds at each stage. Typically,rmdoes not remove directories. However, this option does allowrmto successfully delete directories as well.
For instance, if the cmd is currently in the Educative Folder and you use the following command:
rm -r * // deletes everything in the current working directory
Everything, i.e., all files and directories within the folder Educative, will be deleted. However, if you delete a directory that contains a write-protected file or directory, you will be asked to confirm the deletion. Other examples of the command include:
rm -r filename //deletes the file named filename
rm -r directory // removes the directory with its content
- Sometimes, we may combine different options (For instance,
-rv,-rf, etc.).rfis a combination of recursive deletion and force deletion; in this case, we recursively and forcibly delete the directories and files in the current working directory or in the specified path to file/directory. When combined with recursive deletion,-rv, the verbose option provides more information on what the machine is doing when removing a file or directory.
The rm command may also delete multiple files that follow a pattern. We can define this pattern using wildcard (*) or regular expressions. Examples of the following are:
//assuming files are in the current directory, hence no path
rm *.txt //removes all txt extensions file
rm *.? //removes all files with a single character extension
rm *educative*.* //removes all the files with 'educative' in their name
To delete safely, some prefer to use -i, i.e., Interactive Deletion option with patterns. We can us the pattern and options together as well.
rm -i delFile/*
/*above command deletes all the files in delFile directly after prompting confirmation from the user. delFile is a folder in the current working directory here, else specify the path.
*/
rm -f *.pdf //removes all pdf extensions files in the current directory without prompt regardless if they are write-protected.
unlink
unlink is another command-line tool that permits you to remove a single file. Unlike rm, it does not allow us to delete multiple files simultaneously. If this constraint is violated and multiple filenames are given as parameters, an error occurs. The syntax of the command is:
unlink FILE
unlink ./path/to/FILE
where
- FILE is the name of the file we intend to remove.
Unlike the rm command, the unlink command only has two options:
--helpdisplays the help related to the command.--versiondisplays the version information.
unlink --help
unlink --version