Linux rm | Linux Delete File

The 'rm' means remove. This command is used to remove a file. The command line doesn't have a recycle bin or trash unlike other GUI's to recover the files. Hence, be very much careful while using this command. Once you have deleted a file, it is removed permanently.

Introduction to rm

rm is a general command in Unix and other Unix-like systems. It is used to delete objects like symbolic links, directories, and computer files from the file systems. Also, it removes some special files like sockets, pipes, and devices nodes, the same as the del command in Microsoft Windows, OS/2, and MS-DOS. The command is available inside the EFI shell as well.

Overview of rm

The rm command deletes object references from the file system with the help of the unlink system call, in which the objects might have had two or more references (e.g., a file using two names), and the objects are removed themselves only if every reference has been deleted and the programs still have not been open handles for the objects.

  • It permits scenarios in which a program can launch a file, remove it immediately from the file system, and use it for limited space, knowing the space of the file will be recovered after the program dismisses, even when it dismisses by crashing.
  • Generally, the command doesn't damage file data because its aim is really hard to unlink references. Besides, the freed file system space may still include leftover data using the deleted file. It can be a security consideration in a few cases, and hardened releases sometimes allow to wipe out of the data because the last link is cut, and programs like srm and shred are available specifically to give data wiping features.
  • The rm command is only found on Unix-derived OSes, which typically don't allow recovery of removed files using a method, such as the recycle bin, so the habit of the users to enclose the rm command is a type of wrapper to restrict unintentional file deletion.
  • Also, there are some undelete utilities that will try to recreate the index and if the parts are not reused, it can get the file back.

Brief History of rm

The rm command will remove directories if they're empty on a few old releases of Unix. Still, the behavior can be brought in a few versions of the rm command using the -d option, e.g., the BSDs (like macOS, OpenBSD, NetBSD, and FreeBSD) derived from 4.4BSD-Lite2.

Also, the rm version, grouped in GNU coreutils, offers the -d flag to aid with compatibility. Similar functionality is offered by standard rmdir. In Version 7, the -i flag is substituted by "delete from switches" or dsw, which appeared in Version 1.

Syntax:

The rm command removes the file named after options are included. Users can utilize a relative path or a complete path of the file to name the files to remove. The rm command does not, by default, remove a directory. The rm foo command removes the "foo" file in the current directory of the user.

The rm command can be covered by a shell alias function of the rm -i command so as to ignore accidental file deletion. Still, if a user wants to remove a lot of files without approval, they can set the -i flag by including the -f flag manually. This approach, unfortunately, produces dangerous habits for wildcard usage, causing its accidental removal version.

The rm -rf command is often used in anecdotes and jokes about Unix disasters, including the loss of several files during the creation of the film Pixar and Toy Story 2. The rm -rf / version of the command would lead all files accessible using the current file system to be removed through the machine if executed by a superuser.

On the parent directory, removing a file needs the write permission on most file systems. To remove a directory, one must recursively remove each of its content. It needs that one must recursively contain the execute and write permissions for that directory and the non-empty directory. The read permissions are required to specify the directory contents to remove them. Sometimes, it causes a weird situation in which a non-empty directory can't be removed as one does not have the write permission, so one can't remove its contents; although if a similar directory was empty, one could remove it.

Example:


rm file

In above snapshot, we have removed file myfile1 permanently with the help of 'rm' command.

rm Options

OptionDescription
rm *extensionUsed to delete files having same extension.
rm -r or RTo delete a directory recursively.
rm -iRemove a file interactively.
rm -rfRemove a directory forcefully.
-f-f stands for force. It ignores files that do not exist and overrides the confirmation prompts. However, it will not delete files through a directory when it is write-protected.
-v-v stands for verbose. It prints what the rm command is doing in the terminal.
-d-d stands for directory. It removes an empty directory and works only if the named directory is empty.
--one-file-systemIt only deletes files on a similar file system like the argument and will avoid the mounted file system.





Latest Courses