<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" 
      xmlns:thr="http://purl.org/syndication/thread/1.0">
  <link rel="alternate" type="text/html" href="http://blog.tmcnet.com/blog/tom-keating/linux/greatest-linux-command-ever.asp" />
  <link rel="self" type="application/atom+xml" href="http://blog.tmcnet.com/blog/tom-keating/atom.xml" />
  <id>tag:blog.tmcnet.com,2016:/blog/tom-keating//4/tag:blog.tmcnet.com,2009:/blog/tom-keating//4.40081-</id>
  <updated>2016-03-16T16:34:57Z</updated>
  <title>Comments for Greatest Linux Command Ever!</title>
  <subtitle>VoIP &amp; Gadgets blog - Latest news in VoIP &amp; gadgets, wireless, mobile phones, reviews, &amp; opinions</subtitle>
  <generator uri="http://www.sixapart.com/movabletype/">Movable Type 4.38</generator>
  <entry>
    <id>tag:blog.tmcnet.com,2009:/blog/tom-keating//4.40081</id>
    <link rel="alternate" type="text/html" href="http://blog.tmcnet.com/blog/tom-keating/linux/greatest-linux-command-ever.asp" />
    <link rel="service.edit" type="application/atom+xml" href="http://blog.tmcnet.com/mt/mt-atom.cgi/weblog/blog_id=4/entry_id=40081" title="Greatest Linux Command Ever!" />
    <published>2009-03-11T14:22:16Z</published>
    <updated>2009-03-11T14:20:20Z</updated>
    <title>Greatest Linux Command Ever!</title>
    <summary>This is the greatest Linux command ever! Definitely my favorite.find ./ -name \*.html -printf &apos;%CD\t%p\n&apos; | grep &quot;03/10/08&quot; | awk &apos;{print $2}&apos; | xargs -t -i mv {} temp/ What it does is look (find) for files that end in...</summary>
    <author>
      <name>Tom Keating</name>
      <uri>http://blog.tmcnet.com/blog/tom-keating/</uri>
    </author>
    
    <category term="Computer Software" />
    
    <category term="Linux" />
    
    <category term="TMCnet" />
    
    <category term="Technology and Science" />
    
    <content type="html" xml:lang="en" xml:base="http://blog.tmcnet.com/blog/tom-keating/">
      <![CDATA[This is the greatest Linux command ever! Definitely my favorite.<br /><code>find ./ -name \*.html -printf '%CD\t%p\n' | grep "03/10/08" | awk '{print $2}' | xargs -t -i mv {} temp/</code><br /> <br /> <span style="display: inline;" class="mt-enclosure mt-enclosure-image"><img height="229" width="187" style="margin: 0pt 0pt 20px 20px; float: right;" class="mt-image-right" src="http://blog.tmcnet.com/blog/tom-keating/images/linux-penguin-logo.jpg" alt="linux-penguin-logo.jpg" /></span>What it does is look (find) for files that end in .html uses the printf option to format the 'find' output, then passes it to grep for searching for a certain date, then awk for printing a certain field, and finally xargs for executing a certain command.<br /><br />Let's break it down...<br /><br />The printf part within the find command has the format '%CD\t%p\n'.<br /> <br /><b> %Cx</b> = File's last status change time in the format specified by x. x=D. D=date in the format mm/dd/yy<br /><b> \t</b> = Horizontal tab<br /><b> %p</b> = file's name<br /><b> \n</b> = newline<br /> <br /> So basically it outputs the file's last status change followed by a horizontal tab, then the filename, and then a new line. But before it outputs it, it sends it to '<b>grep</b>' which searches the output and only outputs lines with "03/10/09".<br /> <br /> Example so far: (minus the awk, xargs and mv commands)<br /><b> find ./ -name \*.html -printf '%CD\t%p\n' | grep "03/10/09"</b><br />Outputs this: (notice the tab to separate the 2 fields)<br /> 03/10/09&#160;&#160;&#160;&#160;&#160;&#160;&#160; ./2005/05/index.html<br /> 03/10/09&#160;&#160;&#160;&#160;&#160;&#160;&#160; ./2005/03/index.html<br /> 03/10/09&#160;&#160;&#160;&#160;&#160;&#160;&#160; ./2005/04/index.html<br /> 03/10/09&#160;&#160;&#160;&#160;&#160;&#160;&#160; ./linked-in.html<br /> 03/10/09&#160;&#160;&#160;&#160;&#160;&#160;&#160; ./consumer-electronics/samsung-bribery-news.html<br /> 03/10/09&#160;&#160;&#160;&#160;&#160;&#160;&#160; ./technology/iptv/index.html<br /> <br /> Now send this output into the <b>awk</b> command (<b>awk '{print $2}'</b>) which parses it and pulls out the <b>2nd</b> column/field (hence the <i>tab character</i>), which is the filename, including the path.<br /><br />Here's the output you now have after adding <b>awk '{print $2}' </b>in:<br />./2005/05/index.html<br />./2005/03/index.html<br />./2005/04/index.html<br />./linked-in.html<br />./consumer-electronics/samsung-bribery-news.html<br />./technology/iptv/index.html<br /><br /> Next, send this output of "exact path + filename" to xargs for execution in the Linux shell.<br /> <br /> The "xargs -t -i <b>mv {} temp/</b>" part basically takes the input from the previous commands (<i>files named .html modified on 3/10/09</i>) and moves (<b>mv</b>) them to the <b>temp/</b> folder.<br /> <br /> The xargs command can do anything. So instead of moving the files, I could delete them, run <b>chmod</b> on them, or something else.<br /> <br /> It took me awhile to write this command. I've used various methods of finding files on Linux servers over the years, but this one is one of the most powerful. <br /><br />Definitely a command you should have in your Linux arsenal!<br /><br />p.s. Here's another tip. If you want to search ALL files (not just .html) then use the following command. Notice the \* and not * for the search. That part got me since I didn't think the * (wildcard) had to be backslashed. Usually when you backslash a character that means you want the 'literal' character specified after the \ (backslash) character. I didn't want filenames with a '*' in it. I wanted the wildcard. That threw me for a minute before I figured it out. Anyway, here's the command:<br /><code>find ./ -name \* -printf '%CD\t%p\n' | grep "03/10/08" | awk '{print $2}' | xargs -t -i mv {} temp/</code><br />]]>
      
    </content>
  </entry>

  <entry>
    <id>tag:blog.tmcnet.com,2009:/blog/tom-keating//4.40081-comment:43271</id>
    <thr:in-reply-to ref="tag:blog.tmcnet.com,2009:/blog/tom-keating//4.40081" type="text/html" href="http://blog.tmcnet.com/blog/tom-keating/linux/greatest-linux-command-ever.asp"/>
    <link rel="alternate" type="text/html" href="http://blog.tmcnet.com/blog/tom-keating/linux/greatest-linux-command-ever.asp#c43271" />
    <title>Comment from Glenn Stephens on 2009-03-11</title>
    <author>
        <name>Glenn Stephens</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>Wouldn't a "find ./ -name \*.html -mtime ???? -exec mv {} /temp/ \;" perform the same function using a single command? (Instead of launching an additional 2 text manipulation programs and a second shell environment.)</p>

<p>The time you're placing in the formated output to latter grep and manipulate is something find can filter on internally.</p>

<p>Also, a "find ./ -mtime ???? -exec mv {} /temp/" would work on all files, just remove the "-name" filter and it will find everything...</p>

<p>I'd suggest looking at the manual or info pages for find, it can filter results on access, modified or filesystem status change (most often file create), file and file system type as well as many other things. Also, it can format the output in many ways, especially the "-exec" flag, it can run anything...<br />
(Either "man find" or "info find" on most distros.)</p>]]>
    </content>
    <published>2009-03-12T00:43:54Z</published>
  </entry>

  <entry>
    <id>tag:blog.tmcnet.com,2009:/blog/tom-keating//4.40081-comment:43288</id>
    <thr:in-reply-to ref="tag:blog.tmcnet.com,2009:/blog/tom-keating//4.40081" type="text/html" href="http://blog.tmcnet.com/blog/tom-keating/linux/greatest-linux-command-ever.asp"/>
    <link rel="alternate" type="text/html" href="http://blog.tmcnet.com/blog/tom-keating/linux/greatest-linux-command-ever.asp#c43288" />
    <title>Comment from Clay on 2009-03-12</title>
    <author>
        <name>Clay</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>That's one more function than you need.</p>

<p>find ./ -name \*.html -printf '%CD\t%p\n' | awk '$1 ~ /03\/10\/08/ {print $2}' | xargs -t -i mv {} temp/</p>

<p>This will do the same without needing a call to grep. Awk lets you search through lines for a regex then take an action based on the match succeeding. The regex in this case needs the '/' escaped so it ends up looking like a picket-fence (\/\/\/\).</p>]]>
    </content>
    <published>2009-03-12T13:15:21Z</published>
  </entry>

  <entry>
    <id>tag:blog.tmcnet.com,2009:/blog/tom-keating//4.40081-comment:43319</id>
    <thr:in-reply-to ref="tag:blog.tmcnet.com,2009:/blog/tom-keating//4.40081" type="text/html" href="http://blog.tmcnet.com/blog/tom-keating/linux/greatest-linux-command-ever.asp"/>
    <link rel="alternate" type="text/html" href="http://blog.tmcnet.com/blog/tom-keating/linux/greatest-linux-command-ever.asp#c43319" />
    <title>Comment from john on 2009-03-13</title>
    <author>
        <name>john</name>
        <uri>http://choffee.co.uk</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://choffee.co.uk">
        <![CDATA[<p>I like a challenge :-)</p>

<p>would this do the same?</p>

<p>find -iname "*.html" -daystart -mtime 2 -exec mv "{}" temp/ \;</p>

<p>john</p>]]>
    </content>
    <published>2009-03-13T15:17:38Z</published>
  </entry>

  <entry>
    <id>tag:blog.tmcnet.com,2009:/blog/tom-keating//4.40081-comment:43323</id>
    <thr:in-reply-to ref="tag:blog.tmcnet.com,2009:/blog/tom-keating//4.40081" type="text/html" href="http://blog.tmcnet.com/blog/tom-keating/linux/greatest-linux-command-ever.asp"/>
    <link rel="alternate" type="text/html" href="http://blog.tmcnet.com/blog/tom-keating/linux/greatest-linux-command-ever.asp#c43323" />
    <title>Comment from Tom Keating on 2009-03-13</title>
    <author>
        <name>Tom Keating</name>
        <uri>http://blog.tmcnet.com/blog/tom-keating/</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://blog.tmcnet.com/blog/tom-keating/">
        <![CDATA[<p>John >> would this do the same? find -iname "*.html" -daystart -mtime 2 -exec mv "{}" temp/ \; john (1 reply) </p>

<p>Yes it would. But yours requires the user to calculate the date (-mtime 2). What if it was months or years ago? You'd have to count the number of days backwards and manually calculate the mtime. That's a pain. Mine allows you to use a nice clean date format.</p>]]>
    </content>
    <published>2009-03-13T17:54:40Z</published>
  </entry>

  <entry>
    <id>tag:blog.tmcnet.com,2009:/blog/tom-keating//4.40081-comment:43324</id>
    <thr:in-reply-to ref="tag:blog.tmcnet.com,2009:/blog/tom-keating//4.40081" type="text/html" href="http://blog.tmcnet.com/blog/tom-keating/linux/greatest-linux-command-ever.asp"/>
    <link rel="alternate" type="text/html" href="http://blog.tmcnet.com/blog/tom-keating/linux/greatest-linux-command-ever.asp#c43324" />
    <title>Comment from Tom Keating on 2009-03-13</title>
    <author>
        <name>Tom Keating</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>Clay - good point about using awk and eliminating grep. I'm so used to using grep for string searches. </p>]]>
    </content>
    <published>2009-03-13T17:57:07Z</published>
  </entry>

  <entry>
    <id>tag:blog.tmcnet.com,2009:/blog/tom-keating//4.40081-comment:43467</id>
    <thr:in-reply-to ref="tag:blog.tmcnet.com,2009:/blog/tom-keating//4.40081" type="text/html" href="http://blog.tmcnet.com/blog/tom-keating/linux/greatest-linux-command-ever.asp"/>
    <link rel="alternate" type="text/html" href="http://blog.tmcnet.com/blog/tom-keating/linux/greatest-linux-command-ever.asp#c43467" />
    <title>Comment from Alan Percy on 2009-03-20</title>
    <author>
        <name>Alan Percy</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>Tom - you really scare me!  I remember back in the mid-80's sitting in the darkened labs trying to work out complex one-line shell scripts (just for fun).  Some things just never die!</p>]]>
    </content>
    <published>2009-03-20T14:40:46Z</published>
  </entry>

  <entry>
    <id>tag:blog.tmcnet.com,2009:/blog/tom-keating//4.40081-comment:44353</id>
    <thr:in-reply-to ref="tag:blog.tmcnet.com,2009:/blog/tom-keating//4.40081" type="text/html" href="http://blog.tmcnet.com/blog/tom-keating/linux/greatest-linux-command-ever.asp"/>
    <link rel="alternate" type="text/html" href="http://blog.tmcnet.com/blog/tom-keating/linux/greatest-linux-command-ever.asp#c44353" />
    <title>Comment from dj on 2009-05-06</title>
    <author>
        <name>dj</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>Nice blog.</p>

<p>The asterisk is being interpreted by the shell. Put it in quotes. The man for 'find' says, "Please note that you should quote patterns as a matter of course, otherwise the shell will expand any wildcard characters in them."</p>

<p>The shell does globbing. Additional info: 'man 7 glob'</p>

<p>GNU/Linux Command-Line Tools Summary<br />
<a href="http://tldp.org/LDP/GNU-Linux-Tools-Summary/html/wildcards.html">http://tldp.org/LDP/GNU-Linux-Tools-Summary/html/wildcards.html</a></p>]]>
    </content>
    <published>2009-05-06T19:33:47Z</published>
  </entry>

</feed>
