3 ms·
Traditionally Unix filenames can contain everything except for / and null (aka \0). This is why the find utility provides a facility for printing null terminate
by mustpax 17y ago
Traditionally Unix filenames can contain everything except for / and null (aka \0). This is why the find utility provides a facility for printing null terminated filenames (-print0), when used in conjunction with xargs -0 this is a fool proof way of iterating over files in the shell.
For example, to delete all .o files in the current directory and its subdirectories:
find . -name "*.o" -print0 | xargs -0 rm -f
- BrandonM 17y agoWhy not just find . -name "*.o" -delete ?
- michaelcampbell 17y agoThat's fine for /this/ example because find provides that as an option. But you might want to grep for something in many files of varying depths, or run some home-brewed script on them for which find does not provide a built-in. -exec works of course, but spawns grep (or whatever) once per file; using xargs saves considerable time since it passes lots of files to 1 command. Or, you can use zsh. rm **/*.o grep foo **/*.{java,sql,xml} # Where is that blasted IBM utility? ls **/*pmt*(x.) # list e(x)ecutable files (.) that have "pmt" in their name. # Show me all the "Setup" dirs in my project ls -d **/Setup(/)