ThisCalculator

Everyday

CentOS Delete File: The rm Command Explained

Updated · Standard formula, shown below

CentOS Delete File: The rm Command Explained

To delete a file in CentOS, you use the rm command followed by the file name, for example: rm filename.txt. This permanently removes the file without moving it to a trash or recycle bin, so it cannot be easily recovered.

Understanding the rm Command

The rm command is a standard Unix/Linux utility for removing files and directories. It is available on CentOS and most other Linux distributions. The basic syntax is:

rm [options] file1 file2 ...

When you run rm, it unlinks the file from the filesystem, freeing the space it occupied. The file is not sent to any kind of trash folder, so once deleted, it is gone unless you have a backup or use specialized recovery tools (which may not work on solid-state drives).

Common Options for rm

Here are the most frequently used options with rm:

  • -i (interactive): Prompts you for confirmation before each deletion. This is a safety net.
  • -f (force): Deletes without prompting, even if the file is write-protected. Useful for scripting, but be careful.
  • -r (recursive): Deletes directories and their contents. Required to remove a non-empty directory.
  • -v (verbose): Shows what is being deleted.
  • -d (directory): Deletes an empty directory (like rmdir).

You can combine options, e.g., rm -rf directory to force delete a directory and everything inside it. This is powerful and dangerous—use with extreme caution.

How to Delete a File in CentOS

To delete a single file, use:

rm /path/to/file.txt

If you are in the same directory as the file, you can just use the file name:

rm file.txt

To delete multiple files at once, list them all:

rm file1.txt file2.log file3.tmp

You can also use wildcards to match patterns. For example, to delete all .log files in the current directory:

rm *.log

Be careful with wildcards—they can match more than you expect. Always double-check the pattern before pressing Enter.

Deleting Directories

To delete an empty directory, you can use rmdir or rm -d. For a non-empty directory, use rm -r (recursive). For example:

rm -r myfolder

This removes the folder and all its contents. If you want to be prompted before each deletion inside, add -i:

rm -ri myfolder

But note that -f overrides -i, so rm -rf will not ask for confirmation.

Safety Tips and Common Mistakes

The biggest risk with rm is accidental deletion of important files. Here are some tips:

  • Always double-check the file name and path before running rm.
  • Use ls to list files first to confirm what you are deleting.
  • Consider using rm -i when deleting multiple files or using wildcards.
  • Never run rm -rf / or rm -rf . unless you intend to wipe the system. A single typo can be catastrophic.
  • When using wildcards, test with ls *.log first to see which files match.

A common mistake is using rm on a file that is actually a symbolic link—it will delete the link, not the target. To delete the target, you need to remove the link and then the original file.

Alternatives to rm

If you want a safety net, you can use a trash-cli tool that moves files to a trash directory instead of permanently deleting them. For example, trash-put (from the trash-cli package) moves files to a trash folder that you can later restore. To install it on CentOS:

sudo yum install trash-cli

Then use trash-put file.txt instead of rm. However, this is not a standard CentOS tool, so you may need to enable EPEL repository.

Another alternative is to rename the file instead of deleting it, e.g., mv file.txt file.txt.bak. This keeps the file but hides it. You can later delete it when you are sure.

Recovering Deleted Files

If you accidentally delete a file with rm, there is no built-in undo. However, if the filesystem is not heavily used, you might recover it with tools like extundelete or photorec. These tools scan the disk for remnants of deleted files. They are more likely to work on traditional spinning hard drives than on SSDs, because SSDs use TRIM and can permanently erase data quickly.

To increase the chance of recovery, stop using the disk immediately and unmount it if possible. Then boot from a live CD or another system and run recovery tools. But recovery is not guaranteed, so always back up important data.

Frequently Asked Questions

Can I delete a file that is in use?

Yes, on Linux you can delete a file that is open by a process. The file is unlinked from the directory, but the process still has a handle to it and can continue reading/writing until it closes. The space is not freed until the process closes the file. This can be useful for log rotation, but it may cause confusion if you expect the file to be gone immediately.

What is the difference between rm and unlink?

The unlink command is a low-level utility that removes a single file. rm is a higher-level command that can handle multiple files, directories, and options. For a single file, unlink and rm do the same thing, but rm is more commonly used.

How do I delete a file with a name starting with a dash?

If a file is named -file.txt, rm -file.txt will be interpreted as an option. Use rm -- -file.txt to tell rm that the next argument is a file name, not an option.

Can I delete a file without using rm?

Yes, you can use the unlink command, or you can use a file manager like Nautilus or Thunar to delete files graphically. You can also use find with -delete action, e.g., find /path -name '*.tmp' -delete.

Is there a way to make rm move files to trash?

Not by default, but you can alias rm to a script that moves files to a trash directory. However, this can be risky because scripts may not handle all cases correctly. It is safer to use a dedicated tool like trash-cli.

What happens if I delete a file that is a hard link?

Deleting one hard link removes that link, but the file data remains as long as other links exist. The space is only freed when the last link is deleted. So deleting one hard link does not delete the file completely.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.