Home ยป How to Delete a Directory in Linux with Commands
Coding Technology

How to Delete a Directory in Linux with Commands

How to Delete a Directory in Linux
How to Delete a Directory in Linux

How to delete a Directory in Linux: There is a way to delete a single directory while using the rm command to remove files you no longer need. The rm command removes the entries for a specified file, group of files, or certain select files from a list within a directory. User confirmation, read permission, and write permission are not required before a file is removed when you use the rm command.

So now it’s a common way to delete a directory in Linux There are two Linux commands you can use to remove a directory from the terminal window or command line: The rm command removes complete directories, including subdirectories and files.

The rmdir command removes empty directories. Use the rmdir command to remove the directory, specified by the Directory parameter, from the system. The directory must be empty. To permanently remove a directory in Linux, use either the rmdir or rm command:

For empty directories, use rmdir [dirname] or rm -d [dirname]

For non-empty directories, use rm -r [dirname]

Table of Content

How to Delete All Files and Folders in Linux?

For empty folders, use either the rmdir or rm -d commands followed by the name of the folder:

rmdir: Command to delete an empty directory

Syntax: rmdir dirname

The rm command with -r dir can be used for folders that are not empty. This rm command will delete the directory with its subdirectories

Syntax: rm -r dirname

How to Delete a Directory in Linux

how to delete a directory in linux

There are two ways to remove directories in Linux: the rm and rmdir commands. The purpose of both commands is that rm deletes directories that may contain content such as files and subdirectories, while rmdir only deletes empty directories.

So to permanently remove a directory in Linux, use either the rmdir or rm command. For empty directories, use rmdir [dirname] or rm -d [dirname]; For non-empty directories, use rm -r [dirname]

Syntax

As mentioned above there are two syntaxes to delete directories using the rm command, they are

  • -d: To delete empty directories
  • -r: to delete directories with all of their content

Examples

Deleting files using (rm command)

To delete the file named myfile, type the following:

Syntax: rm myfile

To delete all the files in the mydir directory, type the following: rm -i mydir/* After each file name displays, type y and press Enter to delete the file. Or to keep the file, just press Enter.

Force Delete a Directory in Linux and Its Contents

To remove a directory and all its contents, including any subdirectories and files, use the rm command with the recursive option, -r. Directories that are removed with the rmdir command cannot be recovered, nor can directories and their contents be removed with the rm -r command.

On other hand, the rm command with -f, force option combined with -r as rm -rf option is used to force remove Linux directories. -r stands for recursive so that rm can remove all the sub-directories also. -r option is needed to remove a directory even if the directory is empty with no subdirectory or file in it.

rmdir command is used to remove empty directories from the filesystem in Linux. The rmdir command removes each and every directory specified in the command line only if these directories are empty.

Conclusion

We hope it’s too easy after you all visit our website. I hope you get to know the topic very well. So now it’s your turn to do your program in Linux and almost finish this thing another main thing is to learn more about Linux, read our up-to-up sites. Follow Publish Square blog for more content like this and share your thoughts in the comments section

About the author

Ashwini

A Content Writer at PublishSquare who is passionate about Seo, Creative Writing, Digital Marketing with 2+ years of experience in Content Creation for blogs and social media.