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 7887989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:43:20+00:00 2026-06-03T05:43:20+00:00

I have a task that requires me to delete all folders in a directory

  • 0

I have a task that requires me to delete all folders in a directory that aren’t symlinked, keeping only the symlinked folders and the symlinks.

If I have for instance 3 resources: link, linked_folder and unlinked folder. link and linked_folder will be kept.

Currently I have managed to get this code, which works for the file naming structure we have, but I’d like to know if there is a cleaner solution?

protected_folders=`ls -l | awk '/^l/ {printf $10}' | tr '/' '|'`;
symlinks=`ls -l | awk '/^l/ {print $8}' | tr "\n" "|"`;
protected_resources=$protected_folders$symlinks;
protected_resources=${protected_resources:0:${#protected_resources}-1};
folders_to_delete=`ls | grep -v -E "$protected_resources"`; 
echo $folders_to_delete | tr '\n' ' ' | tr ' ' '\000' | xargs -0 rm -rf

I know the above command can be done on one line, but I’ve separated it as I’ve been requested to have it more “readable”.

Any help would be appreciated.

  • 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-03T05:43:21+00:00Added an answer on June 3, 2026 at 5:43 am

    It might be easier to move the symlinks and their targets to a new directory, then replace the old directory with the new.

    WARNING: this is just a rough sketch, to give you the idea. [UPDATE: clean up and make it less like pseudo-code]

    # Make an empty directory to hold the symlinks and targets
    # that you want to keep
    DIR_TO_CLEAN=/the/directory/to/clean/up
    TMP_DIR=$(mktemp -d)
    
    for x in $DIR_TO_CLEAN/*; do
        # Move each symlink and its target to the new directory
        [[ -h $x ]] && mv $x $(readlink -f $x) $TMP_DIR
    done
    
    # FIRST!!!, confirm that $TMP_DIR has everything you want to keep,
    # and $DIR_TO_CLEAN has nothing you want to keep
    rm -rf $DIR_TO_CLEAN
    mv $TMP_DIR $DIR_TO_CLEAN
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a long task that requires 10 / 15 seconds. I want to
Hi and thanks for looking! Background I have a computing task that requires either
I have a task that requires me to print out the following quantities: I
I have a somewhat complex plot task in matplotlib that requires--I think--an autoscale() function
I have a task in hand that requires me to send a form to
I have a task that takes a rather long time and should run in
I have a task that's driving me crazy because i have no clue where
Just stumbled upon propertyGrid and its awesome! However, i have one task that i
I have an MSBuild task that executes (among other things) a call to xcopy.
I have a scheduled task that is very IO intensive (deleting hundreds of thousands

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.