How to remove directory linux with files

How to Delete Directory in Linux

If a directory you’re trying to remove is empty (does not have any files or directories inside), then you can use the rmdir command to remove the directory from the command line.

For example, let’s say, you have an empty directory test/ which you want to remove.

To remove the empty directory test/, run the following command:

As you can see, the directory test/ is removed.

If the directory you’re trying to remove has contents, you can’t remove it with the rmdir command. You have to use the rm command instead.

Let’s say, you want to remove a directory configs/ which has files and other directories inside.

Now, to remove the directory configs/ run the rm command as follows:

The directory and all the contents (files and directory) of the directory should be removed as you can see in the screenshot below.

If you want, you can remove multiple directories (and its contents) with the rm command as follows:

NOTE: The -f option removes the directory and contents without any safety check. So, be sure that you don’t have anything important in the directory that you’re trying to remove. For more information on that, read the next section of this article.

Safety Check Before Removing Directories from the Command Line:

A directory contains a lot of files and other directories. You may not know whether you have any important files in there all the time before you actually removed it (oh no!). So, it’s always a good idea to use programs such as tree to verify that you don’t have anything important in the directory that you want to remove.

The tree program is very simple to use. The format of the tree command is:

The tree command is not available in most of the Linux distribution by default. But you can easily install it from the official package repository of your desired Linux distribution.

CentOS 7 and RHEL 7:

You can install tree package from the official package repository of CentOS 7 or RHEL 7 using the YUM package manager as follows:

Ubuntu/Debian:

On Ubuntu, Debian or any derivatives of Debian, you can install tree package from the official package repository using the APT package manager as follows:

The rm command also has a safety check option. You can use the -i option to tell rm to prompt for confirmation before it removes any file or directory.

To safely remove a directory images/ using rm, run the following command:

Before rm descend (go into) any directory, it will prompt for confirmation. To confirm, press y and then press .

Читайте также:  Как самому создать тему для windows

rm will also prompt for configuration before it removes any file. To confirm, press y and then press .

It will only remove the directory if you confirm removal of everything. Otherwise, it will leave all the files that you don’t want to remove along with the directories. Now, you have a way to move the important files and remove the directory afterward. It’s better than regretting later.

The -i option will be enabled by default in some Linux distribution. To override it and force rm to remove everything without prompting first, use the -f option.

Removing Directories using Graphical User Interface:

If you’re using graphical desktop environment, then you can use the File Manager (i.e Nautilus, Dolphin etc) included in the desktop environment to remove directories.

To remove a directory or directories, select the directory or directories that you want to remove and then press + . Your file manager should prompt you to confirm the delete operation. To confirm, click on Delete as marked in the screenshot bleow. The directory or directories that you’ve selected should be removed.

So, that’s how you delete a directory or directories in Linux. Thanks for reading this article.

Источник

How to Remove Files and Directories in Linux Command Line [Beginner’s Tutorial]

How to delete a file in Linux? How to delete a directory in Linux? Let’s see how to do both of these tasks with one magical command called rm.

How to delete files in Linux

Let me show you various cases of removing files.

1. Delete a single file

If you want to remove a single file, simply use the rm command with the file name. You may need to add the path if the file is not in your current directory.

If the file is write protected i.e. you don’t have write permission to the file, you’ll be asked to confirm the deletion of the write-protected file.

You can type yes or y and press enter key to confirm the deletion. Read this article to know more about Linux file permissions.

2. Force delete a file

If you want to remove files without any prompts (like the one you saw above), you can use the force removal option -f.

3. Remove multiple files

To remove multiple files at once, you can provide all the filenames.

You can also use wildcard (*) and regex instead of providing all the files individually to the rm command. For example, if you want to remove all the files ending in .hpp in the current directory, you can use rm command in the following way:

4. Remove files interactively

Of course, removing all the matching files at once could be a risky business. This is why rm command has the interactive mode. You can use the interactive mode with the option -i.

It will ask for confirmation for each of the file. You can enter y to delete the file and n for skipping the deletion.

You just learned to delete files. Let’s see how to remove directory in Linux.

How to remove directories in Linux

There is a command called rmdir which is short for remove directory. However, this rmdir command can only be used for deleting empty directories.

Читайте также:  Центр обновления windows ошибка 80244022

If you try to delete a non-empty directory with rmdir, you’ll see an error message:

There is no rmdir force. You cannot force rmdir to delete non-empty directory.

This is why I am going to use the same rm command for deleting folders as well. Remembering rm command is a lot more useful than rmdir which in my opinion is not worth the trouble.

