Linux remove all folders with name

How to Remove (Delete) Directory in Linux

There are several different ways to remove directories in Linux systems. If you use a Desktop file manager such as Gnome’s Files or KDE’s Dolphin, then you can delete files and directories using the manager’s graphical user interface. But, if you are working on a headless server or want to remove multiple directories at once, your best option is to delete the directories (folders) from the command line.

In this article, we will explain how to delete directories in Linux using the rmdir , rm , and find commands.

Before You Begin #

When removing a directory using a desktop file manager, the directory is actually moved to the Trash and can be easily recovered.

Be extra careful when removing files or directories from the command line because once the directory is deleted using the commands explained in this article, it cannot be fully recovered.

On most Linux filesystems, deleting a directory requires write permission on the directory and its content. Otherwise, you will get “Operation not permitted” error.

Directory names with a space in them must be escaped with a backslash ( / ).

Removing Directories with rmdir #

rmdir is a command-line utility for deleting empty directories. It is useful when you want to delete a directory only if it is empty, without needing to check whether the directory is empty or not.

To delete a directory with rmdir , type the command followed by the name of the directory you want to remove. For example, to delete a directory named dir1 you would type:

If the directory is not empty, you will get the following error:

rmdir: failed to remove 'dir1': No such file or directory 

In this case, you will need to use the rm command or manually remove the directory contents before you can delete it.

Removing Directories with rm #

rm is a command-line utility for deleting files and directories. Unlike rmdir the rm command can delete both empty and non-empty directories.

By default, when used without any option rm does not remove directories. To delete an empty directory, use the -d ( —dir ) option and to delete a non-empty directory, and all of its contents use the -r ( —recursive or -R ) option.

For example to delete a directory named dir1 along with all of its contents you would type:

Читайте также:  Best email clients linux

If a directory or a file within the directory is write-protected, you will be prompted to confirm the deletion. To remove a directory without being prompted, use the -f option:

To remove multiple directories at once, invoke the rm command, followed by the names of the directories separated by space. The command below will remove each listed directory and their contents:

The -i option tells rm to prompt you to confirm the deletion of each subdirectory and file. If the directory contains a lot of files, this can be a little annoying, so you may consider using the -I option what will prompt you only once before proceeding with the deletion.

To remove the directory type y and hit Enter .

rm: remove 1 argument recursively? y 

You can also use regular expansions to match and delete multiple directories. For example, to remove all first-level directories in the current directory that ends with _bak , you would use the following command:

Using regular expansions when removing directories may be risky. It is recommended first to list the directories with the ls command so that you can see what directories will be deleted before running the rm command.

Removing Directories with find #

find is a command-line utility that allows you to search for files and directories based on a given expression and perform an action on each matched file or directory.

The most common scenario is to use the find command to delete directories based on a pattern. For example, to delete all directories that end with _cache in the current working directory, you would run:

find . -type d -name '*_cache' -exec rm -r <> +

Let’s analyze the command above:

  • /dir — recursively search in the current working directory ( . ).
  • -type d — restricts the search to directories.
  • -name ‘*_cache’ — search only directories that end with _cache
  • -exec — executes an external command with optional arguments, in this case, that is rm -r .
  • <> + — appends the found files to the end of the rm command.

Removing all empty directories #

To remove all empty directories in a directory tree you would run:

find /dir -type d -empty -delete

Here is an explanation for the options used:

  • /dir — recursively search in the /dir directory.
  • -type d — restricts the search to directories.
  • -empty — restricts the search only to empty directories.
  • -delete — deletes all found empty directories in the subtree. -delete can delete only empty directories.

Use the -delete option with extreme caution. The find command line is evaluated as an expression, and if you add the -delete option first, the command will delete everything below the starting points you specified.

Always test the command first without the -delete option and use -delete as the last option.

/bin/rm: Argument list too long #

This error message appears when you use the rm command to remove a directory that contains a huge number of files. This happens because the number of files is larger than the system limit on the size of the command line argument.

Читайте также:  Linux kernel boot message

There are several different solutions to this problem. For example, you can cd to the directory and manually or using a loop to remove sub-directories one by one.

The easiest solution is first to delete all files within the directory with the find command and then delete the directory:

find /dir -type f -delete && rm -r /dir

Conclusion #

With rm and find you can delete directories based on different criteria fast and efficient.

