Copying excluding some files and folders

I have a main folder.
Inside that i have many(50) subfolders.
In each subfolder, there are a no of large files(500 files ) present.
I want to do copy operation for some files from some of the subfolders to /usr/tmp.
I have the list of the subfolders and list of of files which i dont want to copy.
Could you please let me know how to do it without using rsync. it would not hamper the performance as file size are large.I m using solaris .Thank you in advance.

You could either copy everything and then delete what you don't want (probably rather wasteful) or you could use a find command. Something like:-

cd sourcedir

find . -type d | grep -vf exclude-file | while read dir
do
   echo "Creating directory based on $dir"
   mkdir targetdir/$dir
done

find . -type f | grep -vf exclude-file | while read file
do
   echo "Copying $file"
   cp $file targetdir
done

Your exclude file would need to be in the right format, e.g.

^./not_this_file
^./not_this_directory
not_a_file_or_directory_ending_this$
not_a_file_or_directory_with_this_anywhere

etc.

If you are running AIX (and perhaps a few others) it allows the -L flag for the tar command. You can use find to create your list of files that you do want, then:-

cd sourcedir
tar -cvf - -L filelist | ( cd targetdir ; tar -xvf - )

Similar processes could be done with cpio, and perhaps backup/restore; dump/restore; ufsdump/ufsrestore etc. as appropriate to your OS.

Robin

That's an unsafe grep. If the exclude file is a list of pathnames and not regular expressions, you must use fixed-string whole-line matches, otherwise a literal character could be interpreted as a regular expression metacharacter, and/or a pathname can match a substring in a longer pathname.

A possible alternative solution:

find source -type d | grep -vxFf exclude-file | pax -rw destination

It would be simplest if absolute paths are used in the file and with find.

Regards,
Alister

2 Likes

I am working on solaris.
Here thre is no -f option for grep.
Is there any alternative i can use for solaris?

---------- Post updated at 02:58 AM ---------- Previous update was at 01:51 AM ----------

Thank you guys..it worked

i tried with /usr/xpg4/bin/grep

---------- Post updated at 11:24 PM ---------- Previous update was at 02:58 AM ----------

Hi Robin,

i was trying this in Solaris but i am getting below error as "do unexpected".
Could you pls let me know where this code went wrong.

 
$ cat test1.sh
find /usr/tmp/SB/reports -type f -name *.rdf | /usr/xpg4/bin/grep -F -v -f /usr/tmp/SB1/exclude.txt |read filename
do
        echo "Copying $filename to /usr/tmp/SB1"
        cp $filename /usr/tmp/SB1
done

 
$ sh test1.sh
test1.sh: syntax error at line 3: `do' unexpected

If i run below...it is giving me correct filename.

 
find /usr/tmp/SB/reports -type f -name *.rdf | /usr/xpg4/bin/grep -F -v -f /usr/tmp/SB1/exclude.txt 

To start the loop, you need a while read statement. You just have read which will grab the output in one go and results can be a little odd.

Robin