aspose file tools*
The moose likes Linux / UNIX and the fly likes Shell Script For Getting List Of File name In A directory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Shell Script For Getting List Of File name In A directory" Watch "Shell Script For Getting List Of File name In A directory" New topic
Author

Shell Script For Getting List Of File name In A directory

kayanaat sidiqui
Ranch Hand

Joined: Sep 04, 2008
Posts: 122
Hi,

Can any body please tell me the how to write Shell Script For Getting List Of File name In A directory;

Same In DOS also.

Thanks,
Kaya.
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5843
    
    7

Maybe I am missing something here, but couldn't you just do:

ls /some/directory

and in DOS:

dir c:/some/directory

Why do you need a script?


JBoss In Action
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

you might need to do

ls -a /some/directory

to get the hidden files.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874

kayanaat,

try this . dint check it myself..

saptadip sarkar
Greenhorn

Joined: Jul 06, 2008
Posts: 6
Doing an ls under any directory will give you the list of all files(use -a to get the hidden files details), but the output will include all the details of the files like "permission", "owner of the file", "time stamp" etc....so if you want the name of the files to be displayed ONLY(may be you like to pass the file names as parameter to some function or script), try this:

ls -a <directory> | awk {'print $9'}

Note: The numeric value 9 denotes the column number which denotes the file name.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


but the output will include all the details of the files like "permission", "owner of the file", "time stamp" etc....


Thats false, unless specifies -l option.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Balu Sadhasivam wrote:kayanaat,

try this . dint check it myself..



what is the benefit, compared to


or even


btw - *.* will mask dotfiles.

http://home.arcor.de/hirnstrom/bewerbung
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874



what is the benefit


ls -a would just display files... whereas the script is more flexible and inside loop can do any operations with filenames.. requirements dont stop there..would you write a script to just display files or make it compatible to changes that come in future..

Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Balu Sadhasivam wrote:

what is the benefit


ls -a would just display files...

which mets the requirements


whereas the script is more flexible and inside loop can do any operations with filenames..

no, you have to fix it by hand. If we like to delete the files, you will replace your echo with an rm, and I will replace ls -a with rm.


requirements dont stop there..would you write a script to just display files or make it compatible to changes that come in future..

Which changes that come in future? I don't see what script can do, which I can't do with ls -a - planning for an unforeseable future needs far more information than given in this post.

YAGNI, KISS
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


whereas the script is more flexible and inside loop can do any operations with filenames..

no, you have to fix it by hand. If we like to delete the files, you will replace your echo with an rm, and I will replace ls -a with rm.


rm is just simple operation. What if some consecutive operations , use of | would not be efficient in such cases.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

@Balu Sadhasivam:

You 're right . If the description of the problem isn't appropriate, the solution of the problem I postet isn't appropriate. Absolutely right.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


Steve , Both were right .. its just matter of other parameters.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Shell Script For Getting List Of File name In A directory