4 ms·
Let me quote that thing you linked to: "As with many of the utilities that deal with filenames, the output of ls for multiple files or in one of the long listi
by LambdaComplex 9y ago
Let me quote that thing you linked to:
"As with many of the utilities that deal with filenames, the output of ls for multiple files or in one of the long listing formats must be used carefully on systems where filenames can contain embedded white space."
POSIX is such a system. If you try to parse the filenames output by ls, it will break when you encounter a filename with whitespace in it. Use a shell glob instead—it will actually work right.
- inopinatus 9y agoA shell glob cannot tell you the size, owner, permissions, timestamps of a file. For that .. oh look! There's ls(1). Which, by the way, spits out whitespace it sees just fine, unless you specified -q or implied it with output to a tty. If some code can't handle the result, it's that code that is broken. Tip: the only really problematic character is a newline; if you think you could be seeing those, you can still deal with it programatically, but I use find(1). In the meantime, I recommend you try deleting /bin/ls from your system and then filing bugs against everything that breaks.
- LambdaComplex 9y agoAlright, here's an example: touch 'file with spaces' for file in $(ls); do rm -- "$file" done This will not work, because $(ls) will get expanded to three items, not one. rm will be called on the files "file", "with", and "spaces"...which is not what was intended. Now do this: touch 'file with spaces' for file in *; do rm -- "$file" done This works correctly.
- aabbcc1241 9y agols -Q this will quote the file name for pipe operation
- LambdaComplex 9y agoStill doesn't work. touch 'file with spaces' for file in $(ls -Q); do echo "$file"; done This prints: "file with spaces"