Which of the following will delete a directory and all of the files contained within it?


You can delete a directory in Linux using the rm command. The rm command can delete a directory if it contains files as long as you use the -r flag. If a directory is empty, you can delete it using the rm or rmdir commands.

How do I delete a directory in Linux? This is a common question asked by Linux newbies. As you start working with the Linux operating system, it’s inevitable that you’ll want to delete a directory at some point.

Working with Linux requires learning much more than the basics of the C programming language and other coding languages . In this article, we’re going to discuss two methods you can use to delete a directory in Linux. We’ll also provide an example of each of these methods so you can get to work deleting directories as soon as possible.

Which of the following will delete a directory and all of the files contained within it?

Find Your Bootcamp Match

  • Career Karma matches you with top tech bootcamps
  • Access exclusive scholarships and prep courses

Select your interest
First name

Last name

Email

Phone number

By continuing you agree to our Terms of Service and Privacy Policy , and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

How to Delete a Directory in Linux

In Linux, there are two ways to delete a directory:

  • rmdir: This command deletes an empty directory.
  • rm command: This command deletes a directory including its subdirectories. You must use the -r flag with this command if the folder contains any files.

You may be accustomed to dragging folders into your computer’s trash can when you’re ready to delete them. The trash acts as a storage facility for documents you are going to delete. Before the file is deleted, you need to empty out your trash can.

When you’re using the Linux command line, you should know there is no trash can. Once you’ve deleted a file or a directory, it’s gone.

To learn more about the command line, read our guide to learn the Linux command line .

Linux Delete Directory: rm Command

The rm command allows you to delete files and directories. This command can be used to delete both empty and non-empty directories, unlike rmdir as we’ll discuss in a minute.

The syntax for the rm command is as follows:

rm [flags] [file/folder name]

There are two flags which allow you to delete a directory using rm . These are:

  • -d: Delete an empty directory
  • -r: Delete a non-empty directory and all of its contents

Suppose we want to delete a folder called “lib” in our current working directory. If we run the following command we can see what contents our folder contains:

Our command returns:

Because this folder contains files, we need to use the -r flag to delete it. We can do so using the following command:

Our command deleted the “lib” folder and all of its directories.

Which of the following will delete a directory and all of the files contained within it?

"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"

Venus, Software Engineer at Rockbot

You can also use rm to delete multiple directories. To do so, you can specify multiple folder names after rm . Suppose you want to delete the folders “lib” and “templates” in your current working directory. You could do so using this command:

Force Delete a Directory and Its Contents

By default, the rm -r command will prompt you to confirm the deletion of a file or folder. This happens if a file you are trying to delete has been protected. If you want to override this, you can specify the -f flag, like so:

This command will delete all directories and subdirectories in the “lib” directory permanently. You will not be prompted to confirm whether you want to delete any files when you run this command. So, you should use this command sparingly and only when you’re absolutely sure you want to delete the contents of a folder.

Delete Directory Linux: rmdir Command

The rmdir command allows you to delete empty directories. This command is useful if you know a folder is empty and want to remove it from your system.

Suppose our current working directory contains the following files and folders (which we can see by using the Linux “ls” command ):

The “config” folder is empty. Suppose you want to delete the folder “config” in your current working directory. You could do so using this command:

If we run ls to see the contents of our current folder, our command returns:

You can see that the “config” directory no longer exists. It’s important to note that you cannot use rmdir to delete a directory that contains a file. If our “config” directory contained a file, the following error would have been returned:

rmdir: config/: Directory not empty

Conclusion

Using rm and rmdir , you can delete directories in Linux. rm deletes non-empty directories. The rmdir command delete empty directories. It cannot be used to delete a directory that contains any files.

If you want to learn more about these commands, type in man , followed by the name of the command, in your terminal. This will show you the Linux manual page for the command.

Before you delete a file, make sure you have chosen the right one to delete. There is no turning back when you use a command like rm or rmdir .

To learn more about Linux, read our How to Learn Linux guide . This guide contains courses and resources to help you along your journey toward learning Linux.

About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.

What's Next?

  • Which of the following will delete a directory and all of the files contained within it?

    Want to take action?

    Get matched with top bootcamps

James Gallagher

About the author: James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.

Which of the following commands will delete a directory and all the files contained within it?

Introduction: You can remove all files in a directory using unlink command. Another option is to use the rm command to delete all files in a directory. This page explains how to delete all files in a directory using the command line options.

Which is used to delete a directory?

Use the rmdir command to remove the directory, specified by the Directory parameter, from the system. The directory must be empty (it can contain only .

How do I delete files from a directory?

Open My Computer or Windows Explorer. Locate and select the file or folder you want to delete, click File in the top menu bar, and select Delete.