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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:35:04+00:00 2026-05-30T23:35:04+00:00

I want to list all directories using a shell script. I am using the

  • 0

I want to list all directories using a shell script. I am using the following code:

DIR="$1"

if [ $# -ne 1 ]
then
    echo "Usage: $0 {dir-name}"
exit 1
fi

cd "$DIR"
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for user in $( ls -d */)
do
  for dirs in $( ls -d $PWD/$user*)
  do
    echo $PWD/$user/$dirs;
  done
done
IFS=$SAVEIFS

It is working for me if that directory don’t have any spaces on it, else it split the output for every spaces on it. I got the following output:

abhinaba@abhinaba-desktop:~/software$ sh test.sh /media/2C44138344134F48/RB1
ls: cannot access /m: No such file or directory
ls: cannot access dia/2C44138344134F48/RB1/VB*: No such file or directory
ls: cannot access /m: No such file or directory
ls: cannot access dia/2C44138344134F48/RB1/DLI*: No such file or directory
ls: cannot access /m: No such file or directory
ls: cannot access dia/2C44138344134F48/RB1/3001/*: No such file or directory
ls: cannot access /m: No such file or directory
ls: cannot access dia/2C44138344134F48/RB1/VB*: No such file or directory
ls: cannot access /m: No such file or directory
ls: cannot access dia/2C44138344134F48/RB1/DLI*: No such file or directory
ls: cannot access /m: No such file or directory
ls: cannot access dia/2C44138344134F48/RB1/3002/*: No such file or directory
ls: cannot access /m: No such file or directory
ls: cannot access dia/2C44138344134F48/RB1/VB*: No such file or directory
ls: cannot access /m: No such file or directory
ls: cannot access dia/2C44138344134F48/RB1/DLI*: No such file or directory
ls: cannot access /m: No such file or directory
ls: cannot access dia/2C44138344134F48/RB1/3003/*: No such file or directory
  • 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-30T23:35:05+00:00Added an answer on May 30, 2026 at 11:35 pm

    EDITED: removed ls to optimize the script (from tripleee)

    If you use BASH shell:

    #!/bin/bash
    
    SAVEIFS=$IFS
    IFS=$(echo -en "\n\b")
    DIR="$1"
    
    if [ $# -ne 1 ]
     then
     echo "Usage: $0 {dir-name}"
     exit 1
    fi
    
    cd "$DIR"
    
    for user in $(ls -d */)
     do
     for dirs in $(ls -d $user*)
      do
      echo $dirs
     done
    done
    
    IFS=$SAVEIFS
    

    Run the above “test.sh” script like this:

    bash test.sh /media/2C44138344134F48/RB1
    

    or simply (if you are already in BASH and you have set the eXecutable flag)

    ./test.sh /media/2C44138344134F48/RB1
    

    Reference

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

Sidebar

Related Questions

I want to list all files on an FTP server using PHP. According to
In a Git code repository I want to list all commits that contain a
How to list all available LookAndFeel themes? I want to show then in a
I simply want to list all of the directories under my current working directory,
In Python, I want to list all files in a set of directories. The
I want to write a bash script which will use a list of all
I want to list all the directories that start with . in the current
I want to list all directories ls -d * in the current directory and
I want to list all virtual directories that belong to a web site with
I want to list all users with their corropsonding user class. Here are simplified

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.