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

  • Home
  • SEARCH
  • 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 7083655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:12:17+00:00 2026-05-28T07:12:17+00:00

The below script gives this error: rm: illegal option — 4 rm: illegal option

  • 0

The below script gives this error:

rm: illegal option -- 4
rm: illegal option -- 5
rm: illegal option -- 4
rm: illegal option -- 3
rm: illegal option -- 2

the script:

#!/bin/bash
keep_no=$1+1
cd "/mydirec/"
rm -rf `ls | sort -nr | tail +$keep_no`

I would like the script to accept an argument (of num of direcs to keep) then remove all directories (including their containing files) except for the (number passed in the script – ordering by the numerical direc names in descending order).

ie if /mydirec/ contains these direc names:

53
92
8
152
77

and the script is called like: bash del.sh 2

then /mydirec/ should contains these direcs (as it removes those that aren’t the top 2 in desc order):

152
92

Can someone please help with the syntax?

  • 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-05-28T07:12:18+00:00Added an answer on May 28, 2026 at 7:12 am
    #!/bin/bash
    if [[ -z "$1" ]]; then 
       echo "syntax is..."
       exit 1
    fi
    keep_no=$(( $1 + 1 ))
    cd "/mydirec/"
    IFS='
    ';    # record separator: only enter inside single quotes
    echo rm -rf $(ls | sort -nr | tail +$keep_no)
    

    Verify the output of the script manually, then execute the script through sh:

    ./your_script.sh | sh -x
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've tried the below script but I am getting an error: dim cn, rs
For some reason the script below is not working. This is the code I
whenever I run this below it gives the output but it also gives an
I have the below code in a bash file called 'findError.sh': #!/bin/bash filename=$1 formatindicator=\|\
Given the following html table and script shown below I am having a problem
I have tested the below script on a demo page which is not using
When there is no files inside the folder the below script goes inside the
It's my first time dealing with cookies in JavaScript and the below script works
The script below will replace selected word in a textarea. But it only works
The script below, test.php, is intended to be placed in a specific directory of

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.