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.
How do you copy hidden files from one directory to another?
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'
Re: How do you copy hidden files from one directory to another?
Quote:
Originally posted by Akhran 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 -r switch for cp means to copy directories recursively. Also, there a couple of "dot" file in each directory that cp is trying to copy. Try this:
Code:
cp /home/user/test/.f* /home/user/target
Not a bash expert - there may be a shorter way, but that guarentees the correct source and destination, while ignoring the "dot" files. Have you checked your test dir lately Mine was populated by the the contents of my home dir...
Are all the items you are trying to copy normal files, or are there directories you need to copy as well?
I've just tried out what you describe. I found that cp without the -r flag works fine, but cp -r return the error messages you give. When I looked into the target directory, however, I found that many files that were not in the original directory had been copied. These were files from the parent directory.
The problem is that every directory on a unix-type file system contain two special directories:
"." refers to the directory itself
".." refers to the parent directory
Thus, one of the directories you were trying to copy was the directory itself, and then the command will recursively copy everything from the directory above it in the file tree.
A solution is a bit of pattern matching. Try:
cp -r test/.[a-zA-Z0-9]* target
This will copy any file that begins with a dot followed by any lower- or upper-case letter or number, then followed by anything else.
Originally posted by Robhogg Are all the items you are trying to copy normal files, or are there directories you need to copy as well?
I've just tried out what you describe. I found that cp without the -r flag works fine, but cp -r return the error messages you give. When I looked into the target directory, however, I found that many files that were not in the original directory had been copied. These were files from the parent directory.
The problem is that every directory on a unix-type file system contain two special directories:
"." refers to the directory itself
".." refers to the parent directory
Thus, one of the directories you were trying to copy was the directory itself, and then the command will recursively copy everything from the directory above it in the file tree.
A solution is a bit of pattern matching. Try:
cp -r test/.[a-zA-Z0-9]* target
This will copy any file that begins with a dot followed by any lower- or upper-case letter or number, then followed by anything else.
He doesn't want to copy directories, only files, so the -r should not be necessary - nice trick with the regular expression! I use them with Javascript, silly me didn't even realize I could use them in bash... Your code only worked when I entered the full path to both test and target - otherwise it failed because it couldn't find "target".
Originally posted by Sargek Your code only worked when I entered the full path to both test and target - otherwise it failed because it couldn't find "target".
I didn't have any problem using just the shorter relative filepaths. Of course, if you use the full path, the command will work from anywhere in the system rather than just the parent directory of target and test.
I had the same problem; it turns out you *can* copy hidden files with cp -r. It's actually as simple as:
Code:
cp -r dir1 dir2
The difference between this and
Code:
cp -r dir1/* dir2
is that in the latter, bash will expand the star (which excludes hidden files) before cp even sees the command, so cp has no way of knowing that you wanted them copied.
(At least, this is my understanding... please correct me if it's not quite right.)
That works, but will cause problems in cases where you can't create the directory. Take this for instance...
mkdir /mnt/Storage
mount /dev/sda1 /mnt/Storage
cp ~/* /mnt/Storage
I can't really substitute let it create a folder in /mnt and rename it, as a file system is mounted in Storage. I NEED to copy the contents of the folder only... Tip 4 shows what to do.
Quote:
Originally Posted by bob84123
(At least, this is my understanding... please correct me if it's not quite right.)
I've always used a simple, easy-to-type glob for copying all dot files:
cp .??* destdir
This works - almost perfectly - because ? does not match a dot, so avoids the . and .. directories. I say almost perfectly because it fails to copy any dot file followed by only a single letter (eg. .a), but in practice, such a name for a dot file is extremely rare.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.