11

What command do I have to use to delete all directories that begin with graphene-80 under the directory /tmp?

What can I add to the rm command as option?

1
  • Just empty directories, or the contents too?
    – ilkkachu
    Feb 7, 2018 at 15:50

4 Answers 4

9

To delete the directories matching the pattern graphene-80* directly under /tmp, use

rm -rf /tmp/graphene-80*/

Here, the trailing / ensures that only directories whose names match the graphene-80* pattern are deleted (or symbolic links to directories), and not files etc.

To find the matching directories elsewhere under /tmp and delete them wherever they may be, use

find /tmp -type d -name 'graphene-80*' -prune -exec rm -rf {} +

To additionally see the names of the directories as they are deleted, insert -print before -exec.

The two tests -type d and -name 'graphene-80*' tests for directories with the names that we're looking for. The -prune removes the found directory from the search path (we don't want to look inside these directories as they are being deleted), and the -exec, finally, does the actual removal by means of calling rm.

0
7

Using find command:

find /tmp -type d -name 'graphene-80*' -delete

Arguments used:

  • -type to filter directory only and avoid finding files
  • -name to find file that match the pattern define between quotes
  • -delete to delete the result of the find command

EDIT: cleaner with -delete like shown in this post: Find files matching template and remove

1
  • 2
    cannot delete. directory not empty
    – gebbissimo
    Jul 13, 2021 at 8:59
0

To remove just empty directories with names matching graphene-80*, either

find /tmp -type d -name "graphene-80*" -exec rmdir {} +

or

find /tmp -type d -name "graphene-80*" -delete

(rmdir and GNU find should give you errors for the nonempty ones.)

To remove the directories with their contents:

find /tmp -type d -name "graphene-80*" -exec rm -r {} \; -prune

(With -exec rm + you may get errors from rm if there are nested matching directories; and without -prune, from find since it tries to descend to the just-removed directories.)

1
  • Why not just find /tmp -name 'graphene-80*' -type d -prune -exec rm -rf {} +? May 15, 2021 at 18:18
-3

You can use the following command:

rm -rf `find -type d -name folder_name`

Just replace the folder_name and you'll be good to go.

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.