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. |
How would this script be different from the ls command?
|
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. |
Quote:
Quote:
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. |
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 |
OK, how about this
Code:
#!/bin/bash |
here's by simply using for loops
Code:
#!/bin/bash |
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. |
Quote:
Quote:
|
All times are GMT -5. The time now is 02:20 AM. |