1. Remove an empty directory

To remove an empty directory, you can use the -d option. This is equivalent to the rmdir command and helps you ensure that the directory is empty before deleting it.

2. Remove directory with content

To remove directory with contents, you can use the recursive option with rm command.

This will delete all the contents of the directory including its sub-directories. If there are write-protected files and directories, you’ll be asked to confirm the deletion.

3. Force remove a directory and its content

If you want to avoid the confirmation prompt, you can force delete.

4. Remove multiple directories

You can also delete multiple directories at once with rm command.

Summary

Here’s a summary of the rm command and its usage for a quick reference.

Purpose Command
Delete a single file rm filename
Delete multiple files rm file1 file2 file3
Force remove files rm -f file1 file2 file3
Remove files interactively rm -i *.txt
Remove an empty directory rm -d dir
Remove a directory with its contents rm -r dir
Remove multiple directories rm -r dir1 dir 2 dir3

I hope you like this tutorial and learned to delete files and remove directories in Linux command line. If you have any questions or suggestions, please leave a comment below.

Источник

How to Delete a Directory in Linux

Earlier in one of the tutorials, we have explained how to create directory in Linux. Now let’s check how to delete a directory in Linux which is either empty or having subdirectories with files. This is especially when you need to free up some space on your system in order to save more files or install additional packages.

There are many ways in which you can remove a directory in Linux. You can make use of the file manager if you are using a GUI system such as GNOME, MATE or KDE Plasma, or you can do it over the terminal.

When working with a GUI system deleting a directory takes it to the crash can, the equivalent of recycle bin in Windows from where it can be restored. However, the scenario is different when working on a command line on a minimal system because once a directory is deleted, it is permanently removed and cannot be recovered.

This tutorial will take you through various ways in which you can delete a directory in Linux.

Delete a directory using rmdir command

The rmdir command, short for’remove directory’, is a command-line tool that is used to delete empty directories. The operation will be successful if and only if the directory is empty. The syntax for deleting a directory is as follows:

For instance, to remove an empty directory called ‘mydirectory’, run the command:

If the directory is not empty, an error will be displayed on the screen as shown:

The error is a clear indication that the directory contains either files or folders or both.

Remove a directory using rm command

Short for remove, the rm command is used for deleting both empty and non-empty directories.

The rm command is usually used for removing files in Linux. However, you can pass some arguments that can help you delete directories. For example, to remove a directory recursively ( remove the directory alongside its contents), use the recursive option -r (-R or —recursive) as shown below.

If a directory is write-protected, you will be prompted whether to continue deleting the files inside the directory and the directory as a whole. To save you the annoyance and inconvenience of constantly bumping into such prompts, add the -f option to force the deletion without being prompted.

Additionally, you can delete multiple directories at a go in a single command as shown in the command below. The command deletes all the directories and their subdirectories without prompting for deletion.

To exercise more caution, you can use the -i option which prompts for the deletion of the directories and subdirectories. However, as we saw earlier, this can be quite annoying especially if you have several subfolders and files. To address this inconvenience, use the -I flag to prompt you only once.

When you hit y for ‘Yes’, the command will remove all the subfolders and files in the directory without prompting any further.

To remove an empty directory, pass the -d option as shown below.

Using find command

Find command is a command-line tool that helps users search for files as well as directories based on specific search criteria/pattern or expression. Additionally, the command can be used to search for directories and delete them based on the specified search criteria.

For example, to delete a directory called ‘mydirectory’ in the current directory, run the command below.

Let’s break down the parameters in the command

( . ) — This denotes the directory in which the search operation is being carried out. If you want to carry out the search in your current directory use the period sign (.)

-type d — This sets the search operation to search for directories only.

-name — This specifies the name of the directory.

-exec rm -rf — This deletes all directories and their contents.

<> +- — This appends all the files found at the end of the rm command.

Let’s take another example:

Remove an empty directory

If you wish to remove all empty directories use the following command:

Again, let’s break this down

. — This recursively searches in the current working directory

-type d — This keeps the search to directories only

-empty — This restricts the search pattern to empty directories only

-delete — This will delete all the empty directories found including subdirectories.

If you have lots of empty directories then use a shell script to delete empty directory.

Conclusion

In this tutorial, we looked at how to delete a directory in Linux using the rm, rmdir and find commands. We hope you can comfortably delete a directory in Linux whether it contains files and other subdirectories, or simply if it is empty. Give it a try and get back to us with your feedback.

Источник

Читайте также:  Windows клиент под linux
Оцените статью