<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: techen spectre</title>
    <description>The latest articles on DEV Community by techen spectre (@techen_spectre_6069be436d).</description>
    <link>https://dev.to/techen_spectre_6069be436d</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4220983%2F0df68489-e544-4841-8860-1c061801bd91.png</url>
      <title>DEV Community: techen spectre</title>
      <link>https://dev.to/techen_spectre_6069be436d</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/techen_spectre_6069be436d"/>
    <language>en</language>
    <item>
      <title>`xargs rm` was aimed at one file and hit a different one</title>
      <dc:creator>techen spectre</dc:creator>
      <pubDate>Fri, 06 Nov 2026 18:59:40 +0000</pubDate>
      <link>https://dev.to/techen_spectre_6069be436d/xargs-rm-was-aimed-at-one-file-and-hit-a-different-one-515f</link>
      <guid>https://dev.to/techen_spectre_6069be436d/xargs-rm-was-aimed-at-one-file-and-hit-a-different-one-515f</guid>
      <description>&lt;p&gt;There are two files here, and you want to delete the meeting notes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;ls
&lt;/span&gt;meeting notes.txt
notes.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So you'd write this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; | &lt;span class="nb"&gt;grep &lt;/span&gt;meeting | xargs &lt;span class="nb"&gt;rm&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before running it, ask xargs what command it's about to build:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; | &lt;span class="nb"&gt;grep &lt;/span&gt;meeting | xargs &lt;span class="nb"&gt;echo rm
rm &lt;/span&gt;meeting notes.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's &lt;strong&gt;two&lt;/strong&gt; arguments: &lt;code&gt;meeting&lt;/code&gt;, which doesn't exist, and &lt;code&gt;notes.txt&lt;/code&gt;, which is the other file. Running it would leave the meeting notes alone and delete &lt;code&gt;notes.txt&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The mental model most people have is wrong
&lt;/h2&gt;

&lt;p&gt;People tend to read &lt;code&gt;| xargs cmd&lt;/code&gt; as "send the input to &lt;code&gt;cmd&lt;/code&gt;". That's what a plain pipe does:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;echo &lt;/span&gt;notes.txt | &lt;span class="nb"&gt;cat
&lt;/span&gt;notes.txt

&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;echo &lt;/span&gt;notes.txt | xargs &lt;span class="nb"&gt;cat
&lt;/span&gt;important
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The pipe gave &lt;code&gt;cat&lt;/code&gt; the &lt;em&gt;text&lt;/em&gt; &lt;code&gt;notes.txt&lt;/code&gt;. xargs gave &lt;code&gt;cat&lt;/code&gt; the &lt;em&gt;argument&lt;/em&gt; &lt;code&gt;notes.txt&lt;/code&gt;, so &lt;code&gt;cat&lt;/code&gt; opened the file.&lt;/p&gt;

&lt;p&gt;xargs does three things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Reads text from stdin.&lt;/li&gt;
&lt;li&gt;Splits it into words at spaces, tabs and newlines. Quotes and backslashes are treated specially too.&lt;/li&gt;
&lt;li&gt;Builds &lt;strong&gt;one command line&lt;/strong&gt; from as many of those words as fit, and runs it.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Everything people find confusing about xargs follows from those three steps.&lt;/p&gt;

&lt;h2&gt;
  
  
  Every common surprise, explained by that model
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;It splits on spaces, not lines.&lt;/strong&gt; This is the opening example. &lt;code&gt;meeting notes.txt&lt;/code&gt; is one line but two words. Use NUL separators so names can't be split:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;find &lt;span class="nb"&gt;.&lt;/span&gt; &lt;span class="nt"&gt;-name&lt;/span&gt; &lt;span class="s1"&gt;'meeting*'&lt;/span&gt; &lt;span class="nt"&gt;-print0&lt;/span&gt; | xargs &lt;span class="nt"&gt;-0&lt;/span&gt; &lt;span class="nb"&gt;printf&lt;/span&gt; &lt;span class="s1"&gt;'[%s]\n'&lt;/span&gt;
&lt;span class="o"&gt;[&lt;/span&gt;./meeting notes.txt]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With GNU xargs, &lt;code&gt;-d '\n'&lt;/code&gt; also works when you only have one name per line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; | &lt;span class="nb"&gt;grep &lt;/span&gt;meeting | xargs &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="s1"&gt;'\n'&lt;/span&gt; &lt;span class="nb"&gt;printf&lt;/span&gt; &lt;span class="s1"&gt;'[%s]\n'&lt;/span&gt;
&lt;span class="o"&gt;[&lt;/span&gt;meeting notes.txt]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Quotes are special.&lt;/strong&gt; An apostrophe in a filename stops it with an error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"it's.txt"&lt;/span&gt; | xargs &lt;span class="nb"&gt;echo
&lt;/span&gt;xargs: unmatched single quote&lt;span class="p"&gt;;&lt;/span&gt; by default quotes are special to xargs unless you use the &lt;span class="nt"&gt;-0&lt;/span&gt; option
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;It runs the command once, not once per line.&lt;/strong&gt; This is the most-asked xargs question on Stack Overflow, with over 500 votes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;printf&lt;/span&gt; &lt;span class="s1"&gt;'a\nb\nc\n'&lt;/span&gt; | xargs &lt;span class="nb"&gt;echo &lt;/span&gt;run:
run: a b c

&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;printf&lt;/span&gt; &lt;span class="s1"&gt;'a\nb\nc\n'&lt;/span&gt; | xargs &lt;span class="nt"&gt;-n1&lt;/span&gt; &lt;span class="nb"&gt;echo &lt;/span&gt;run:
run: a
run: b
run: c
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;xargs packs the words into as few command lines as it can. Use &lt;code&gt;-n1&lt;/code&gt; for one argument per run, or &lt;code&gt;-I{}&lt;/code&gt; to place each line exactly where you want it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Empty input still runs the command.&lt;/strong&gt; With GNU xargs, no input means the command runs once with no arguments:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;find &lt;span class="nb"&gt;.&lt;/span&gt; &lt;span class="nt"&gt;-name&lt;/span&gt; &lt;span class="s1"&gt;'*.orig'&lt;/span&gt; | xargs &lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-l&lt;/span&gt;
total 0
&lt;span class="nt"&gt;-rw-r--r--&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt; 1 user user 0 Nov  7 00:06 important.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nothing matched, so &lt;code&gt;ls -l&lt;/code&gt; ran with no arguments and listed the current directory. &lt;code&gt;-r&lt;/code&gt; (&lt;code&gt;--no-run-if-empty&lt;/code&gt;) skips the run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;find &lt;span class="nb"&gt;.&lt;/span&gt; &lt;span class="nt"&gt;-name&lt;/span&gt; &lt;span class="s1"&gt;'*.orig'&lt;/span&gt; | xargs &lt;span class="nt"&gt;-r&lt;/span&gt; &lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-l&lt;/span&gt;
&lt;span class="err"&gt;$&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;&amp;gt;&lt;/code&gt; doesn't go where you think.&lt;/strong&gt; The shell handles redirection before xargs even starts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;printf&lt;/span&gt; &lt;span class="s1"&gt;'x\ny\n'&lt;/span&gt; | xargs &lt;span class="nt"&gt;-I&lt;/span&gt;&lt;span class="o"&gt;{}&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt;.out
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;ls&lt;/span&gt;
&lt;span class="o"&gt;{}&lt;/span&gt;.out
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cat&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt;.out
x
y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That created one file, literally named &lt;code&gt;{}.out&lt;/code&gt;, holding all of xargs's output. To redirect per item, give each run its own shell:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;printf&lt;/span&gt; &lt;span class="s1"&gt;'x\ny\n'&lt;/span&gt; | xargs &lt;span class="nt"&gt;-I&lt;/span&gt;&lt;span class="o"&gt;{}&lt;/span&gt; sh &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s1"&gt;'echo "$1" &amp;gt; "$1.out"'&lt;/span&gt; _ &lt;span class="o"&gt;{}&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;ls
&lt;/span&gt;x.out
y.out
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Two habits that prevent all of this
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;See the command before you run it.&lt;/strong&gt; Put &lt;code&gt;echo&lt;/code&gt; in front of the command for a dry run, as in the opening. Or use &lt;code&gt;-t&lt;/code&gt;, which prints each command to stderr as it runs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;printf&lt;/span&gt; &lt;span class="s1"&gt;'a\nb\n'&lt;/span&gt; | xargs &lt;span class="nt"&gt;-t&lt;/span&gt; &lt;span class="nb"&gt;echo
echo &lt;/span&gt;a b
a b
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Often you don't need xargs.&lt;/strong&gt; If the list comes from &lt;code&gt;find&lt;/code&gt;, &lt;code&gt;-exec ... {} +&lt;/code&gt; batches arguments the same way, and it never re-splits names:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;find &lt;span class="nb"&gt;.&lt;/span&gt; &lt;span class="nt"&gt;-name&lt;/span&gt; &lt;span class="s1"&gt;'meeting*'&lt;/span&gt; &lt;span class="nt"&gt;-exec&lt;/span&gt; &lt;span class="nb"&gt;printf&lt;/span&gt; &lt;span class="s1"&gt;'[%s]\n'&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt; +
&lt;span class="o"&gt;[&lt;/span&gt;./meeting notes.txt]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which of these has bitten you: spaces, empty input or the &lt;code&gt;&amp;gt;&lt;/code&gt;?&lt;/p&gt;

