Is there an easy way to recursively copy all hidden files in a directory to another directory? I would like to back up just all the settings files in a home directory, not the normal files. I tried:
cp -R .* directory
but it recognizes .
and ..
and recursively copies all the non-hidden files too. Is there a way to get cp to ignore .
and ..
?
Almost every time this can be solved just with:
It's pretty unusual to have a hidden file that doesn't start with one of those characters.
Other pattern matches are available (
.??*
,.[^.]*
) - see the commentsMy favorite to move dirs in general has been:
which tars up the current directory to stdout then pipes it to a subshell that first cd's to the destination directory before untarring stdin. Simple, direct, extensible - consider what happens when you replace the () with an ssh to another machine. Or to answer your question you might do:
You could use
rsync
.that will copy the contents of the current directory (including dot files, but not including
..
)I implore you, step away from plain shell expansion on the
cp
command line - shell expansion has all sorts of ahem "interesting" corner cases (unwanted recursion caused by . and .., spaces, non-printable stuff, hardlinks, symbolic links, and so on.) Usefind
instead (it comes in thefindutils
package, in case you don't have it installed - which would be weird, all distributions install it by default):Step by step explanation:
So, in plain English, this
find
command line says this:I've always used .??* to find hidden files without getting "." and "..". It might miss ".a" or something, though, but I never have one of those.
Much better answers here; https://superuser.com/questions/61611/how-to-copy-with-cp-to-include-hidden-files-and-hidden-directories-and-their-con
It describes for example using shopt for a native bash solution