Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you're after a succinct command, then with <a href="http://www.linuxjournal.com/content/bash-extended-globbing" rel="nofollow">extended globbing in bash</a>, you should be able to use:</p> <pre><code>rm !(abc.txt) </code></pre> <p>There are however several caveats to this approach.</p> <ol> <li><p>This will run <code>rm</code> on all entries in the directory (apart from "abc.txt") and this includes subdirectories. You will therefore end up with the "cannot remove directory" error if subdirs exist. If this is the case, use <code>find</code> instead:</p> <pre><code>find . -maxdepth 1 -type f \! -name "abc.txt" -exec rm {} \; # omit -maxdepth 1 if you also want to delete files within subdirectories. </code></pre></li> <li><p>If <code>!(abc.txt)</code> returns a very long list of files, you will potentially get the infamous <a href="http://www.linuxjournal.com/article/6060" rel="nofollow">"argument list too long" error</a>. Again, <code>find</code> would be the solution to this issue.</p></li> <li><p><code>rm !(abc.txt)</code> will fail if the directory is empty or if abc.txt is the only file. Example:</p> <pre><code>[me@home]$ ls abc.txt [me@home]$ rm !(abc.txt) rm: cannot remove `!(abc.txt)': No such file or directory </code></pre> <p>You can <a href="http://mywiki.wooledge.org/NullGlob" rel="nofollow">workaround this using nullglob</a>, but it can often be cleaner to simply use <code>find</code>. To illustrate, a possible workaround would be:</p> <pre><code>shopt -s nullglob F=(!(abc.txt)); if [ ${#F[*]} -gt 0 ]; then rm !(abc.txt); fi # not pretty </code></pre></li> </ol>
    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. 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