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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:07:53+00:00 2026-06-08T17:07:53+00:00

Below is a snippet from a larger script that exports a list of the

  • 0

Below is a snippet from a larger script that exports a list of the subdirectories of a user-specified directory, and prompts the user before making directories with the same names in another user-specified directory.

COPY_DIR=${1:-/}
DEST_DIR=${2}
export DIRS="`ls --hide="*.*" -m ${COPY_DIR}`"
export DIRS="`echo $DIRS | sed "s/\,//g"`"
if [ \( -z "${DIRS}" -a "${1}" != "/" \) ]; then 
  echo -e "Error: Invalid Input: No Subdirectories To Output\n"&&exit
elif [ -z "${DEST_DIR}" ]; then 
  echo "${DIRS}"&&exit
else
  echo "${DIRS}"
  read -p "Create these subdirectories in ${DEST_DIR}?" ANS
  if [ ${ANS} = "n|no|N|No|NO|nO" ]; then
    exit
  elif [ ${ANS} = "y|ye|yes|Y|Ye|Yes|YE|YES|yES|yeS|yEs|YeS" ]; then
    if [ ${COPYDIR} = ${DEST_DIR} ]; then
      echo "Error: Invalid Target: Source and Destination are the same"&&exit
    fi
    cd "${DEST_DIR}"
    mkdir ${DIRS}
  else 
    exit
  fi
fi

However, the command ls --hide="*.*" -m ${COPY_DIR} also prints files in the list as well. Is there any way to reword this command so that it only prints out directories? I tried ls -d, but that doesn’t work, either.
Any ideas?

  • 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-08T17:07:55+00:00Added an answer on June 8, 2026 at 5:07 pm

    You should never rely on the output of ls to provide filenames. See the following for reasons not to parse ls: http://mywiki.wooledge.org/ParsingLs

    You can build a list of directories safely using GNU find’s -print0 option and appending the results to an array.

    dirs=() # create an empty array
    while read -r -d $'\0' dir; do # read up to the next \0 and store the value in "dir"
       dirs+=("$dir") # append the value in "dir" to the array
    done < <(find "$COPY_DIR" -type d -maxdepth 1 -mindepth 1 ! -name '*.*') # find directories that do not match *.*
    

    The -mindepth 1 prevents find from matching the $COPY_DIR itself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below is the snippet of a shell script from a larger script. It removes
I have the following snippet below from my script that's using a WebRequest to
The below is a snippet from VB that I am porting to a C#
With the below snippet I want to append/prepend a string to a list that
Validation in Spring 3.x using @Valid annotation: Below is snippet from the messages_en.properties. I
I am very new to asp classic application. The below code snippet is from
Below is a code snippet from a book. Why can serialNumber still be set
In the code snippet below, from my jQuery setup, I need to check if
I am using the below code snippet to FTP simple text files from a
Using the snippet below, I've attempted to extract the text data from this PDF

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.