View previous topic :: View next topic |
Author |
Message |
f.kater Guru
Joined: 23 May 2002 Posts: 342 Location: Berlin
|
Posted: Tue Aug 20, 2002 2:16 pm Post subject: cp: copy dot-files? |
|
|
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 |
|
|
Guest
|
Posted: Tue Aug 20, 2002 2:56 pm Post subject: |
|
|
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
Joined: 23 May 2002 Posts: 342 Location: Berlin
|
Posted: Tue Aug 20, 2002 5:32 pm Post subject: |
|
|
Thank You very much.
This is really not trivial...
Felix |
|
Back to top |
|
|
|
|
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
|
|