</description>
      <category>linux</category>
      <category>bash</category>
      <category>shell</category>
      <category>productivity</category>
    </item>
    <item>
      <title>`find -delete` before `-name` deletes everything</title>
      <dc:creator>techen spectre</dc:creator>
      <pubDate>Wed, 04 Nov 2026 09:57:02 +0000</pubDate>
      <link>https://dev.to/techen_spectre_6069be436d/find-delete-before-name-deletes-everything-nf3</link>
      <guid>https://dev.to/techen_spectre_6069be436d/find-delete-before-name-deletes-everything-nf3</guid>
      <description>&lt;p&gt;This looks like it deletes one temp file. It deletes the whole directory, including the directory itself:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;demo
&lt;span class="nb"&gt;touch &lt;/span&gt;demo/keep.txt demo/old.tmp

find demo &lt;span class="nt"&gt;-delete&lt;/span&gt; &lt;span class="nt"&gt;-name&lt;/span&gt; &lt;span class="s1"&gt;'*.tmp'&lt;/span&gt;

&lt;span class="nb"&gt;ls &lt;/span&gt;demo
&lt;span class="c"&gt;# ls: cannot access 'demo': No such file or directory&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's GNU findutils 4.10.0. There's no prompt and no warning. &lt;code&gt;keep.txt&lt;/code&gt; is gone.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why it happens
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;find&lt;/code&gt; doesn't treat its arguments as a list of options. It evaluates them as an expression, from left to right, for each file. So the command above means:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Delete this file.&lt;/li&gt;
&lt;li&gt;Then check whether its name matches &lt;code&gt;*.tmp&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By the time &lt;code&gt;-name&lt;/code&gt; runs, the damage is done. &lt;code&gt;-delete&lt;/code&gt; isn't a setting that applies to whatever matched. It's an action that runs at the point where you wrote it.&lt;/p&gt;

&lt;p&gt;The man page says this outright:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Warning: Don't forget that find evaluates the command line as an expression, so putting -delete first will make find try to delete everything below the starting points you specified.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  The safe version
&lt;/h2&gt;

&lt;p&gt;Run it with &lt;code&gt;-print&lt;/code&gt; first, then swap in &lt;code&gt;-delete&lt;/code&gt; as the last thing on the line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;demo
&lt;span class="nb"&gt;touch &lt;/span&gt;demo/keep.txt demo/old.tmp

find demo &lt;span class="nt"&gt;-name&lt;/span&gt; &lt;span class="s1"&gt;'*.tmp'&lt;/span&gt; &lt;span class="nt"&gt;-print&lt;/span&gt;
&lt;span class="c"&gt;# demo/old.tmp&lt;/span&gt;

find demo &lt;span class="nt"&gt;-name&lt;/span&gt; &lt;span class="s1"&gt;'*.tmp'&lt;/span&gt; &lt;span class="nt"&gt;-delete&lt;/span&gt;

&lt;span class="nb"&gt;ls &lt;/span&gt;demo
&lt;span class="c"&gt;# keep.txt&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The same order rule applies to &lt;code&gt;-exec&lt;/code&gt;. Filters go first, and the action goes last.&lt;/p&gt;

&lt;p&gt;What's the worst thing a command with the arguments in the wrong order has done to you?&lt;/p&gt;

</description>
      <category>linux</category>
      <category>bash</category>
      <category>cli</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
