Welcome to WebmasterWorld Guest from 54.227.6.156

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)

Inactive Member
Account Expired

 
 


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.
10:39 am on June 20, 2002 (gmt 0)

Preferred Member

10+ Year Member

joined:Oct 4, 2000
posts:446
votes: 0


change permissions on the file so it can't be read?
11:07 am on June 20, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Aug 10, 2001
posts:1551
votes: 10


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... ;)

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members