Pages: Welcome | Projects

The reason why dir

2018/6/26
Tags: [ Today I learned ]

As a "Today I Learned" moment, I'll tell about the dir command.

For some reason I woke up this morning with a Why do we have dir question. I always thought it is a matter of compatibility with something, but I never really investigated.

I found this answer quite useful.

A quite interesting thing is that it handles newlines in files correctly, which is definitely problematic under bash!

dacav@lolcalhost:tmp.M8h8Ku4WuF$ touch $'test\nlol'
dacav@lolcalhost:tmp.M8h8Ku4WuF$ ls
'test'$'\n''lol'
dacav@lolcalhost:tmp.M8h8Ku4WuF$ dir
test\nlol
dacav@lolcalhost:tmp.M8h8Ku4WuF$ for i in $(ls); do echo $i; done
test
lol
dacav@lolcalhost:tmp.M8h8Ku4WuF$ for i in $(dir); do echo $i; done
test\nlol

I'm not sure it can be used to make it right (it is probably plain impossible, but it might be helpful.