Listing the files in a directory

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Listing the files in a directory
# 1  
Old 11-28-2012
Computer Listing the files in a directory

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:

A script that takes any number of directories as command line arguments and then lists the contents of each of these directories. The script should output each directory name prior to listing the files within that directory

2. Relevant commands, code, scripts, algorithms:

Code:
No of dir = $#
count=1
If [ count -le $# ] then;
echo ${$count}
ls –ltr ${$count}/
count = `expr count + 1`
fi

3. The attempts at a solution (include all code and scripts):

Code:
No of dir = $#
count=1
If [ count -le $# ] then;
echo ${$count}
ls –ltr ${$count}/
count = `expr count + 1`
fi

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

BITS pilani, Hyderabad, India, B.V. Prasad Babu, System Programming

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Last edited by Scrutinizer; 11-28-2012 at 10:40 AM.. Reason: code tags
# 2  
Old 11-28-2012
Thank you for filling out the template.

A few problems:

1) Variables can't have spaces in their names. No of dir = $# is wrong.

2) You shouldn't put spaces on either side of the equal sign. something = asdf is wrong. Do something=asdf

3) You got your if-statement nearly right, but not quite. Don't capitalize anything. Your ; is also in the wrong place.

If it helps, imagine it like this:

Code:
if [ something ]
then
...
fi

To combine any of that onto one line, you'd put ; where there's newlines, so you'd get if [ something ] ; then

4) You can't put a variable in a variable like this: ${$count} it just doesn't expand there. There's a better way, anyway.

The shift command will get rid of the first parameter. If your parameters were a b c d before, after you run shift they will be b c d. So you can loop until $# is zero, running shift at the bottom of your loop, using $1 every time instead of using $1, $2, $3...
# 3  
Old 11-28-2012
I would also add and comment:
Code:
count=1
If [ count -le $# ]

What is the if supposed to do here?
I see you are comparing a string (count) with a numeral since you are using -le How can you expect that to work?
Code:
if [ "$count" -le $# ]   # Here the shell know that the content "should" be an integer 
then                     # since you are using -le ...
    ...

# 4  
Old 11-29-2012
Thnax. I wrote like this.. now it is working
Code:
if [ $# -eq 0 ]
then
  echo "Input needed"
  exit 0;
else
while (($#));
do
    echo "Dir Name ->" $1
    sleep 2
    ls -ltr $1
    shift
   echo "\n"
done
fi

Moderator's Comments:
Mod Comment Please use code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Listing only the files under a directory from the result of find command

Hi, I have a main folder 'home'. Lets say there is a folder 'bin' under 'home'. I want to check the list of files under subdirectories present under the /bin directory created in the last 24 hours. I am using the following find command under home/bin directory: find . -mtime -1 -print ... (3 Replies)
Discussion started by: DJose
3 Replies

2. UNIX for Dummies Questions & Answers

How to find directory listing from root to all files in tree format with details of perm/own/grp?

Hi, My apologies if my query is already available on this forum but I am new and could not find. I need a script to list all directories/sub directories and files with permissions/groups/owners. The script would run from home directory and should capture every directory. How do I do this? ... (4 Replies)
Discussion started by: 8709711
4 Replies

3. UNIX for Dummies Questions & Answers

Listing files in a Unix Directory ending with .....

Hi Unix Gurus, I need to list all files in a Unix Directory which either end with a .pdf or .rtf and they should be case insensitive ie .Pdf , .pDF , .RtF etc are also possible. How can i accomplish this with with a ls command ? If not then a find command. (6 Replies)
Discussion started by: pchegoor
6 Replies

4. Shell Programming and Scripting

Help with listing given files in a given directory path

hello every one, i'm a novice in the field of Linux, so please help me out with this problem. a text file with the following syntax is given: file1 file2 file3 file4 file5 a script is to be written to list all d file names and tar the files with the filename... (3 Replies)
Discussion started by: Amruthesh C
3 Replies

5. Shell Programming and Scripting

Listing files in a given directory with given extention

for some reason my code does not give the right number of files. can omeone help me please? (2 Replies)
Discussion started by: andrew1400
2 Replies

6. Shell Programming and Scripting

Just listing size, timestamp & name of files in a directory

How can I list the files in a directory and just show the file size, date stamp, timestamp and file name.. I've been trying to ls -lrt the directory to a file and then use the cut command but I'm not having any luck with getting the proper results.. I thought i could use the -f switch and count... (4 Replies)
Discussion started by: Jazmania
4 Replies

7. UNIX for Dummies Questions & Answers

Listing files in a non-parent directory

Hi, Edit: The title should really read listing files in a non-parent directory, sorry! Im trying to get one of my Bash scripting assignments done for uni and now I'm stuck. This is probably going to be one of those kick yourself moments but, in my script I have a variable usrDir which... (2 Replies)
Discussion started by: Adzi
2 Replies

8. UNIX for Dummies Questions & Answers

listing files in a directory in bases of size

Hi , I want to list all files in the order of size . Just want to know which files occupies more size and which occupies less size . Is it possible with ls command ? :) Thanks, Arun. (1 Reply)
Discussion started by: arunkumar_mca
1 Replies

9. UNIX for Dummies Questions & Answers

Recursive directory listing without listing files

Does any one know how to get a recursive directory listing in long format (showing owner, group, permission etc) without listing the files contained in the directories. The following command also shows the files but I only want to see the directories. ls -lrtR * (4 Replies)
Discussion started by: psingh
4 Replies

10. UNIX for Dummies Questions & Answers

listing files and directory in Page wise

!hello , Any one can me how can i display files and directory in Pagewise. how to change prompt in UNIX . (1 Reply)
Discussion started by: smdakram
1 Replies
Login or Register to Ask a Question