How to remove a directory in Linux

A directory which is having some contents inside it cannot be removed by rmdir command. There are two ways to delete the directory with contents.

1. Remove with contents inside the directory and then run rmdir command.
2. Run rm -rf directory_name (Where r stands for recursive and f stands for forcibly).

Try to remove the which contains files or folder

[root@localhost administrator]# rmdir k2schools
rmdir: failed to remove `k2schools’: Directory not empty

Now remove the files recursively. But, it is always good practice to take backup of the files if they are important.

[root@localhost administrator]# rm -rf k2schools -i
rm: descend into directory `k2schools’? yes
rm: remove regular file `k2schools/linuxtutorials.txt’? yes
rm: remove regular file `k2schools/k2admin.txt’? yes
rm: remove directory `k2schools’? yes
[root@localhost administrator]# ls -ltrh
total 0

You can leave a response, or trackback from your own site.

Leave a Reply

Powered by k2schools
Skip to toolbar