[Gllug] C and Unix pioneer Dennis Ritchie reported dead

Paul Hewlett phewlett76 at gmail.com
Tue Nov 1 16:38:21 UTC 2011


Hi Guys

------------------------------------------
Given these files :

paul at gould:~/test> find -type f -exec cat -- {} \; -print
fish
./fish.html.fred
fish
./-r hello.html
fish
./hello world.html
fish
./fred.html
paul at gould:~/test>
---------------------------------------------
This command works for me - (adding -print to show which files are
processed)

paul at gould:~/test> find -name '*.html' -type f -exec sed -i -e
's/fish/chips/g' -- {} \; -print
./-r hello.html
./hello world.html
./fred.html
---------------------------------------------------------------
Show the results:

paul at gould:~/test> find -type f -exec cat -- {} \; -print
fish
./fish.html.fred
chips
./-r hello.html
chips
./hello world.html
chips
./fred.html
paul at gould:~/test>

I always put single quotes around the wildcard to -name. In my experience
'find' always intelligently handles filenames with spaces etc so is usually
a better
solution than explicit loops.

Regards
Paul


On 1 November 2011 14:58, James Hawtin <oolon at ankh.org> wrote:

> tid wrote:
>
>> A colleague asked my opinion as to whether I felt the following was a
>> correct answer to an test question:
>>
>>  Q: Please supply a one-line command to substitute all occurences of
>> 'fish' to 'chips' in all files ending in .html
>>  -A: $ for i in *.html ; do sed -i -e 's/fish/chips/g' $i ; done
>>
>>  I think it is valid I see no reason why a for loop is worse than say
>
> find . -name "*.html" -exec sed -i -e 's/fish/chips/g' '{}' \;
>
> However this is ALSO wrong... it should be
>
> find . -name "*.html" -exec sed -i -e 's/fish/chips/g' -- '{}' \;
>
> because it would not work with "-r hello.html" or "hello world.html"
>
> $ ls -l
> total 0
> -rw-rw-r-- 1 oolon ankh 0 Nov  1 14:43 fish.html.fred
> -rw-rw-r-- 1 oolon ankh 0 Nov  1 14:52 fred.html
> -rw-rw-r-- 1 oolon ankh 0 Nov  1 14:51 hello world.html
> -rw-rw-r-- 1 oolon ankh 0 Nov  1 14:51 -r hello.html
>
> for i in *.html ; do sed -i -e 's/fish/chips/g' $i ; done
> sed: can't read hello: No such file or directory
> sed: can't read world.html: No such file or directory
> sed: can't read hello.html: No such file or directory
>
> Personally I hate using -exec (not sure why) and generally do a "find |
> while read" insted, because a for loop from a glob has a finite number of
> argument it can handle (however it is very large with linux these days).
>
> James
> --
> Gllug mailing list  -  Gllug at gllug.org.uk
> http://lists.gllug.org.uk/**mailman/listinfo/gllug<http://lists.gllug.org.uk/mailman/listinfo/gllug>
>



-- 
Tel: +44 79 8532 7353
LinkedIn: http://uk.linkedin.com/pub/paul-hewlett/0/629/9b4
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.lug.org.uk/pipermail/gllug/attachments/20111101/91fdaada/attachment.html>
-------------- next part --------------
--
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug


More information about the GLLUG mailing list