Cat files listed in text file and redirect to new directory with same filename

I have a directory that is restricted and I cannot just copy the files need, but I can cat them and redirect them to a new directory. The files all have the date listed in them. If I perform a long listing and grep for the date (150620) I can redirect that output to a text file. Now I need to take that file and, using a script, cat all of the files listed and redirect the output to a temp directory with the same filename.

I've tried several thing but keep getting errors. I've VERY new to scripting and any help would be appreciated.

Hi

You probably want:

cat "thisfile" > "/tmp/newfile"

or

grep 150620 "$thisfile" > "/tmp/newfile"

Note that > will overwrite newfile with the the content of thisfle .
To append thisfile to newfile, use >> .

hth

I realized the problem I was creating. I forgot about all of the extra items in the long listing. I figured it out, of course after I posted for help.

This worked for me.

For I in $(ls /sourcedir | grep string)
Do
Cat /sourcedir /$i > targetdir/$i
Done

This didnt work for you:
1) there are cap-chars.
2) you issue 'I' /$I) but are using 'i' ($i) (cap/noncap chars too)

I'd be very surprised that you'd be denied to cp files but allowed to cat them, as both are just reading operations that need read permission. Are you sure you didn't try sth. different?

In addition to the capitization errors already mentioned, having a space in the middle of a filenames guaranteed to give you the wrong results.

The command cat /sourcedir /$i tries to copy the contents of a directory ( /sourcedir ) and the contents of a (probably non-existent) file in the root directory ( /$i ). That is an illegal operation on a directory on many systems. I doubt that this worked for you (even if you used cat instead of Cat ; but it would have created empty files in the destinations you wanted.

If I understand what you're trying to do (and I'm not at all sure that I do), the following might be a better approach:

for i in /sourcedir/*string*
do	cp "$i" "targetdir"
done

or maybe just:

cp /sourcedir/*string* "targetdir"

if the files to be moved don't overflow argument length limits.