How to Delete (Remove) Files and Directories in Linux

Remove (Delete) Files and Directories in Linux

How to Delete (Remove) Files and Directories in Linux Cover

There are several ways to remove directories in Linux systems. If you are using a desktop file manager, such as Gnome files or KDE Dolphin, you can use the administrator user graphical interface to delete files and directories. However, if you are working on a headless server or if you want to remove multiple directories at once, it is best to remove the directory (folders) from the command line.

This article describes how to remove a directory in Linux using the rmdir, rm and find commands.

About Linux Directories

If you use desktop file manager to delete a directory, the directory is actually moved to the trash can and you can easily restore it.

Be very careful when deleting files or directories from the command line, because if you use the commands described in this article to delete a directory, you will not be able to recover it completely.

On most Linux filesystems, deleting a directory requires permission to log in to the directory and its contents. Otherwise, a “Operation not permitted” error will occur.

Names of directories that contain spaces must be deleted using a backslash (/).

Removing Linux Directories With rmdir

rmdir is an auxiliary command line program for removing empty directories. This is useful if you want to remove empty directories without having to check if the directories are empty.

To remove a directory using rmdir, enter the name of the directory you want to remove after the command. For example, to remove a directory named dir1, type:

rmdir dir1

If the directory is not empty, the following error will occur:

rmdir: Delete failure ‘dir1’ : There is no such file or directory

In this case, you must either use the rm command or manually delete the contents of the directory before removing it.

Removing Linux Directories With rm

rm is an auxiliary command line program for removing files and directories. Unlike rmdir, the rm command can remove empty and non-empty directories.

By default, rm does not remove catalogs when used without options. To remove a blank directory, use the -d (–dir) option and remove the non-empty directory. For all of its content, use the -r (–recursive or -R).

For example, to remove the directory named dir1 and all of its contents, type:

rm -r dir1

If the directory or files on it are protected by registration, you will be prompted to confirm the deletion. Use the -f option to remove the directory without a message appearing.

rm -rf dir1

To remove multiple directories at once, call the rm command and then enter the names of the directories, separated by spaces. The following command deletes all registered directory and its contents.

rm -r dir1 dir2 dir3

The -i option tells rm to ask for confirmation to delete each subdirectory and file. If your directory contains a large number of files, this can be a little too much, so consider using the -I option, which only asks you once before proceeding with the removal.

rm -rI dir1

To remove the directory, type y and press Enter.

rm: Remove 1 argument recursively? y

You can also use the normal extension to match and remove multiple directories. For example, to remove all first-order directories that end in _bak in the current directory, use the following command:

rm -r * _bak

Using a regular extension when deleting directories can be dangerous. We recommend that you first list the directories with the ls command to see which directories will be removed before executing the rm command.

Removing Linux Directories With find

find is an auxiliary command line program that can search for files and directories based on specific expressions and perform an action on each corresponding file or directory.

The most common scenario is to use the find command to remove catalogs based on a pattern. For example, to remove all _cache catalogs in the current worksheet, run the following command:

Find . -type d -name ‘* _cache’ -exec rm -r {} +

Let’s analyze the above command.

  1. / dir- retrospectively search the current work directory (.)
  2. – type d- Limit search in directories.
  3. -name ‘*_cache’- search only catalogs ending in _cache
  4. -exec- executes an external command with an optional argument. In this case, rm -r.
  5. {} + – Add the file found at the end of the rm command.

Also, know How to Recover Deleted WhatsApp Chat Messages.

Removing All Linux Empty Directories

To remove all empty directories in the directory tree, execute the following command:

find / dir -type d -empty -delete

The following is a description of the options used.

  1. / dir- retrospective search directories in / dir
  2. – type d- Limit search in directories.
  3. -empty- Limits search to empty directories only.
  4. -design- Delete all empty directories in the secondary tree. – Deletion can only delete empty directories.

Be careful when using the delete option. The find command line is rated as a representation. If you first add the -delete option, the command deletes everything below the specified starting point.

Always try the command first without the option-description and use the option-description as the last.

Delete (Remove) Files and Directories in Linux

You can use rm and find for quick and effective removal of directories based on various criteria.

Deleting a directory is a simple and easy process, but you need to be careful not to delete data that is important.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.