Processor
Processor

Reputation: 324

Delete folders with ^M inside name

On linux a buggy script created folders with this : ^M at the end of the name. The ^M is moved to ? with a ls -al.

A search on the ? is not working as a search on ^M (or ^M)

How can I remove these folders?

Upvotes: 3

Views: 394

Answers (1)

Léa Gris
Léa Gris

Reputation: 19625

What you need is use a c-style string where you can specify the ^M or Carriage Return characters with its backslash escape \r.

A c-style string is made of $ and single-quotes: $'a c-style string'

Find the bugged directories:

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

List the bugged directory names:

ls -d *$'\r'

Rename the bugged directories by removing the trailing CR.

find . -type d -name $'*\r' -execdir sh -c $'for d; do mv -- "$d" "${d%\r}"; done' _ {} +

Delete the faulty named directories in current directory, with their content:

rm -r -- *$'\r'

Upvotes: 6

Related Questions