• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Shell Script For Getting List Of File name In A directory

 
kayanaat sidiqui
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you might need to do

ls -a /some/directory

to get the hidden files.
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kayanaat,

try this . dint check it myself..

 
saptadip sarkar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Balu Sadhasivam wrote:kayanaat,

try this . dint check it myself..



what is the benefit, compared to


or even


btw - *.* will mask dotfiles.
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Steve , Both were right .. its just matter of other parameters.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic