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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:22:10+00:00 2026-06-09T18:22:10+00:00

My assignment is to write a Unix shell script that asks the user for

  • 0

My assignment is to write a Unix shell script that asks the user for the name of a directory, and then works exactly like find.

Here is what I have so far:

#!/bin/bash
dir_lister()
{
    cd "$1"
    echo "$1"
    list=$(ls -l ${1})


    nolines=$(echo "$list" | awk 'END{printf "%d",NF}')

    if [ $nolines -eq 2 ]
    then

        echo "$1"

        return
    fi

    filelist=$(echo "$list" | grep ^-.*)
    dirlist=$(echo "$list" | grep ^d.*)

    filename=$(echo "$filelist"| awk '{printf "%s\n",$NF}')
    present=$(pwd)
    echo "$filename"| awk -v pres=$present '{printf "%s/%s\n",pres,$0}'

    dirlist2=$(echo "$dirlist" | awk '{printf "%s\n",$NF}')

    echo "$dirlist2" | while IFS= read -r line;
    do
        nextCall=$(echo "$present/$line");
        dir_lister $nextCall;
        cd ".."
    done
    cd ".."
}



read -p "Enter the name of the direcotry: " dName

dir_lister $dName 

The problem is, after a depth of three directories, this script gets into an infinite loop, and I don’t see why.

EDIT:

Here is the code i came up with after looking at your answer, it still doesn’t go more than 1 directory depth:

#!/bin/bash

shopt -s dotglob # don't miss "hidden files"
shopt -s nullglob # don't fail on empty directories


list_directory()
{
    cd "$2"
    cd "$1"
    ##echo -e "I am called \t $1 \t $2"
    for fileName in "$1/"* 
    do
        ##echo -e "hello \t $fileName"
        if [ -d "$fileName" ];
        then
            echo "$fileName"
            list_directory $fileName $2

        else
            echo "$fileName"
        fi
    done

}
read -p "Enter the direcotory Name: " dirName
var=$(pwd)
list_directory $dirName $var
  • 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-09T18:22:12+00:00Added an answer on June 9, 2026 at 6:22 pm

    Okay, that is completely the wrong way to list files in a directory (see ParsingLs). I’ll give you the pieces and you should be able to put them together into a working script.

    Put this at the top of your script:

    shopt -s dotglob # don't miss "hidden files"
    shopt -s nullglob # don't fail on empty directories
    

    Then you can easily loop over directory contents with:

    for file in "$directory/"* ; do
       #...
    done
    

    Test if you have a directory:

    if [ -d "$file" ] ; then
       # "$file" is a directory, recurse...
    fi
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose that the purpose of an assignment is to write a compiler that works
My class assignment is to write a program that has the user input a
My assignment is to write an overloaded version of iquote() , a method that
I have an assignment that wants me to write an ternary search algorithm and
My Java assignment is to to write a Java application that uses data from
Background: my assignment was to write a program that uses linked list to keep
I am writing my own unix shell as a part of my assignment and
This is the assignment: Write a method that sorts the elements of a matrix
Here is the assignment verbatim: 1.) Write a Java program that is a TCP
One of my assignments in to write my own UNIX Shell. To receive input

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.