LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-01-2008, 03:32 AM   #1
Azzath
Member
 
Registered: Jan 2005
Location: Russia
Distribution: Mandrake, CentOS
Posts: 138

Rep: Reputation: 16
Question 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.

Last edited by Azzath; 04-01-2008 at 01:42 PM.
 
Old 04-01-2008, 07:28 AM   #2
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora and others
Posts: 757

Rep: Reputation: 145Reputation: 145
How would this script be different from the ls command?
 
Old 04-01-2008, 07:47 AM   #3
Azzath
Member
 
Registered: Jan 2005
Location: Russia
Distribution: Mandrake, CentOS
Posts: 138

Original Poster
Rep: Reputation: 16
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.

Last edited by Azzath; 04-01-2008 at 01:42 PM.
 
Old 04-01-2008, 07:49 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by KenJackson View Post
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 View Post
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.
 
Old 04-02-2008, 03:02 PM   #5
prad77
Member
 
Registered: Mar 2008
Posts: 101

Rep: Reputation: 15
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

Last edited by prad77; 04-17-2008 at 03:37 AM.
 
Old 04-02-2008, 04:30 PM   #6
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora and others
Posts: 757

Rep: Reputation: 145Reputation: 145
OK, how about this
Code:
#!/bin/bash
test -d "$1" && ls "$1" || ls
 
Old 04-02-2008, 08:10 PM   #7
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
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

Last edited by konsolebox; 04-02-2008 at 08:12 PM.
 
Old 04-03-2008, 02:18 AM   #8
Azzath
Member
 
Registered: Jan 2005
Location: Russia
Distribution: Mandrake, CentOS
Posts: 138

Original Poster
Rep: Reputation: 16
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.
 
Old 04-03-2008, 07:02 AM   #9
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora and others
Posts: 757

Rep: Reputation: 145Reputation: 145
Quote:
Originally Posted by Azzath View Post
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ List Files In Directory bendeco13 Programming 8 11-02-2010 12:08 PM
how to list how many files in a directory? malaka56 Linux - Software 8 09-02-2005 05:37 AM
How to list all the files in a directory Linh Programming 2 05-11-2004 10:09 AM
Missing List.* files in URPMI directory Ronw Linux - General 2 10-08-2003 03:57 AM
list only files in current directory xscousr Programming 8 09-22-2003 07:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration