Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fedora - Command line quick tips: Locate and process files with find and xargs

#1
Command line quick tips: Locate and process files with find and xargs

<div><p><strong>find</strong> is one of the more powerful and flexible command-line programs in the daily toolbox. It does what the name suggests: it finds files and directories that match the conditions you specify. And with arguments like <strong>-exec</strong> or <strong>-delete</strong>, you can have find take action on what it…&nbsp;finds. </p>
<p>In this installment of the <a href="https://fedoramagazine.org/?s=command+line+quick+tips">Command Line Quick Tips</a> series, you’ll get an introduction to the <strong>find</strong> command and learn how to use it to process files with built-in commands or the <strong>xargs</strong> command.</p>
<p> <span id="more-29440"></span> </p>
<h2>Finding files</h2>
<p>At a minimum, <strong>find</strong> takes a path to find things in. For example, this command will find (and print) every file on the system:</p>
<pre class="wp-block-preformatted">find /</pre>
<p>And since everything is a file, you will get a lot of output to sort through. This probably doesn’t help you locate what you’re looking for. You can change the path argument to narrow things down a bit, but it’s still not really any more helpful than using the <strong>ls</strong> command. So you need to think about what you’re trying to locate.</p>
<p>Perhaps you want to find all the JPEG files in your home directory. The <strong>-name</strong> argument allows you to restrict your results to files that match the given pattern.</p>
<pre class="wp-block-preformatted">find ~ -name '*jpg'</pre>
<p>But wait! What if some of them have an uppercase extension? <strong>-iname</strong> is like <strong>-name</strong>, but it is case-insensitive:</p>
<pre class="wp-block-preformatted">find ~ -iname '*jpg'</pre>
<p>Great! But the 8.3 name scheme is so 1985. Some of the pictures might have a .jpeg extension. Fortunately, we can combine patterns with an “or,” represented by <strong>-o</strong>. The parentheses are escaped so that the shell doesn’t try to interpret them instead of the <strong>find</strong> command.</p>
<pre class="wp-block-preformatted">find ~ \( -iname 'jpeg' -o -iname 'jpg' \)</pre>
<p>We’re getting closer. But what if you have some directories that end in jpg? (Why you named a directory <strong>bucketofjpg</strong> instead of <strong>pictures</strong> is beyond me.) We can modify our command with the <strong>-type</strong> argument to look only for files:</p>
<pre class="wp-block-preformatted">find ~ \( -iname '*jpeg' -o -iname '*jpg' \) -type f</pre>
<p>Or maybe you’d like to find those oddly named directories so you can rename them later:</p>
<pre class="wp-block-preformatted">find ~ \( -iname '*jpeg' -o -iname '*jpg' \) -type d</pre>
<p>It turns out you’ve been taking a lot of pictures lately, so narrow this down to files that have changed in the last week with <strong>-mtime</strong> (modification time). The <strong>-7</strong> means all files modified in 7 days or fewer. </p>
<pre class="wp-block-preformatted">find ~ \( -iname '*jpeg' -o -iname '*jpg' \) -type f -mtime -7</pre>
<h2>Taking action with xargs</h2>
<p>The <strong>xargs</strong> command takes arguments from the standard input stream and executes a command based on them. Sticking with the example in the previous section, let’s say you want to copy all of the JPEG files in your home directory that have been modified in the last week to a thumb drive that you’ll attach to a digital photo display. Assume you already have the thumb drive mounted as <em>/media/photo_display</em>.</p>
<pre class="wp-block-preformatted">find ~ \( -iname '*jpeg' -o -iname '*jpg' \) -type f -mtime -7 -print0 | xargs -0 cp -t /media/photo_display</pre>
<p>The <strong>find</strong> command is slightly modified from the previous version. The <strong>-print0</strong> command makes a subtle change on how the output is written: instead of using a newline, it adds a null character. The<strong> -0</strong> (zero) option to <strong>xargs</strong> adjusts the parsing to expect this. This is important because otherwise actions on file names that contain spaces, quotes, or other special characters may not work as expected. You should use these options whenever you’re taking action on files.</p>
<p>The <strong>-t</strong> argument to <strong>cp</strong> is important because <strong>cp</strong> normally expects the destination to come last. You can do this without <strong>xargs</strong> using <strong>find</strong>‘s <strong>-exec</strong> command, but the <strong>xargs</strong> method will be faster, especially with a large number of files, because it will run as a single invocation of <strong>cp</strong>. </p>
<h2>Find out more</h2>
<p>This post only scratches the surface of what <strong>find</strong> can do. <strong>find</strong> supports testing based on permissions, ownership, access time, and much more. It can even compare the files in the search path to other files. Combining tests with Boolean logic can give you incredible flexibility to find exactly the files you’re looking for. With build in commands or piping to <strong>xargs</strong>, you can quickly process a large set of files.</p>
<p><em>Portions of this article were previously published on <a href="https://opensource.com/article/18/4/how-use-find-linux">Opensource.com</a>.</em> <em>Photo by&nbsp;</em><a href="https://unsplash.com/@wflwong?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText"><em>Warren Wong</em></a><em>&nbsp;on&nbsp;<a href="https://unsplash.com/s/photos/search?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></em>.</p>
</div>


https://www.sickgaming.net/blog/2019/10/...and-xargs/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016