3 ms·
find(1) is super useful for any hierarchical or filtering case, and I use it often, but it can also be arcane and unwieldy. In many circumstances your cwd shel
by inopinatus 6y ago
find(1) is super useful for any hierarchical or filtering case, and I use it often, but it can also be arcane and unwieldy. In many circumstances your cwd shell iterator can be simpler:
for fname in *; do echo "$fname"; done
This skips dotfiles, of course, but that’s intentional. The point of dotfiles is to be skipped.
With bash or on GNU systems try
printf "%q\n" "$fname"
instead of echo, to obtain an escaped string.
- vbezhenar 6y agoI thought that it would iterate over space-separated tokens. But it works, some magic here, thanks, that's definitely the proper way to go.
- inopinatus 6y agoIt's because the result of a glob is not immediately subjected to shell expansion.
- samatman 6y agowon't this echo "*" if there are no files in the directory? I always have to look up the 'safe iteration' invocation when iterating files in a directory, because it involves jumping through a few more hoops than is really reasonable.
- eMSF 6y agoYes. You can change the behaviour in bash by setting the nullglob option (shopt -s nullglob).
- deleted 6y ago[deleted]