concatenating the filenames in a directory

hi all,

I have a requirement where in i have to read all the filenames based on a pattern from a directory and concatenate all these file names and write it to another file.

i am using the following code to do this

var1=''
for filename in $_DIR/${FILE_NAME}*
do
if
if [ X${var1} = "X" ]
then
var1=$var1$filename
else
var1=$var1:$filename
fi

done

`echo "$var1"` >> $DIR/$filename_out.txt

its taking really long time to execute this script if there are more number files matching the pattern.

can any one suggest a better appraoch, than above one?

Thanks,
Narendar

pattern="xyz"
ls | grep "$pattern" > file_out.txt

this will add the files matching the patter to the file_out..

but i want them to concatenated using ":" and added.

What the heck does concatenated mean. Provide an example of what you want to do.

I did something similar using the current date (fetching Status pages from my cable modem). Don't know if this will help you:

CUR=`date | sed 's/\ /_/g'`.asp
wget http://192.168.100.1/Status.asp
mv Status.asp $CUR

for the heck...i want the output to be in the form
DIR/filename: DIR/filename2: DIR/filename3: DIR/filename4..so on to be added to the output file.

[space between : and DIR is not required but the webpage was converting it to a smiley :D. so i have kept the space. in original output that space is not required]
where filename[1-4]..files matching the pattern 'filename' in the DIR

as i mentioned i was able to do this..but just wanted to know if there is any better way to do this....

If you don't mind the colon at the end of the line use:

$ dir=test
$ FILE_NAME=html

$ printf "%s:" `ls -p $dir/${FILE_NAME}*|grep -v '/$'` 

$ test/html_1:test/html_2:test/html_3:

removing the last colon:

$ printf "%s:" `ls -p $dir/${FILE_NAME}*|grep -v '/$'` | sed 's/:$//'

Thank you all for the responses!!!

I am using the following query

var1=$( printf ":%s" "$DIR/${FILE_NAME}"* )
printf "%s\n" "${var1#?}" >> "$DIR/$filename_out.txt"

Thanks,
Narendar