Forum Moderators: bakedjake

Message Too Old, No Replies

*******Omit A file while copying..

Omit file while copying a directory

         

SuperNaut

12:25 am on Jun 20, 2002 (gmt 0)



Hi,
I want to write a shell script to copy data from a directory to another directory. And while copying I want to omit a file from getting copied.
Ex:
My source directory is /info/SDATA.
I want to copy all the files in SDATA except for 1 file named "BADFILE.lst" into a destination /archive.
I could use a recursive option like (cp -r) but that would copy even the BADFILE.lst. Please anyone help me with this. Any help would be greatly appreciated.
Thanks,
Sat.

Josk

10:39 am on Jun 20, 2002 (gmt 0)

10+ Year Member



change permissions on the file so it can't be read?

bird

11:07 am on Jun 20, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



a) Create a listing of the source directory, filter out the unwanted name(s), and feed the result to cp (note the backticks around "ls SRCDIR ¦ grep -v '^BADFILE$'"). This solution will only work for a reasonable number of files, as the command line to cp eventually becomes too long:

cp `ls SRCDIR ¦ grep -v '^BADFILE$'` TARGETDIR

b) If you want to make sure you can handle arbitrary numbers of files, but don't care quite as much about raw performance, then you can loop over the files in the source directory; and copy them one by one.

In [t]csh:

foreach f in ( SRCDIR/* )
if ( "${f}" == "BADFILE" ) then
; # do nothing
else
cp SRCDIR/${f} TARGETDIR
endif
end

In [ba]sh:

for f in SRCDIR/*; do
if [ "${f}" == "BADFILE" ] ; then
; # nothing
else
cp SRCDIR/${f} TARGETDIR
fi
done;

Please test on some non-critical data before blaming me for any failures... ;)