LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Script to list files in a given directory (https://www.linuxquestions.org/questions/programming-9/script-to-list-files-in-a-given-directory-632103/)

Azzath 04-01-2008 03:32 AM

Script to list files in a given directory
 
Hi All,
Could someone help me in writing a small script which will display the files in a given directory?

So I should be able to give a directory path (as an argument or any sort) and then be able to see all files within that Dir. If I dont give a path then the script should list the contents of the current directory.

I kind of got the pseudo, it is something like this
1. Request for a directory path
2. Take the directory path and assign to a variable eg: $1
3. Check if $1 is valid i.e if a path is given by the user
4. If $1 is a valid path then list the contents
4. Else if $1 is not valid, print the list in the current directory

Any help is appreciated.

KenJackson 04-01-2008 07:28 AM

How would this script be different from the ls command?

Azzath 04-01-2008 07:47 AM

Hi Ken,
I know that “ls” would list the contents of a dir. but I want a script to do as described in my original post. It's not just about learning this one script for me, but also a learning cure how to pass an argument and also using a control statement in the following script to achieve this goal.

Any advice from anyone would be appreciated.

Anyway thanks for your concern Ken.

unSpawn 04-01-2008 07:49 AM

Quote:

Originally Posted by KenJackson (Post 3107034)
How would this script be different from the ls command?

Because it doesn't print $CWD if you supply a nonexistent directoryname?


Quote:

Originally Posted by Azzath (Post 3106826)
I am new to bash scripting. Could someone help me in writing a small script which will display the files in a given directory?

Make this your homework:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html, http://www.tldp.org/LDP/Bash-Beginne...tml/index.html (and maybe some of the http://www.tldp.org/LDP/abs/html/) and work on using:
- checking "$#" (or using "getopts"),
- "test -d",
- if else (and maybe case) statements.

prad77 04-02-2008 03:02 PM

echo "Dir Name?"
read dir
if [ -d $dir ]; then
ls $dir
else
echo invalid dir name
ls .
fi


Your script can go like this.
Ofcourse You will have to debug and tune it to your need from there.

Gentoo

KenJackson 04-02-2008 04:30 PM

OK, how about this
Code:

#!/bin/bash
test -d "$1" && ls "$1" || ls


konsolebox 04-02-2008 08:10 PM

here's by simply using for loops
Code:

#!/bin/bash

usage() {
        echo $0 [directory]
        exit 1
}

if [ -n "$1" ]; then
        if [ "$1" == "--help" ] || [ "$1" == "-h" ]; then
                usage
        fi
        if ! [ -d "$1" ]; then
                echo "directory $1 not found"
                exit 1
        fi

        DIR=${1}
        DIR=${DIR%\/}
        for ANY in ${DIR}/*; do
                echo "${ANY}"
        done
else
        for ANY in *; do
                echo "${ANY}"
        done
fi

# note: it seems that ${DIR}* (e.g. DIR=abc/) won't work


Azzath 04-03-2008 02:18 AM

Hey Thanks guys..
Ken you are simply listing what ever is in the current directory, in that case anyone can simply use an "ls" like you said (correct me if I’m wrong).

prad77, yours is a simple and quick one (very handy), I can improvise on that..thanks buddy.

konsolebox, great stuff. Still need to play around with your code :) thanks to you too.

KenJackson 04-03-2008 07:02 AM

Quote:

Originally Posted by Azzath (Post 3109093)
Ken you are simply listing what ever is in the current directory, in that case anyone can simply use an "ls" like you said (correct me if I’m wrong).

The difference is that if an invalid directory is specified with the "ls" command, an error message is displayed with no listing. Whereas my little one-line script implements your fourth requirement:
Quote:

4. Else if $1 is not valid, print the list in the current directory


All times are GMT -5. The time now is 02:20 AM.