Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

linux
communitycreator

How to delete a file from a set of files in Linux using cmd

Shanzay Gauhar

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 the deleted file does not move to Trash or the Recycle bin.They are deleted from your machine right away, retrieving data after deletion is only possible if a backup exists. For the directory that contains the file you want to remove, you must have write permission on that directory.Otherwise, the operation won’t be permitted, and the permission error will be denied. For instance, if you try to delete the file in the directory owned by the root directory without root privileges, the error will occur. If the deletion operation is successful, both commands will not produce any output. However, both commands will produce output in case of an error. The 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 the rm command in a slightly different manner from rm -- -FILE1

Options, as mentioned in the syntax above, allow us to modify the behavior of the same command. Some available options include:

  • --version successfully returns the version of rm active on the current system
rm --version
  • --help displays the help related to the command
rm --help
  • Verbose option or -v provides more information and insight into the performed actions
  • -i is also referred to as Interactive 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 the Y key followed by the Enter key. If any other key is pressed, the deletion operation would be unsuccessful, i.e., the file won’t be deleted from the system.
  • -f is also referred to as Force 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 as Recursive 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, rm does not remove directories. However, this option does allow rm to 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.). rf is 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:

  • --help displays the help related to the command.
  • --version displays the version information.
unlink --help
unlink --version

RELATED TAGS

linux
communitycreator
RELATED COURSES

View all Courses

Keep Exploring