3

What command do I have to use to delete all directories that begin with "graphene-80"

What can I add to the "rm" command as option?

  • On the whole system or in a particular directory, or anywhere beneath a particular directory? – Kusalananda Feb 7 '18 at 14:18
  • in the directory /tmp – user1543915 Feb 7 '18 at 14:22
  • Just empty directories, or the contents too? – ilkkachu Feb 7 '18 at 15:50
3

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

  • 2
    Use -depth when deleting directories, or find may complain about "no such directory" when trying to process the directories it has just deleted. You should probably also use -type d to be sure to not delete files. – Kusalananda Feb 7 '18 at 14:19
  • 3
    On Linux (as the Q is tagged), -delete will add in -depth for you. – Jeff Schaller Feb 7 '18 at 14:22
  • find: warning: you have specified the -depth option after a non-option argument -type, but options are not positional (-depth affects tests specified before it as well as those specified after it). Please specify options before other arguments. – user1543915 Feb 7 '18 at 14:37
  • It's corrected. – Kevin Lemaire Feb 7 '18 at 14:42
1

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, and not files etc.

To find the matching directories elsewhere under /tmp and delete them wherever they may be (or to handle the case where there are too many matching names resulting in a too long command), use

find /tmp -depth -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. Inserting -print after -exec will print the names of directories succesfully deleted.

  • find -delete will only delete directories if they are empty. You'd probably want -name 'graphene-80*' -prune -exec rm -rf {} +, or with -delete, use some -path approach to also delete the files inside those directories (note that -delete implies -depth) – Stéphane Chazelas Feb 8 '18 at 11:03
  • You'd want to use -prune as well to not descend into the directories you're going to delete anyway. – Stéphane Chazelas Feb 8 '18 at 11:12
  • @StéphaneChazelas Thanks, I was mislead by Jeff's comment to another answer and assumed it worked the way I thought it would without testing. – Kusalananda Feb 8 '18 at 11:12
  • You should choose between + and ;. Note that with +, -exec always returns true since the execution of the command is postponed (see also rm -v with some implementations to print deleted files) – Stéphane Chazelas Feb 8 '18 at 11:14
  • @StéphaneChazelas Sorted. I have too much in my head today. I appreciate your input as always. – Kusalananda Feb 8 '18 at 11:16
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.)

-2

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.

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.