Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
cp: copy dot-files?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo
View previous topic :: View next topic  
Author Message
f.kater
Guru
Guru


Joined: 23 May 2002
Posts: 342
Location: Berlin

PostPosted: Tue Aug 20, 2002 2:16 pm    Post subject: cp: copy dot-files? Reply with quote

Hi,
how can I ensure that a copy (with cp) also copies all files and directories beginnig with a dot?
Thank You
Felix
Back to top
View user's profile Send private message
Guest






PostPosted: Tue Aug 20, 2002 2:56 pm    Post subject: Reply with quote

If you're doing a recursive copy of a directory, the dot files in that directory and its descendants will also be copied. I tend to use cp -a olddir newdir to do this.

If you just want to copy a bunch of files to another directory, e.g. cp * ../otherdir, but want to copy the dot files too, it's just a case of constructing shell glob patterns that match dot files, but don't match the special directory entries '.' and '..'.


  • * matches everything not starting with a dot.
  • ..?* matches everything starting with two dots except '..' itself.
  • .[^.]* matches everything starting with a dot followed by not-dot.


So if you do cp * ..?* .[^.]* ../otherdir all files matching the above patterns will be copied. However, if one or more of the patterns do not match any filenames, the cp command will output an error to standard error (e.g. cp: cannot stat `..?*': No such file or directory) and will exit with a non-zero status code.

In bash you can change how shell globbing patterns are expanded. If you run (shopt -s dotglob; cp * ../otherdir) then the '*' will also match the dot files (but not '.' and '..') during the copy. (The parentheses run the commands in a new shell to avoid changing the options in the current shell, but you could just run shopt -s dotglob in the current shell to allow * to match dot files, and run shopt -u dotglob to return to the default behavior of * not matching dot files.)
Back to top
f.kater
Guru
Guru


Joined: 23 May 2002
Posts: 342
Location: Berlin

PostPosted: Tue Aug 20, 2002 5:32 pm    Post subject: Reply with quote

Thank You very much.

This is really not trivial...

Felix
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum