0

I have two folders, one that I know the name of, and one that I don't:

~/foobar-as9df7 # I know the name of this one
~/foobar-7fq92h # I don't know the name of that one

How can I delete the folder that starts with foobar- and is NOT ~/foobar-as9df7, and that I don't know the name of?

Said differently, I want to rm -rf ~/foobar-as9df7 except that I don't know it's called foobar-as9df7, I only know its name starts with foobar- and it's not foobar-as9df7.

I've tried doing:

find foobar-* ! -name "foobar-as9df7"  -exec rm -rf {} \;

which works but strangely this triggers a message saying:

find: ‘foobar-7fq92h’: No such file or directory

-- EDIT

the output of this command:

find ~/ -type d -name 'foobar-*' ! -name 'foobar-as9df7' -exec echo .rm -r {} +

is as follows:

.rm -r /home/ubuntu/foobar-7fq92h
Jivan
  • 829
  • 1
  • 7
  • 9
  • What shell are you using? – terdon Mar 23 '21 at 12:21
  • using bash shell – Jivan Mar 23 '21 at 12:22
  • 1
    Perfect, the answers in the dupe should be exactly what you need then. Something like `shopt -s extglob; rm -r ~/foobar-!(as9df7)`. – terdon Mar 23 '21 at 12:24
  • is there an equivalent to `shopt -s extglob` for `zsh`, ideally that would work with both of them? – Jivan Mar 23 '21 at 12:25
  • Yes, but I don't know what it is. Zsh can do far fancier stuff than that so I am sure it can do it, but if you need a zsh-specific way, then please edit your question to clarify. Also, the `find` approach given in the answers of the dupe should work for any shell. If that isn't enough and you still want a zsh-only solution, edit your question so it can be reopened. – terdon Mar 23 '21 at 12:26
  • looks like zsh and bash have completely different syntax and extensions – Jivan Mar 23 '21 at 12:29
  • I find it crazy that this question was marked as "duplicate" so quickly — I'm still struggling to adapt the answers from the other question — if that was a real duplicate I wouldn't be struggling – Jivan Mar 23 '21 at 12:36
  • 1
    OK, that's why I left the comment here so you could easily ping me and let me know if the dupe was not right. But please edit and explain _why_ the answers don't work for you. Simply saying it isn't a dupe when it certainly looks like one doesn't help us understand how to help you. – terdon Mar 23 '21 at 12:41
  • The commands in your case would be `shopt -s extglob; rm -r ~/foobar-!(as9df7)` in bash. Or, with `find` it would be `find ~/ -type d -name 'foobar-*' ! -name 'foobar-as9df7' -exec rm -r {} +` doesn't work. Since the main issue is the same, we prefer not to cover it again. If the problem was reaching the commands I just gave (which is understandable if you're not familiar with the syntax of the relevant tools), it is probably not worth having a separate question since the main answers are given in the dupe. – terdon Mar 23 '21 at 12:42
  • alright — I've tried to use the `find` command but somehow it says that `find: ‘foobar-1e91d’: No such file or directory` despite successfully removing it, which is weird – Jivan Mar 23 '21 at 12:50
  • Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/121182/discussion-between-terdon-and-jivan). – terdon Mar 23 '21 at 12:51

0 Answers0