Copy all dotfiles except for `.git` and `..`

7,876

Solution 1

You can use the the extended globbing in bash:

shopt -s extglob
ls .!(.|git)

This also matches ., though, so you probably need

ls .!(|.|git)

Solution 2

You can use find :

find . -type f '!' -iname ".git" -exec cp -rv {} /dest/path  \;

It will search all files in current directory and but not include .git as we used ! -iname ( where ! means not equal to) then it will copy file to destination location

Update

find . -not -path '.' -not -path './.git' -iname '.*'

also we can use -iregex in find

find . -not -iregex '.\|./.git' -iname '.*'

both example will refer to all dotfiles except for .. and .git in current path

Share:
7,876
dotancohen
Author by

dotancohen

Updated on September 18, 2022

Comments

  • dotancohen
    dotancohen almost 2 years

    I am aware of using .[!.]* to refer to all dotfiles in a directory with the exception of .., but how might one refer to all dotfiles except for .. and .git? I have tried several variations on .[!.||.git]* and .[!.][!.git]* and the like, but none refer to the intended files.

  • Rahul Patil
    Rahul Patil almost 11 years
  • choroba
    choroba almost 11 years
    @RahulPatil: So what?
  • Rahul Patil
    Rahul Patil almost 11 years
    have you tested with that cp to exclude only .. and .git as OP wants
  • dotancohen
    dotancohen almost 11 years
    Thanks. This does not work, and it took me a long time to figure out why. It turns out that -iname relates only to the name of the file, not to any of the names of directories in the files' paths.
  • choroba
    choroba almost 11 years
    @RahulPatil: Have you tested with the second pattern which excludes .?
  • choroba
    choroba almost 11 years
    @dotancohen: Seems very strange. It does something else.
  • Rahul Patil
    Rahul Patil almost 11 years
    I thought .git was file, and I've also tested with .git as file..