How to delete and remove files on RHEL (Red Hat) Linux

last updated in Categories , ,

I am a new RHEL (Red Hat) Linux user. How do I delete and remove a file on RHEL (Red Hat) Linux based system?

You need to use the rm command. It tries to remove the files specified on the command line. Use the rm command to delete files and directories on RHEL (Red Hat) Linux. This page shows how to delete and remove files on RHEL (Red Hat) Linux.

Command to delete and remove files on RHEL (Red Hat) Linux

The syntax is as follows for the rm and unlink command to remove files on RHEL (Red Hat) Linux:

  1. Open the RHEL (Red Hat) terminal application (bash shell)
  2. Type any one of the following command to delete a file named rhel7.demo.txt in the current directory
  3. rm rhel7.demo.txt
    OR
    unlink rhel7.demo.txt

Let use see all the rm command options to delete and remove files on RHEL (Red Hat) Linux.

Delete multiple files

Type the following command to delete the file named tom.txt, tom.txt, and dance.jpg located in the current directory:
[vivek@nixcraft-rhel:~]$ rm tom.txt tom.txt dance.jpg
You can specify path too. If a file named tom.txt located in /tmp/ directory, you can run:
[vivek@nixcraft-rhel:~]$ rm /tmp/tom.txt
[vivek@nixcraft-rhel:~]$ rm /tmp/tom.txt /home/vivek/dance.jpg /home/vivek/data/tom.txt

RHEL (Red Hat) Linux delete a file and prompt before every removal

To get confirmation before attempting to remove each file pass the -i option to the rm command on RHEL (Red Hat) Linux:
[vivek@nixcraft-rhel:~]$ rm -i my-file-name-here
[vivek@nixcraft-rhel:~]$ rm -i tom.txt

Force rm command on RHEL (Red Hat) Linux to explain what is being done with file

Pass the -v option as follows to get verbose output on RHEL (Red Hat) Linux box:
[vivek@nixcraft-rhel:~]$ rm -v my-file-name-here
[vivek@nixcraft-rhel:~]$ rm -v cake-day.jpg

Red Hat Enterprise Linux delete all files in folder or directory

You need to pass the following options:
[vivek@nixcraft-rhel:~]$ rm -rf dir1
[vivek@nixcraft-rhel:~]$ rm -rf /path/to/dir/
[vivek@nixcraft-rhel:~]$ rm -rf /home/vivek/oldschoolpics/

It will remove all files and subdirectories from a directory. So be careful. Always keep backups of all important data on RHEL (Red Hat) Linux.
Delete and remove files on RHEL (Red Hat) Linux

RHEL (Red Hat) Linux delete file begins with a dash or hyphen

If the name of a file or directory or folder starts with a dash (- or hyphen --), use the following syntax:
[vivek@nixcraft-rhel:~]$ rm -- -my-file-name-here
[vivek@nixcraft-rhel:~]$ rm -- --my-file-name-here
[vivek@nixcraft-rhel:~]$ rm -rf --my-directory-name-here
[vivek@nixcraft-rhel:~]$ rm ./-file
[vivek@nixcraft-rhel:~]$ rm -rf ./--my-directory-name-here

Do not run ‘rm -rf /‘ command as an administrator/root or normal RHEL (Red Hat) Linux user

rm -rf (variously, rm -rf /, rm -rf *, and others) is frequently used in jokes and anecdotes about RHEL (Red Hat) Linux disasters. The rm -rf / variant of the command, if run by an administrator, would cause the contents of every writable mounted filesystem on the computer to be deleted. Do not try these commands on RHEL (Red Hat) Linux:
[vivek@nixcraft-rhel:~]$ rm -rf /
[vivek@nixcraft-rhel:~]$ rm -rf *

Conclusion

And there you have it, the rm command which is used to delete files and directories on RHEL (Red Hat) Linux operating systems. For more info see the rm command man page on RHEL (Red Hat) by typing the following man command:
$ man rm
OR
rm --help
Sample outputs:

Usage: rm [OPTION]... FILE...
Remove (unlink) the FILE(s).
 
  -f, --force           ignore nonexistent files and arguments, never prompt
  -i                    prompt before every removal
  -I                    prompt once before removing more than three files, or
                          when removing recursively; less intrusive than -i,
                          while still giving protection against most mistakes
      --interactive[=WHEN]  prompt according to WHEN: never, once (-I), or
                          always (-i); without WHEN, prompt always
      --one-file-system  when removing a hierarchy recursively, skip any
                          directory that is on a file system different from
                          that of the corresponding command line argument
      --no-preserve-root  do not treat '/' specially
      --preserve-root   do not remove '/' (default)
  -r, -R, --recursive   remove directories and their contents recursively
  -d, --dir             remove empty directories
  -v, --verbose         explain what is being done
      --help     display this help and exit
      --version  output version information and exit
 
By default, rm does not remove directories.  Use the --recursive (-r or -R)
option to remove each listed directory, too, along with all of its contents.
 
To remove a file whose name starts with a '-', for example '-foo',
use one of these commands:
  rm -- -foo
 
  rm ./-foo
 
Note that if you use rm to remove a file, it might be possible to recover
some of its contents, given sufficient expertise and/or time.  For greater
assurance that the contents are truly unrecoverable, consider using shred.
 
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'rm invocation'

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.