1

Why is not working? I'm trying to remove files ending by ~ and #

find . (-name "*~" -o -name "*#") -delete
jww
  • 97,681
  • 90
  • 411
  • 885
Jonathan
  • 53
  • 1
  • 8
  • Possible duplicate of [Which characters need to be escaped when using Bash?](https://stackoverflow.com/q/15783701/608639) – jww Oct 03 '18 at 21:52

2 Answers2

3

In POSIX shells (incl. bash) you need to escape the parentheses.

find .  \( -name "*~" -o -name "*#"  \) -delete

Here's a working example (run in an empty directory)

#!/usr/bin/bash -eu
mkdir -p a/b/c d/e f
touch a/a~ a/b/b~ 'd/e/e#' 'f/f#' 'root#' 'root~'
find .  \( -name "*~" -o -name "*#"  \) -delete
find .
Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
2

You need to escape the paranthesis so they get interpreted by find, not by the shell.

mkdir testme
cd testme
touch "foo~"
touch "bar#"
ls

# With parenthesis escaped
find . \( -name "*~" -o -name "*#" \) -delete

ls
cd ..
rmdir testme
DevSolar
  • 67,862
  • 21
  • 134
  • 209