Deleting directories is a simple and easy process, but you must be cautious not to delete important data.

If you have any questions or feedback, feel free to leave a comment.

Источник

How to Remove Files and Directories Using Linux Command Line

This tutorial, will show you how to use the rm , unlink , and rmdir commands to remove files and directories in Linux.

How to Remove Files #

To remove (or delete) a file in Linux from the command line, use either the rm (remove) or unlink command.

The unlink command allows you to remove only a single file, while with rm , you can remove multiple files at once.

Be extra careful when removing files or directories, because once the file is deleted it cannot be easily recovered.

    To delete a single file, use the rm or unlink command followed by the file name:

If the file is write-protected, you will be prompted for confirmation, as shown below. To remove the file type y , and hit Enter . Otherwise, if the file is not write-protected, it will be deleted without prompting.

 rm: remove write-protected regular empty file 'filename'?
rm filename1 filename2 filename3

You can also use a wildcard ( * ) and regular expansions to match multiple files. For example, to remove all .pdf files in the current directory, use the following command:

How to Remove Directories (Folders) #

In Linux, you can remove/delete directories with the rmdir and rm .

rmdir is a command-line utility for deleting empty directories, while with rm you can remove directories and their contents recursively.

    To remove an empty directory, use either rmdir or rm -d followed by the directory name:

rm -r dirname1 dirname2 dirname3

Conclusion #

By now you should have a good understanding of how to use the Linux rm , rmdir and unlink commands and you should be able to safely remove files and directories from the command line.

Feel free to leave a comment if you have any questions.

Источник

Delete files and folders with specific name from a certain directory

I have a folder /home/userA/folderA this folder contains many files and folders and subfolders. What I want to do is to delete all files that have certain names data.txt and glass.txt . I also want to delete any folder named match with all its contents. I’d be thankful for any advice in how to do this.

Читайте также:  Kali linux openvpn настройка

3 Answers 3

You can delete the files and folders in the subdirectories of folderA .

find /home/userA/folderA/* -type f \( -name "data.txt" -or -name "glass.txt" \) -delete 

and to remove the folders match :

find /home/userA/folderA/* -depth -name "match" -type d -exec rm -rf "<>" \; 

its giving this error find: invalid expression; I was expecting to find a ‘)’ somewhere but did not see one.

Try: find /home/userA/folderA/* -depth -name «data.txt» -type f -exec rm <> \; and find /home/userA/folderA/* -depth -name «glass.txt» -type f -exec rm <> \;

And the verbose (python) option:

#!/usr/bin/env python3 import os import shutil # -------------------------------------------------------- reorg_dir = "/path/to/your/folder" remove_files = ("data.txt", "glass.txt") remove_dirs = ("match") # --------------------------------------------------------- for root, dirs, files in os.walk(reorg_dir): for name in files: if name in remove_files: os.remove(root+"/"+name) for dr in dirs: if dr in remove_dirs: shutil.rmtree(root+"/"+dr) 

Copy the script into an empty file, set the directory and if you want/need: edit the list of files and folders to remove, save it as reorg.py and run it by the command:

Источник

How to delete all directories with a particular name?

Under a directory, I have a bunch of directories named Debug . How do I delete all the Debug directories?

1 Answer 1

It’s not clear — under the directory dir_a you can have only one directory named Debug . Or are you saying that you have dir_a/Debug , dir_a/dir_b/Debug , dir_a/dir_c/Debug and so on?

You can list all directory named exactly Debug under the directory dir_a with:

cd dir_a # or whatever you need to go there find . -type d -name Debug 

this will list all the directories named Debug under the current directory. To delete them (double check, this IS NOT UNDOABLE):

find . -depth -type d -name Debug -exec rm -r <> \; 

-depth will list Debug/Debug/ before Debug/ to avoid errors.

  1. find . -type d -name Debug will search all directory under the current one ( . ) for entries which are both directory ( -type d ) and have the name «Debug» ( -name Debug ).
  2. The added -depth flag make the search depth-first (it means that «deeper» matches are found before «shallow» ones).
  3. -exec . \; tells find to execute the command in . for each match. (The semicolon is quoted because otherwise the shell will interpret it).
  4. In the command . above, the special symbol <> is substituted by the current match.

So in plain English it is: find every directory under this one which name is «Debug», depth first, and for each one execute the command rm -r followed by the full name of the directory.

Источник

Оцените статью
Adblock
detector