How To Delete or Remove a Directory forcelly Using Linux Command
This post will guide you how to remove or delete a directory using linux command under your CentOS or RHEL Linux system. How do I force delete a directory in Linux. when you try to delete a directory under command line interface in Linux using rmdir command, and you would get an error message as “rmdir: failed to remove ‘mydir’: Directory not empty”? so how do I delete a directory that is not empty in your Linux operating system.
rm Command
rm command can be used to remove files or directories in Linux. It removes each specified file. By default, it does not remove directories.
The syntax of the rm command are as follows:
-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
-d, --dir
remove empty directories
-r, -R, --recursive
remove directories and their contents recursively
rmdir Command
rmdir command can be used to remove empty directories in Linux.
The syntax of the rmdir command are as follows:
--ignore-fail-on-non-empty
ignore each failure that is solely because a directory is non-empty
-p, --parents
remove DIRECTORY and its ancestors; e.g., ‘rmdir -p a/b/c’ is similar to ‘rmdir a/b/c a/b a’
unlink Command
unlink command can be used to call the unlink function to remove the specified file.
Remove Files
If you want to remove or delete a file or directory from the command line interface in your Linux system, and you can use rm
command or unlink
command. and you can use unlink
command to remove only a single file and you can remove multiple files at once using rm
command.
For example, you want to remove a single file named myfile1 under mydir directory, just type one of the following command:
$ rm myfile1
or
$ unlink myfile1
If you want to remove multiple files at once, and you can use the rm
command followed by the file names sperated by space.
For example, you wish to remove multiple files (myfile1, myfile2, myfile3), just type the following command:
$ rm myfile1 myfile2 myfile3
You can also use a wildcard or regular expansions to match multiple files for removing multiple files. For example, you wish to remove all .txt
files in your current directory, and you can use the following command:
$ rm *.txt
Remove Directoris
If you want to remove a directory that contain other files or directories in your Linux system, and you can use the rm
command with -rf
options, and the syntax is as follows:
$ rm -rf /root/mydir
After delete that specified directory, and you can use the following command to verify it:
$ ls -l /root/mydir
If you want to learn more about rm or rmdir or unlink command, and you can run the following commands:
$ rm --help $ rmdir --help $ unlink --help $ man rm $ man rmdir $ man unlink
Outputs:
[root@mydevops mydir]# 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[=all] do not remove '/' (default); with 'all', reject any command line argument on a separate device from its parent -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 [root@mydevops mydir]# rmdir --help Usage: rmdir [OPTION]... DIRECTORY... Remove the DIRECTORY(ies), if they are empty. --ignore-fail-on-non-empty ignore each failure that is solely because a directory is non-empty -p, --parents remove DIRECTORY and its ancestors; e.g., 'rmdir -p a/b/c' is similar to 'rmdir a/b/c a/b a' -v, --verbose output a diagnostic for every directory processed --help display this help and exit --version output version information and exit
Conclusion
You should know that how to use rm command to delete or remove a directory that is not empty in your CentOS or RHEL Linux system.