PDA

View Full Version : Unix copy directory but skip files




kainjow
Sep 26, 2006, 01:11 PM
Anyone know of a unix command to copy a directory, but skip certain files? Kind of like the -x argument for zip. I looked at man cp and man ditto but didn't see any such option. Basically I'm writing a Bash script that automates copying a directory out of a Subversion repository, and I want to strip all the *.svn* files from it.



bousozoku
Sep 26, 2006, 01:19 PM
Use awk or grep to filter them and use the filtered output to copy the rest.

lexfuzo
Sep 26, 2006, 01:56 PM
$ man rsync | grep exclude
o exclude and exclude-from options similar to GNU tar
o a CVS exclude mode for ignoring the same files that CVS would
rsync -avuzb --exclude ’*~’ samba:samba/ .
--delete-excluded also delete excluded files on receiver
-C, --cvs-exclude auto-ignore files in the same way CVS does
--exclude=PATTERN exclude files matching PATTERN
--exclude-from=FILE read exclude patterns from FILE
--include=PATTERN don’t exclude files matching PATTERN

...

balamw
Sep 26, 2006, 02:02 PM
find would also be pretty easy to make this work.

something like cp `find . -not -name "*.svn"` destination/ (not tested)

You could add in xargs if there are lots of files in the source directory...

B

savar
Sep 26, 2006, 04:57 PM
Anyone know of a unix command to copy a directory, but skip certain files? Kind of like the -x argument for zip. I looked at man cp and man ditto but didn't see any such option. Basically I'm writing a Bash script that automates copying a directory out of a Subversion repository, and I want to strip all the *.svn* files from it.

find ./ -type d | xargs -i cp {} dest_dir

What do I win?

Oh wait, you only want to skip *some* files, not all of them. Okay:

find ./ ! -name '*.svn' | xargs -i cp {} dest_dir

kainjow
Sep 26, 2006, 07:30 PM
Thanks guys, plenty of solutions.

I ended up just removing the files after I copied them, because I'm lazy :p

find directory -name "*.svn*" -exec rm -rf {} \;

balamw
Sep 26, 2006, 10:42 PM
Thanks guys, plenty of solutions.
So many ways to skin a cat....

B

mrkenzie
Nov 24, 2006, 06:13 PM
I too am trying to copy files over and exclude certain files/directories. When I use the following code:

find ./ ! -name '*.svn' | xargs -i cp {} dest_dir

it works except if it needs to copy a new empty directory. So basically I am trying to synchronize to directories and it is working except for the case where there is an empty directory.

Anyone know how to get it to work including with empty directories?

Thanks.

balamw
Nov 24, 2006, 06:19 PM
I too am trying to copy files over and exclude certain files/directories. When I use the following code:

find ./ ! -name '*.svn' | xargs -i cp {} dest_dir

it works except if it needs to copy a new empty directory. So basically I am trying to synchronize to directories and it is working except for the case where there is an empty directory.

Anyone know how to get it to work including with empty directories?

Thanks.
I think it's a limitation of cp. You could try and just do it in two steps. Copy the directory structure with a find ./ -type d ... |mkdir first?

Or, use a tool better suited towards syncing, like rsync.

B