How do I copy file named starting with a dot?
The reason is because in bash
, *
does not include files starting with dot (.
).
You can run
cp A/.* B
It will warn you that it did not copy .
or ..
, or any subdirectories, but this is fine.
Or, if you want to copy dot files and normal files together, run
cp A/.* A/* B
You could also run
shopt -s dotglob
cp A/* B
which will work in bash
, but not sh
.
And if you don't mind subdirectories being copied too, then this is the easiest:
cp -R A/ B
Tip: If ever wildcards aren't doing what you expect, try running it with echo, e.g.
$ echo A/*
A/file1 A/file2
$ echo A/.*
A/. A/.. A/.hidden1 A/.hidden2
$ echo A/.* A/*
A/. A/.. A/.hidden1 A/.hidden2 A/file1 A/file2
$ shopt -s dotglob
$ echo A/*
A/file1 A/file2 A/.hidden1 A/.hidden2
If bash, you can set dotglob
before you copy
shopt -s dotglob
cp A/* /destination
Or a programming language
$ ruby -rfileutils -e 'Dir[".*"].each {|x| FileUtils.copy(x,"/destination") if File.file?x}'
If you don't want to set dotglob, just
cp A/.* /destination 2>/dev/null
What you're looking for is more along the lines of:
cp A/.??* B/
This will match all dotfiles, but not "." or "..". Most of the above solutions are fine as long as you're not working recursively. But as soon as you want to do something like:
cp -R A/.??* B/
Without omitting ".." you'll copy everything from the parent directory on down, including non-dotfiles.