Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Unix scripting question - Reading directories  RSS feed

 
M Burke
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to read a directory and feed the file names to another process as a parameter. How can I do this in Unix script? I am using the korn shell.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This will visit subdirectories too.
 
Tim Holloway
Saloon Keeper
Posts: 18789
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stefan Wagner:


This will visit subdirectories too.


You might need to tweak it slightly:



Otherwise I think some shells will apply a more immediate and less desirable meaning to the "{}".

Also note that the trailing ";" is not optional!

You can limit the directory depth of the find and do many other amusing things as well.
[ May 31, 2007: Message edited by: Tim Holloway ]
 
M Easter
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The find command is incredibly powerful but note that this will pass files to the script 'one at a time'.

Depending on the situation, it can be as easy as this, say if you just have one directory:

$ ls > fileList.txt
$ my_script.sh fileList.txt
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or more likely

myscript.sh `ls`
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes - or



which would transfer directory-names too, and omit subdirs.


would pass all names together too.
 
M Burke
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I call 'find' from inside the script and use some kind of array variable (or something) to step through the file names?
[ June 04, 2007: Message edited by: M Burke ]
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[ June 05, 2007: Message edited by: Stefan Wagner ]
 
M Burke
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's some hairy looking syntax


Thanks
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by M Burke:
That's some hairy looking syntax

For Array-access?
Yes.
I try to avoid it if I can.

better?
 
M Burke
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it does

I even simplified it further...

list=$(find $OUTPUT_FILE_MASK".sh")
for fileName in $list
do

cp $WORKING_DIR/$fileName $WORKING_DIR/$OUTPUT_SQL_SHELL_PREFIX$fileName

done
 
Tim Holloway
Saloon Keeper
Posts: 18789
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... And yet one other - one that I use frequently to remove junk files from a project:


BTW, In Solaris, there's no implicit location, so it would be 'find . -name "*~"' there.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!