How do you copy hidden files from one directory to another?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Will result in ".a" file in source/ to be copied in target/.a for example.
This way, all files will be copied, including hidden ones, and this syntax does not depend on the type of shell you use and the configuration of it.
But doing this with the recursive flag will cause cp to recurse up directories in some circumstances, especially if the mv command is used. My solution is more correct because it makes the glob operator (*) work as expected for scripts and other commands also.
debian:/home# mkdir test
debian:/home# mkdir target
debian:/home# cd test
debian:/home/test# touch .file1 .file2
debian:/home/test# cd ..
debian:/home# cp -r test/.* target
cp: will not create hard link `target/test' to directory `target/.'
cp: cannot copy a directory, `test/..', into itself, `target'
cp: cannot copy a directory, `test/..', into itself, `target'
cp: cannot copy a directory, `test/..', into itself, `target'
cp: cannot copy a directory, `test/..', into itself, `target'
Seems that I can't copy hidden files with cp -r.
Please advise.
Thanks.
The correct glob is
Code:
cp -r test/.[!.]* target
A dot then a non-dot than anything.
This includes file names like .a that would not work with .??* as was suggested by Mr. C.
A glob is the wildcard/expression in the shell.
A [a-z0-9] character range works in a glob and in an RE(=regular expression), but otherwise they are much different.
Last edited by MadeInGermany; 12-13-2019 at 03:34 PM.
I normally use rsync for that, using the directory WITH the hidden files as source, but with a trailing / (to not copy the directory itself).
Code:
rsync -a source-dir/ destination-dir
will make a complete copy of the contents of the source directory into the destination directory, including both hidden files and subdirectories.
Optional flags for rsync (see the man page too):
Code:
-v verbose (shows files as they're copied)
-H copy hard links AS hard-links, if the "other link" is in the copied tree too
-n dry-run, only show what would have been copied
So often you use a "rsync -avn" command first, just to see if it does what you want.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.