Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>cat dirs.txt | while read dir; do ls -al "$dir" done </code></pre> <ol> <li><p>When you use the backtick operator the output is split into tokens at each whitespace character. <code>read</code>, on the other hand, will read one line at a time rather than one word at a time. So the solution, which looks kind of odd, is to pipe the file into the loop so it can be read line by line.</p></li> <li><p>You need to quote <code>"$dir"</code> in the <code>ls</code> command so that the entire file name, whitespace and all, is treated as a single argument. If you don't do that <code>ls</code> will see two file names, <code>/var/tmp/old</code> and <code>files.txt</code>.</p></li> </ol> <p>I'm probably going to get awarded the <a href="http://www.partmaps.org/era/unix/award.html" rel="noreferrer">Useless Use of Cat Award</a>. You can simplify this even further by removing the <code>cat</code>:</p> <pre><code>while read dir; do ls -al "$dir" done &lt; dirs.txt </code></pre> <p>This works because the <strong>entire</strong> <code>while</code> loop acts like one big command, so just like you can pipe <code>cat</code> into it, you can also use file redirection.</p> <hr> <h2>Taking it even further...</h2> <p>Depending on how <code>dirs.txt</code> is generated you may just be able to get rid of it entirely and do everything in one command. If it's just a temporary file you could eliminate it by piping the command generates <code>dirs.txt</code> into the <code>while</code> loop directly, skipping the temporary file. For instance, replace</p> <pre><code>find /var -name '*old*' &gt; dirs.txt while read dir; do ls -al "$dir" done &lt; dirs.txt </code></pre> <p>with</p> <pre><code>find /var -name '*old*' | while read dir; do ls -al "$dir" done </code></pre> <p>Pretend <code>find</code> is whatever command you're doing to generate the file list.</p> <p>And actually, if you are in fact using <code>find</code> you can probably do everything in one big <code>find</code> command without any loops or anything! For example, the above code using <code>find</code> and <code>while</code> could be done with a single <code>find</code> command like this:</p> <pre><code>find /var -name '*old*' -exec ls -al {} \; </code></pre> <p><code>find</code> is a really flexible command that can both search for files matching all kinds of complicated criteria, and pass those files as command-line arguments to other commands using the <code>-exec</code> option. When you use <code>-exec</code> the <code>{}</code> gets replaced with each file name.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload