In Linux, a folder is also known as directory and directory is nothing but simply a group of files.

If the directory or folder is empty then you can use rmdir command to delete the folder or directory in LinuxOn the other hand if the folder or directory is not empty then you have to use rm to remove it.

Syntax to delete folder or directory using rmdir command in Linux

► Play

rmdir command can be used to delete a folder or directories in Linux.

The syntax is;

$ rmdir directory-name

$ rmdir rmdir

For example, if you have a folder named myfolder and it is empty and you want to delete it, then do the following steps;

  • Open the terminal application in your Linux Operating system
  • Then type the command:

$ rmdir myfolder

Example to delete a folder in Linux

► Play

In this example delete the folder named /temp/alpha/.

$ rmdir /temp/alpha/

We can use rmdir command to delete two folders at the same time by using the following syntax.

$ rmdir dir1 dir2

This command will delete both dir1 and dir2 directories. We can use options to get additional information about what is happening when running rmdir command. Pass the –v (verbose) option as follows:

$ rmdir –v dir1

How to delete directory and subdirectories in Linux

► Play

The -p option can delete a directory and its subdirectories/sub-folders:

$ rmdir -p dir1/dir2/dir3

  • -p option will remove the parent folder and specified subfolder.
  • -v diagnose every directory that we have processed
  • –ignore-fail-on-non-empty this option will ignore each failure if a folder is non-empty.

How to delete directory/folder recursively in Linux

► Play

The rm command is used to delete all directories and subdirectories recursively. For example, remove *.doc files and all sub-directories and files inside letters directory, type the following command.

$ rm -rf  letters/

Where,

  • -r:  Attempt to remove the document hierarchy rooted in every record argument i.E. Recursively take away subdirectories and documents from the required listing.
  • -f: Attempt to remove the files without prompting for confirmation, no matter the document’s permissions.

The only root user can delete the directory or subdirectory using above commands. System admin also deletes any directory or folder created by anyone.

Conclusion

In this tutorial, we learned how to delete folder/directories in Linux using rm and rmdir command. rmdir command is used to delete empty directory/folder in Linux operating system and rm command is used to delete directory and subdirectories recursively.

Send us a feedback0/500

Do you like this article?
Yes
No