2

I mistyped a directory name when building a project, and embarassingly, now I have a directory I cannot spell, so I can't run rm -rf ..<innomable>:

> cd buil
build-fix-memleak/             buil�d-master-debug/       build-master-debug     ...

> file "buil�d-master-debug" buil�d-master-debug: cannot open `buil�d-master-debug' (No such file or directory)

> find . -maxdepth 1 -type d -name "bui*" ./build-fix-memleak ./build-master-debug ..

> ls | grep "^buil" ... build-fix-memleak build-group-move build-master build-master-debug ..

I can't even grep it

How do I remove it without deleting all other build-* dirs?

EDIT: Okey I actually followed jesse_b comment, and moved everything useful into a temporary directory, cleaned the current, and moved everything back again

dgan
  • 264
  • 1
    what if you move build-master-debug temporarily and then rm bui*-master-debug? Or you might be able to just rm buil?d-master-debug – jesse_b Jul 22 '21 at 12:40
  • haha yes, that worked. I moved everything valuable into a temporary directory, ran rm -rf, and moved it back. Thanks for making me think straight – dgan Jul 22 '21 at 12:43
  • Would you want to post that as an answer? – Kusalananda Jul 22 '21 at 12:45
  • Using ls -b or some hex-tool should give you the bytes as well. – ibuprofen Jul 22 '21 at 13:16
  • @ibuprofen what would you do with that though? Use the $'code' format in a glob? Would that even work? – terdon Jul 22 '21 at 13:17
  • @terdon Mostly if one is curious to what the bytes are, secondly one can use it in printf. E.g. mv "$(printf 'Foo\x07\x13Some\xf8Thing')" new. (Was the thought.). But limited to how ls is implemented. for f in ./*; do printf %s "$f" | xxd -g 1 ; done is perhaps better for that. – ibuprofen Jul 22 '21 at 14:25
  • @ibuprofen yep, that makes sense, thanks. – terdon Jul 22 '21 at 14:26
  • As for why file "buil�d-master-debug" does not work is that is a generic replacement symbol for unprintable characters. If one copy it one do not actually copy the real bytes - like one sometimes can in some applications. – ibuprofen Jul 22 '21 at 15:19

1 Answers1

4

You could (probably) match it with a glob such as buil?*d-master-debug, where the ? matches a single character and the * matches zero or more characters. The ? requires a match and so prevents a match to the correct build-master-debug directory.

ls -d buil?*d-master-debug     # Check for a single match
rm -rf buil?*d-master-debug    # Remove that single match
Chris Davies
  • 116,213
  • 16
  • 160
  • 287
  • I haven't tried using globs with ls, but using these with find didn't work – dgan Jul 22 '21 at 13:23
  • @dgan because find is a different beast. If you do something like find . -name 'buil?*d-master-debug' -delete it should work (note the quotes around the glob, they're essential). – terdon Jul 22 '21 at 13:24
  • 1
    @dgan your ls | grep "^buil" would more idiomatically be written ls -d buil* (the -d flag prevents ls listing the contents of matching directories rather than the names of them) – Chris Davies Jul 22 '21 at 13:25