• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Shell Script For Getting List Of File name In A directory

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 4686
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you might need to do

ls -a /some/directory

to get the hidden files.
 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
kayanaat,

try this . dint check it myself..

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve , Both were right .. its just matter of other parameters.
 
The City calls upon her steadfast protectors. Now for a tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic