1

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

find . (-name "*~" -o -name "*#") -delete
jww
  • 90,984
  • 81
  • 374
  • 818
Jonathan
  • 53
  • 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 .
PSkocik
  • 55,062
  • 6
  • 86
  • 132
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
  • 63,860
  • 19
  • 125
  • 201