RHEL/CentOS: Delete Files or Directories

This post will guide you how to delete or remove files on your rhel or redhat or CentOS Linux. How do I remove directories under your RHEL/CentOS Linux. How to quickly delete a file under a directory in RHEL/CentOS Linux.

Delete a Files On RHEL/CentOS


You need to user the rm command to remove files on rhel linux. Assuming that you want to delete a file called fio.txt under /root directory, you just need to execute the rm command with the file name that you want to remove in command line interface of your RHEL system.

Just do the following steps:
#1 open a termial or command line interface on your RHEL Linux
#2 type the following command to remove fio.txt file

# rm fio.txt

Delete Multiple Files on RHEL/CentOS


If you want to remove multiple files on a directory, you can can use the rm command to achive it. you just need to pass the file names to rm command and separated by space. For example, you want to delete two files under the /root directory, type the following command:

#rm fio1.txt fio2.txt

Note: before removing the files, you need to change the current directory to /root directory. type:

# cd /root

or you can also use the absolute path while you delete file, type:

#rm   /root/fio1.txt     /root/fio2.txt

Confirm each file before removal


if you want to get the confirmation for each file while removing, you can use the rm command with -i option, type:

#rm -i fio.txt

Outputs:

[root@osetc_x8664 ~]# rm -i fio.txt
rm: remove regular empty file ‘fio.txt’? y
[root@osetc_x8664 ~]#

Show report of each file removed


if you want to get the report of each file removed, you need to use the rm command with -v option, type:

#rm -v fio.txt

outputs:

[root@osetc_x8664 ~]# rm -v fio.txt
rm: remove regular empty file ‘fio.txt’? y
removed ‘fio.txt’

Remove Directory on RHEL/CentOS


if you want to use the rm command to remove a directory , you need to use the -d option, type:

# rm -d fio

Outputs:

[root@osetc_x8664 ~]# rm fio
rm: cannot remove ‘fio’: Is a directory
[root@osetc_x8664 ~]# rm -d fio
rm: remove directory ‘fio’? y

Revmoe all files under a directory(Recursive) on RHEL/CentOS


if you want to remove a directory and its files or its subdirectory, and you need to use the rm command with -rf option, type:

#rm -rf fio

if you want to get more information about rm command, you can directory run “rm –help” or “man rm” command.

type:

# rm --help

outputs:

[root@osetc_x8664 ~]# rm --help
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

Or

# man rm

 

You might also like:

Sidebar



back to top