Script that displays contents of a directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script that displays contents of a directory
# 1  
Old 01-29-2012
Script that displays contents of a directory

Hello all! I am writing a script that takes in a directory name as input and if the directory exists, it shows the files inside the directory

here is what I have so far (incomplete) (mostly like pseudocode)
Code:
#/bin/sh
echo Please enter the name of a directory
read dir
grep $dir /home/other/xxxxx
if test $? -eq 0
then
cd $dir ls (this is where I get confused, this is basically pseudo code to show what I am trying to do(
else
echo $dir is not a directory
fi

disregard the xxxxx, that is personal information I have erased. What I want it to do is use grep to search the current directory for any "sub directories" and see if they exist. If they do exist then run the cd command to jump to that directory and ls to show the contents. I am having trouble incorporating it into the script. Any help is appreciated. . thank you
# 2  
Old 01-29-2012
grep matches strings inside files, not names of directories inside other directories.

You don't have to do
Code:
something
if [ $? -eq 0 ]
then

by the way, you can just do
Code:
if something
then
        echo something succeeded
else
        echo something failed
fi

...less chance of mixing up the value of $? that way.

You could just check whether the directory exists with [ -d ]:

Code:
read DIR
if [ -d /home/xxx/"$DIR" ]
then
        echo "$DIR exists in xxx"
        ls /home/xxx/"$DIR"
else
        echo "$DIR does not exist"
fi

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 01-29-2012
Similar to Corona688, but retaining and revising the question.

Code:
#/bin/sh
echo "Please enter the name of a subdirectory under /home/other"
read dir
if [ -d "/home/other/${dir}" ]
then
      ls -la "/home/other/${dir}"
else
      echo "Directory does not exist: /home/other/${dir}"
fi

See "man test" for the explanation of "if ... -d".

Last edited by methyl; 01-29-2012 at 06:45 PM.. Reason: mispaste and rewrite
This User Gave Thanks to methyl For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script Output Displays Multiple Text

Hello there, I'm using a read-while loop to preserve the word Failed within a text file. For example, if the word Failed exist twice in a single text file, my STDOUT should re-direct to a new text file and display Failed twice. My output is attached to this thread. I would like output to... (4 Replies)
Discussion started by: SysAdminRialto
4 Replies

2. Shell Programming and Scripting

Shell script to find and replace contents of files in directory

Hi all This is my first post. Please bear with me with all my mistakes. I started learning shell since couple of days now and this might be quite basic for all, i want to search for files in a directory containing specific string and replace it with new string. The code i wrote is quite bulky... (2 Replies)
Discussion started by: theprogrammer
2 Replies

3. UNIX for Dummies Questions & Answers

script works well but displays " line 6: =: No such file or directory"

strange :) can you tell why?:cool: #!/bin/bash echo " enter your age " read age if ; then echo " you do not have to pay tax " elif ]; then echo " you are eligible for income tax " else echo " you dont have to pay tax " fi (3 Replies)
Discussion started by: me.
3 Replies

4. Shell Programming and Scripting

script to delete contents in a directory by date

Hi , I am trying to make a cron job to delete the contents of a directory in a linux environment.The contents created before 2 days should get deleted by this job.Here is what i have written : /usr/bin/find / -name *.log -ctime +2 -exec /bin/rm -f {} \; Is it correct....If not so,please... (9 Replies)
Discussion started by: d8011
9 Replies

5. Shell Programming and Scripting

Remove contents of directory, but not directory

What's the best way to delete everything in a directory, but not the directory itself, without using shell wildcards? (9 Replies)
Discussion started by: pdc
9 Replies

6. Shell Programming and Scripting

Regular Expression on Directory Contents

This should be an easy question for you gurus. :) How can I create a regular expression to match all files in the current directory that have only one period in their file name, and also not contain the string "abc" before the period? It would match: foo.txt foobar.log It would not... (4 Replies)
Discussion started by: blondie53403
4 Replies

7. Solaris

Directory should not be deleted, But the contents can be

Hi Guys, I have an user's home directory set to /home/A And A contains the following directories B & C Is there some way in solaris by which i can prevent the directories B and C from getting deleted by the user but the contents of the directories B & C can be deleted ? Also i have... (2 Replies)
Discussion started by: localhost
2 Replies

8. UNIX for Dummies Questions & Answers

Best way to list a directory's contents?

Hey guys! I'm so glad I found this site, I've had so many questions and have been left alone for roughly a year scanning man pages but It's just not quite cutting it for some things. So, I often like to list directories when browsing around my local machine, a friend's machine, or my web... (6 Replies)
Discussion started by: bbilheimer
6 Replies

9. UNIX for Dummies Questions & Answers

list contents of directory

I want to list the contents of a directory, but I do not want to use the ls, is there another way?? (3 Replies)
Discussion started by: carl_vieyra
3 Replies

10. UNIX for Dummies Questions & Answers

pine email tool displays directory listing

I just moved my mail server (qmail in maildir format) from a solaris box to a RedHat linux box. When I select some inboxes in pine, I get a directory listing of the inbox, its not automatically opening the qmail cur directory. This doesn't happen with all, and it doesn't happen at all on... (0 Replies)
Discussion started by: cross
0 Replies
Login or Register to Ask a Question