Unix copy directory but skip files

Discussion in 'Mac Programming' started by kainjow, Sep 26, 2006.

  1. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    Messages:
    7,743
    #1
    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.
     
  2. Moderator emeritus

    Joined:
    Jun 25, 2002
    Messages:
    13,951
    Location:
    Gone but not forgotten.
    #2
    Use awk or grep to filter them and use the filtered output to copy the rest.
     
  3. macrumors 6502

    Joined:
    Mar 15, 2005
    Messages:
    262
    Location:
    The heart of Europe
    #3
    rsync

    $ 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

    ...
     
  4. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Messages:
    17,827
    Location:
    New England
    #4
    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
     
  5. macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Messages:
    1,954
    Location:
    District of Columbia
    #5
    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
     
  6. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    Messages:
    7,743
    #6
    Thanks guys, plenty of solutions.

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

    Code:
    find directory -name "*.svn*" -exec rm -rf {} \;
     
  7. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Messages:
    17,827
    Location:
    New England
    #7
    So many ways to skin a cat....

    B
     
  8. macrumors newbie

    Joined:
    Nov 24, 2006
    Messages:
    1
    #8
    I too am trying to copy files over and exclude certain files/directories. When I use the following code:

    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.
     
  9. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Messages:
    17,827
    Location:
    New England
    #9
    I think it's a limitation of cp. You could try and just do it in two steps. Copy the directory structure with a
    Code:
    find ./ -type d ... |mkdir
    first?

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

    B
     

Share This Page