how to copy the directory but not copy certain file

Hi experts

 
cp bin root src /mnt
 

but not copy bin/bigfile

any help?
( I post this thread in the "redhat" forum wrongly, I don't know how to withdraw that question in that wrong forum)

Thanks

If your find supports the wholename option, and if your cp supports both --parent and -t then this might do the trick. It works for a small directory that I tested with. (These are two big if's)

find bin root src -type f ! -wholename bin/bigfile | xargs  cp --parent -t /mnt

It will not copy bigfile only from the bin directory; if it exists in other directories it should be copied.

If you want to test this to see what would be copied, make the following changes which will show the copy command for each source file. The list will be one copy per file to make it easy to read even though the command above will not run one copy per file, but will invoke the cp command once for multiple files.

find bin root src -type f ! -wholename bin/bigfile | xargs -n 1 echo cp --parent -t /mnt

The output will be something like:

cp --parent -t  /mnt  bin/foo

The -t specifies the target directory allowing it to be placed at the head of the list making invocation with xargs more efficient, but showing the command this way might be confusing since the normal parameter arrangement is source followed by destination.

Another way to do it would be to use tar. This is my 'go-to' solution for such things. I'm unsure as to whether the --exclude option is recognised by all/most versions of tar. If not you can use -X filename, but that requires creating filename and populating it with a list of files to exclude. Not difficult, but not as simple as the example below:

tar -cf - bin src root --exclude=bin/bigfile | (cd /mnt; tar -xf )

Alternative with -X

echo "bin/bigfile" >/tmp/exlist.$$
tar -cf - bin src root -X /tmp/exlist.$$ | (cd /mnt; tar -xf )
rm /tmp/exlist.$$

Hope these get you going.

1 Like

fast solution:

cp bin root src /mnt; rm /mnt/bin/bigfile

serious: assuming you have bash with "extended pattern matching operators" try (replacing ls with cp):

ls /root /src /bin/!(bigfile)

Hi the bigfile is a big file I don't want to copy it to /mnt and then delete it because the /mnt has no big space

OK, what about the second suggestion? Are you using bash?

yes the bash, the second suggestion changed the directory structure

Is it only files directly under ./bin ./root ./src that need to be copied, or do you need to copy the whole directory trees (excluding bigfile)?

If as I suspect you want the whole directory tree excluding bigfile you could try rsync:

rsync -ax --exclude=bin/bigfile bin root src /mnt

You can also use --max-size=300K with rsync to skip any file bigger than 300KB