1

Somehow, I seem to have generated files with a carriage return (\r) in the filename:

$ ls -1 tri-rods.tm.dat* tri-rods.tm.dat 'tri-rods.tm.dat'$'\r' $ ls tri-rods.tm.dat? 'tri-rods.tm.dat'$'\r' 

I tried find with "\r", but it finds nothing:

$ find . -type f -name '*\r' 

How can I list/find and remove such files? Adding "?" to the filename works, so I could delete them one by one, but I would prefer a more general way.

Note: I am trying to do this on Windows via Cygwin/git-bash/Windows Subsystem for Linux, so maybe some commands don't work as expected.

1 Answer 1

3

It finds nothing because the -name test takes a shell glob and globs don't know \r. Assuming your Cygwin shell supports $' ' notation, you could do:

find . -name '*'$'\r''*' 

So, to delete, you can do:

find . -name '*'$'\r''*' -delete 

Or, if your find doesn't have the -delete action, use:

find . -name '*'$'\r''*' -exec rm {} + 

The -regex test might seem like the best option, but unfortunately, none of the regex flavors supported by find know about backslash-letter escapes (also see this answer):

$ find . -regextype findutils-default -regex '.*\r.*' $ find . -regextype ed -regex '.*\r.*' $ find . -regextype emacs -regex '.*\r.*' $ find . -regextype gnu-awk -regex '.*\r.*' $ find . -regextype grep -regex '.*\r.*' $ find . -regextype posix-awk -regex '.*\r.*' $ find . -regextype awk -regex '.*\r.*' $ find . -regextype posix-basic -regex '.*\r.*' $ find . -regextype posix-egrep -regex '.*\r.*' $ find . -regextype egrep -regex '.*\r.*' $ find . -regextype posix-extended -regex '.*\r.*' $ find . -regextype posix-minimal-basic -regex '.*\r.*' $ find . -regextype sed -regex '.*\r.*' 

Only the first one, with $'\r' worked for me:

$ find . -name '*'$'\r''*' ./bad?file 
4
  • @AdminBee not according to GIlles's answer: Why can't find -regex match a newline?. And I just tried it and can confirm he's right. Commented Oct 2, 2020 at 9:06
  • Excellent, thanks. Maybe add "find . -name ''$'\r''' -delete -print" for completeness to remove the files (verbosily). I tested and it works on all 3: Cygwin, git-bash and WSL. :) Commented Oct 2, 2020 at 9:24
  • Also, any more infos on the $' ' notation? I never heard of it before. Commented Oct 2, 2020 at 9:26
  • 1
    @KIAaze it's called "ANSI quoting".Note how you actually have it in the output of ls shown in your question. And good point, I added a note about deleting, thanks. Commented Oct 2, 2020 at 9:30

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.