Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8538173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:05:04+00:00 2026-06-11T11:05:04+00:00

I need to remove all the files in the current directory except one file,

  • 0

I need to remove all the files in the current directory except one file, say abc.txt. Is there any command to rm all the other files in the directory except abc.txt?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:05:06+00:00Added an answer on June 11, 2026 at 11:05 am

    If you’re after a succinct command, then with extended globbing in bash, you should be able to use:

    rm !(abc.txt)
    

    There are however several caveats to this approach.

    1. This will run rm 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 find instead:

      find . -maxdepth 1 -type f \! -name "abc.txt" -exec rm {} \;
      # omit -maxdepth 1 if you also want to delete files within subdirectories.
      
    2. If !(abc.txt) returns a very long list of files, you will potentially get the infamous “argument list too long” error. Again, find would be the solution to this issue.

    3. rm !(abc.txt) will fail if the directory is empty or if abc.txt is the only file. Example:

      [me@home]$ ls
      abc.txt
      [me@home]$ rm !(abc.txt)
      rm: cannot remove `!(abc.txt)': No such file or directory
      

      You can workaround this using nullglob, but it can often be cleaner to simply use find. To illustrate, a possible workaround would be:

      shopt -s nullglob
      F=(!(abc.txt)); if [ ${#F[*]} -gt 0 ]; then rm !(abc.txt); fi  # not pretty
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

File: /home/USER/DIR/a http://www.here.is.a.hyper.link.net/ /home/USER/DIR/b http://www.here.is.another.hyper.link.net/ Need to remove all the odd lines in this
I need to include all xml files inside a directory (I dont know the
I need to remove all classes of 'no-right-marg' on all #tips li elements. I
I need to remove all entries in a dictionary accordingly to a specified lower
I have a problem where I need to remove all code and triggers from
So I have these lines of javascript code that I need to remove all
In my application, _collection is a List from which I need to remove all
I'm adding dynamically labels to hbox, and i need to remove all spaces between
I have a large list [[1,.., ..],[2,...,...],[5,...,...],[1,...,...]] I need to remove all elements that
I need a way to remove ALL using statements from code and fully qualify

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.