3 ms·
When dealing with filenames one has to get used to always using '\0' separated output instead of newline separated output, as filenames in Linux can contain eve
by grumbel 5y ago
When dealing with filenames one has to get used to always using '\0' separated output instead of newline separated output, as filenames in Linux can contain everything except '\0' and '/'. Luckily most tools are prepared for this and have options to either output '\0' separators or accept them as input.
Dealing with all the files in the current directory would look something like this (for demonstration, can be made shorter by using '-name' or '-regex' option from 'find'):
find . -maxdepth 1 -type f -print0 | grep -z needle | xargs -n 1 -0 echo
Looping over '\0' separated output is possible as well with this:
find . -print0 | while read -r -d $'\0' filename; do echo "$filename"; done
It stops being brittle when used correctly, but can take a bit getting used to and can get a little verbose.
- benibela 5y agoThe one I found to delete all files was rm -- .[!.]* ..?* * 2>/dev/null