<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How do &amp;quot;find .... -exec cat /dev/null &amp;gt; {} \; &amp;quot; in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/how-do-quot-find-exec-cat-dev-null-gt-quot/m-p/4118256#M92883</link>
    <description>Robert,&lt;BR /&gt;&lt;BR /&gt;OK. You are rigth.&lt;BR /&gt;&lt;BR /&gt;But my question is a particular case of a more general one. Really I am looking for a way of use the shell redirection ("&amp;gt;") inside the "-exec" clause of "find".&lt;BR /&gt;&lt;BR /&gt;Any idea?&lt;BR /&gt;</description>
    <pubDate>Tue, 18 Dec 2007 15:26:32 GMT</pubDate>
    <dc:creator>Victor M. Gomez Hornill</dc:creator>
    <dc:date>2007-12-18T15:26:32Z</dc:date>
    <item>
      <title>How do "find .... -exec cat /dev/null &gt; {} \; "</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-do-quot-find-exec-cat-dev-null-gt-quot/m-p/4118254#M92881</link>
      <description>&lt;!--!*#--&gt;&lt;BR /&gt;Hello everyone,&lt;BR /&gt;&lt;BR /&gt;I am looking for a way of reset files (cat /dev/null &amp;gt; MY_FILE) using the "find ... -exec" construction. This doesn't work:&lt;BR /&gt;&lt;BR /&gt; find  /tmp -name "my_file.pp.*" -mtime "+15" -exec cat /dev/null &amp;gt; {}  \;&lt;BR /&gt;&lt;BR /&gt;neither:&lt;BR /&gt;&lt;BR /&gt; find  /tmp -name "my_file.pp.*" -mtime "+15" -exec cat /dev/null \&amp;gt; {}  \;&lt;BR /&gt;&lt;BR /&gt; find  /tmp -name "my_file.pp.*" -mtime "+15" -exec " cat /dev/null &amp;gt; {} " \;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;I also know this other method to do the job (it works OK):&lt;BR /&gt;&lt;BR /&gt;find  /tmp -name "my_file.pp.*" -mtime "+15" -print | while read file_to_reset&lt;BR /&gt;      do&lt;BR /&gt;        cat /dev/null &amp;gt; $file_to_reset&lt;BR /&gt;      done&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Someone knows how to write a working "-exec" versión (please try before) similar to the first example ?&lt;BR /&gt;&lt;BR /&gt;Thanks,&lt;BR /&gt;Victor&lt;BR /&gt;</description>
      <pubDate>Tue, 18 Dec 2007 14:53:07 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-do-quot-find-exec-cat-dev-null-gt-quot/m-p/4118254#M92881</guid>
      <dc:creator>Victor M. Gomez Hornill</dc:creator>
      <dc:date>2007-12-18T14:53:07Z</dc:date>
    </item>
    <item>
      <title>Re: How do "find .... -exec cat /dev/null &gt; {} \; "</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-do-quot-find-exec-cat-dev-null-gt-quot/m-p/4118255#M92882</link>
      <description>well this works&lt;BR /&gt;# find . -type f -exec cp /dev/null {} \;&lt;BR /&gt;&lt;BR /&gt;Edited your command.&lt;BR /&gt;# find  /tmp -type f -name "my_file.pp.*" -mtime "+15" -exec cp /dev/null {} \;&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;Robert-Jan</description>
      <pubDate>Tue, 18 Dec 2007 14:58:55 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-do-quot-find-exec-cat-dev-null-gt-quot/m-p/4118255#M92882</guid>
      <dc:creator>Robert-Jan Goossens</dc:creator>
      <dc:date>2007-12-18T14:58:55Z</dc:date>
    </item>
    <item>
      <title>Re: How do "find .... -exec cat /dev/null &gt; {} \; "</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-do-quot-find-exec-cat-dev-null-gt-quot/m-p/4118256#M92883</link>
      <description>Robert,&lt;BR /&gt;&lt;BR /&gt;OK. You are rigth.&lt;BR /&gt;&lt;BR /&gt;But my question is a particular case of a more general one. Really I am looking for a way of use the shell redirection ("&amp;gt;") inside the "-exec" clause of "find".&lt;BR /&gt;&lt;BR /&gt;Any idea?&lt;BR /&gt;</description>
      <pubDate>Tue, 18 Dec 2007 15:26:32 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-do-quot-find-exec-cat-dev-null-gt-quot/m-p/4118256#M92883</guid>
      <dc:creator>Victor M. Gomez Hornill</dc:creator>
      <dc:date>2007-12-18T15:26:32Z</dc:date>
    </item>
    <item>
      <title>Re: How do "find .... -exec cat /dev/null &gt; {} \; "</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-do-quot-find-exec-cat-dev-null-gt-quot/m-p/4118257#M92884</link>
      <description>Hi Victor:&lt;BR /&gt;&lt;BR /&gt;In cases like this, create a small shell script that is 'exec'ed.  For example:&lt;BR /&gt;&lt;BR /&gt;# find /tmp -type f -name "my_file.pp*" -mtime +15 -exec /tmp/wrapper {} \;&lt;BR /&gt;&lt;BR /&gt;...where:&lt;BR /&gt;&lt;BR /&gt;# cat /tmp/wrapper&lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;cat /dev/null &amp;gt; $@&lt;BR /&gt;exit 0&lt;BR /&gt;&lt;BR /&gt;NOTE that I deliberately chose the form of '-exec' that spawns ONE process per invocation.  This allows the executed script to be simple insofar as only one file argument is expected.  If you use '-exec /tmp/wrapper {} +' instead, multiple file names would be passed and the wrapper would need to look like:&lt;BR /&gt;&lt;BR /&gt;# cat /tmp/wrapper&lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;while [ $# -gt 0 ]&lt;BR /&gt;do&lt;BR /&gt;    cat /dev/null &amp;gt; $1&lt;BR /&gt;    shift&lt;BR /&gt;done&lt;BR /&gt;exit 0&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Tue, 18 Dec 2007 16:35:57 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-do-quot-find-exec-cat-dev-null-gt-quot/m-p/4118257#M92884</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2007-12-18T16:35:57Z</dc:date>
    </item>
    <item>
      <title>Re: How do "find .... -exec cat /dev/null &gt; {} \; "</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-do-quot-find-exec-cat-dev-null-gt-quot/m-p/4118258#M92885</link>
      <description>&amp;gt;I am looking for a way of use the shell redirection ("&amp;gt;") inside the "-exec" clause of "find".&lt;BR /&gt;&lt;BR /&gt;It looks like you can't do it.  I tried using \&amp;gt; and even using sh -c "&amp;gt; {}".  Unfortunately -exec doesn't replace the {} and so the shell creates a file "{}".&lt;BR /&gt;&lt;BR /&gt;So you need to use cp as Robert-Jan suggests.  Or do as JRF suggests, write an auxiliary script.  Or use your while read loop.&lt;BR /&gt;Note: you don't need to use cat(1) in your script.  "&amp;gt; $1" works just as well.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 19 Dec 2007 03:37:49 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-do-quot-find-exec-cat-dev-null-gt-quot/m-p/4118258#M92885</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2007-12-19T03:37:49Z</dc:date>
    </item>
    <item>
      <title>Re: How do "find .... -exec cat /dev/null &gt; {} \; "</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-do-quot-find-exec-cat-dev-null-gt-quot/m-p/4118259#M92886</link>
      <description>To make shell redirection work inside the "find  ... -exec" clause, you must first cause a shell to be started inside the -exec.&lt;BR /&gt;&lt;BR /&gt;Think about how the entire command line is parsed:&lt;BR /&gt;&lt;BR /&gt;1.) The shell expands any unescaped variables and performs any unescaped redirections before the find command is executed. The "{}" construct has no special meaning for the shell, so it's left unchanged. Any escaped characters become unescaped.&lt;BR /&gt;&lt;BR /&gt;2.) The find command begins executing, and when finding a file that matches the conditions, it fork()s a new process, replaces the "{}" with the name of the file and exec()s the command.&lt;BR /&gt;NOTE: there is no shell involved in starting the command.&lt;BR /&gt;&lt;BR /&gt;3.) To use the shell redirection for each matching file separately, a shell must start up at this point. Furthermore, it must take its command input from the command line. Looks like "sh -c" might be needed here.&lt;BR /&gt;&lt;BR /&gt;So, here's my first idea:&lt;BR /&gt;&lt;BR /&gt;find /tmp -type f -name "my_file.pp*" -mtime +15 -exec sh -c "&amp;gt; {}" \;&lt;BR /&gt;&lt;BR /&gt;Works on Linux... but not on HP-UX. Hmm.&lt;BR /&gt;&lt;BR /&gt;After a bit of experimentation, it seems that HP-UX "find -exec" does not like having its {} enclosed in double quotes. In addition, the sh of HP-UX (on my "toy" 11.00 at least) apparently does not need nor want the -c option for this.&lt;BR /&gt;&lt;BR /&gt;So, let's try an alternative method of quoting:&lt;BR /&gt;&lt;BR /&gt;find /tmp -type f -name "my_file.pp*" -mtime +15 -exec sh \&amp;gt; {} \;&lt;BR /&gt;&lt;BR /&gt;MK</description>
      <pubDate>Wed, 19 Dec 2007 08:51:32 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-do-quot-find-exec-cat-dev-null-gt-quot/m-p/4118259#M92886</guid>
      <dc:creator>Matti_Kurkela</dc:creator>
      <dc:date>2007-12-19T08:51:32Z</dc:date>
    </item>
    <item>
      <title>Re: How do "find .... -exec cat /dev/null &gt; {} \; "</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-do-quot-find-exec-cat-dev-null-gt-quot/m-p/4118260#M92887</link>
      <description>&amp;gt;MK: sh apparently does not need nor want the -c option for this.&lt;BR /&gt;&lt;BR /&gt;The rotten man page doesn't go into any detail on mixing -c "string" with arg.  Probably because it thinks the first arg must be a script.&lt;BR /&gt;&lt;BR /&gt;&amp;gt;let's try an alternative method of quoting:&lt;BR /&gt;... -exec sh \&amp;gt; {} \;&lt;BR /&gt;&lt;BR /&gt;Well this works.  But adding any other tokens like cat would make it fail.&lt;BR /&gt;&lt;BR /&gt;I'm not sure I'd depend on this.&lt;BR /&gt;</description>
      <pubDate>Wed, 19 Dec 2007 09:31:22 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-do-quot-find-exec-cat-dev-null-gt-quot/m-p/4118260#M92887</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2007-12-19T09:31:22Z</dc:date>
    </item>
    <item>
      <title>Re: How do "find .... -exec cat /dev/null &gt; {} \; "</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-do-quot-find-exec-cat-dev-null-gt-quot/m-p/4118261#M92888</link>
      <description>Yes, that's true.&lt;BR /&gt;&lt;BR /&gt;I don't have POSIX standard documents handy, but I did some tests on a Sun Solaris. It seems to behave the same, so I think this might be POSIXLY_CORRECT behaviour. Maybe the "most obvious" implementation of the standard is not the most useful one?&lt;BR /&gt;&lt;BR /&gt;The problem seems to be two-fold:&lt;BR /&gt;&lt;BR /&gt;1.) The "-c" option of sh seems to have some hidden assumptions and the man page of "sh-posix" does not offer any details.&lt;BR /&gt;If you do&lt;BR /&gt;&lt;BR /&gt;sh -c 'echo !$0!$1!$2!' foo&lt;BR /&gt;&lt;BR /&gt;the output is&lt;BR /&gt;&lt;BR /&gt;!foo!!!&lt;BR /&gt;&lt;BR /&gt;which seems to indicate the first arg becomes $0, not $1 as one might expect.&lt;BR /&gt;&lt;BR /&gt;2.) The "find ... -exec" requires "{}" as a separate command line parameter. It cannot be embedded in a longer string. With GNU find, this is allowed.&lt;BR /&gt;&lt;BR /&gt;The interaction between these two things makes it hard or impossible to write a reliable one-liner for this kind of job.&lt;BR /&gt;&lt;BR /&gt;MK</description>
      <pubDate>Wed, 19 Dec 2007 14:43:29 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-do-quot-find-exec-cat-dev-null-gt-quot/m-p/4118261#M92888</guid>
      <dc:creator>Matti_Kurkela</dc:creator>
      <dc:date>2007-12-19T14:43:29Z</dc:date>
    </item>
    <item>
      <title>Re: How do "find .... -exec cat /dev/null &gt; {} \; "</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-do-quot-find-exec-cat-dev-null-gt-quot/m-p/4118262#M92889</link>
      <description>Hi (again) Victor:&lt;BR /&gt;&lt;BR /&gt;There is yet another way to trim your files; use Perl:&lt;BR /&gt;&lt;BR /&gt;# perl -MFile::Find -e 'die unless @ARGV;find(sub{truncate($File::Find::name,0) if -f &amp;amp;&amp;amp; -M&amp;gt;15 &amp;amp;&amp;amp; m/my_file\.pp\..*/},@ARGV)' /tmp&lt;BR /&gt;&lt;BR /&gt;This performs the same selection that you showed in your opening post with the addition that the action is restricted only to *files* in the directory offered.&lt;BR /&gt;&lt;BR /&gt;For safety, I have let the script fail unless a directory (path) is provided as the command-line script's argument.&lt;BR /&gt;&lt;BR /&gt;You will probably find this to be faster than 'exec'ing any wrapper script as I first suggested since everything is done in the one Perl process.&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Wed, 19 Dec 2007 15:44:47 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-do-quot-find-exec-cat-dev-null-gt-quot/m-p/4118262#M92889</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2007-12-19T15:44:47Z</dc:date>
    </item>
  </channel>
</rss>

