ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
#!/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
Last edited by konsolebox; 04-02-2008 at 08:12 PM.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.