This is a little anecdote from earlier on why not running root is vital.
I was sorting my home directory and deleted a few compressed files I had, I wrote
ls . | grep -P 'zip|tar|7z' | xargs rm and thought, hey I could also write this as rm -r $(ls . | grep -P '...') I suppose.
The second part I didn't mean to use it since there was nothing to delete, it was morelike a mental exercise, I wrote it next to the last command with a 'divider' to visually compare them.
ls . | grep -P 'zip|tar|7z' | xargs rm **//** rm -r $(ls . | grep -P '...')
Being **//** the "divider" and ... the mental "substitute" for 'zip|tar..'
I thought this wouldn't run but to my surprise, it acted as rm -r /and tried to delete everything, luckily permissions saved me and nothing was deleted.
But I'm curious why it'd work that way,
my guess is that rm **//** somehow translated to rm / but I'm not sure.
In the
zshshell,**//**would expand to all names under/as well to all names below the current directory (recursively).From an empty directory on my system:
Why? Well,
**/matches all directories recursively under the current directory. More importantly, it matches the current directory, but since the current directory's name is not available inside the current directory, there's no entry returned for that.However, when you add a
/to that to create**//, then you get a lone/back for the current directory. Again in an empty directory:Then, if you add a further
**to make**//**, you pick up all names from the root directory, together with all names from the current directory and below (directory names from the current directory and below would occur twice in the list).Your
xargsis callingIf you're using GNU
rm, it will helpfully rearrange the command line so that it is interpreted the same asWhat this does should now be clear.
If you want to delete all regular files in the current directory that have filename suffixes
.zip,.taror.7z, usein the
zshshell. If want to do that recursively down into subdirectories, useThe glob qualifier
(.)makes the globbing pattern only match regular files. You could even restrict it to files above a certain size, say 10MB, with./**/*.(zip|tar|7z)(.Lm+10).