[H-GEN] Useful not so obvious tips

Michael Anthon michael at anthon.net
Tue Oct 26 06:08:18 EDT 2004


Anthony Towns wrote:

>>>belial:~# find . -name file\* -print0 -maxdepth 1 | xargs -0 -i ls {}
>>>./files.txt
>>>./file with spaces
>>>Does them one at a time (useful is the program only takes one argument)
>>>      
>>>
>>You could also use:
>>    find . -type f -printf '"%p" ' | xargs ls
>>    
>>
>
>Err, you could also use:
>
>	find . -name file\* -print0 -maxdepth 1 | xargs -0 ls
>  
>
I don't believe either of these incantations produce the same result as 
the original command.  I understand that there is no benefit to using it 
for the ls command but I was simply using that as a trivial example of 
the effect of the -i option to xargs.

While I'm on it and in a similar vein to something Greg said and back 
more to the original topic I often use a little trick [1] to make sure I 
don't do something particularly stupid.  Occasionally I have a need to 
perform what I consider to be potentially dangerous things (in terms of 
dangerous to my data).  For example, I might want to find a bunch of 
files and run a command on them all so I might generate a bash one liner 
to do this... BUT, instead of, say doing "find blah | xargs -i rm -rf" I 
will do "find blah | xargs -i echo rm -rf".  This produces a nice little 
shell script on stdout that I can then inspect to see if it was really 
what I wanted and can edit the script until it gives me what I think I 
need.  When I'm happy with that, rather than go remove the echo I simply 
add "| sh" onto the end. 

Not very tricky but simple and effective


Cheers,
Michael

[1] Trick is probably stretching the meaning of the term a little 8^p




More information about the General mailing list