LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 11-27-2005, 07:20 PM   #1
Akhran
Member
 
Registered: Aug 2005
Distribution: Debian 'lenny'
Posts: 208

Rep: Reputation: 30
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'

Seems that I can't copy hidden files with cp -r.

Please advise.

Thanks.
 
Old 11-27-2005, 07:44 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
You're encountering a problem with the globbing here ;}

You can do 2 different things:

a) Don't mkdir target first, then cp -r test/ target/
b) cd test; cp -r ./.* ../target


Cheers,
Tink
 
Old 11-27-2005, 07:53 PM   #3
Sargek
Member
 
Registered: Jan 2003
Location: San Antonio, Texas
Distribution: Debian testing
Posts: 416

Rep: Reputation: 36
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...
 
Old 11-27-2005, 08:00 PM   #4
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
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.

Last edited by Robhogg; 11-27-2005 at 08:08 PM.
 
2 members found this post helpful.
Old 11-27-2005, 08:15 PM   #5
Sargek
Member
 
Registered: Jan 2003
Location: San Antonio, Texas
Distribution: Debian testing
Posts: 416

Rep: Reputation: 36
Quote:
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".
 
Old 11-27-2005, 08:59 PM   #6
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
Quote:
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.

Rob
 
Old 11-28-2005, 06:26 AM   #7
Akhran
Member
 
Registered: Aug 2005
Distribution: Debian 'lenny'
Posts: 208

Original Poster
Rep: Reputation: 30
Thanks for all the tips

Btw, what is globbing?

Thanks !


Quote:
Originally posted by Tinkster
You're encountering a problem with the globbing here ;}

You can do 2 different things:

a) Don't mkdir target first, then cp -r test/ target/
b) cd test; cp -r ./.* ../target


Cheers,
Tink
 
Old 02-01-2008, 07:19 PM   #8
bob84123
LQ Newbie
 
Registered: Feb 2008
Posts: 1

Rep: Reputation: 0
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.)
 
Old 04-17-2008, 01:11 AM   #9
bobpaul
LQ Newbie
 
Registered: Aug 2005
Posts: 14

Rep: Reputation: 0
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 View Post
(At least, this is my understanding... please correct me if it's not quite right.)
Yes, that is correct.
 
Old 10-05-2008, 02:48 AM   #10
geaplanet
LQ Newbie
 
Registered: Oct 2008
Posts: 1

Rep: Reputation: 0
For that situations is useful
cp -a source destiny
 
Old 10-05-2008, 03:03 AM   #11
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
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.
 
1 members found this post helpful.
Old 07-08-2009, 05:25 AM   #12
gatoatigrado
LQ Newbie
 
Registered: Aug 2007
Location: Berkeley, CA
Posts: 4

Rep: Reputation: 1
one issue is that bash actually passes the filename '.??*' or '.[a-zA-Z0-9]*' to the program if no files exist. Does anyone know a solution?

Thanks,
Nicholas
 
Old 07-08-2009, 11:20 AM   #13
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Use the shell option failglob:

Code:
$ echo .[a-zA-Z0-9]*
.[a-zA-Z0-9]*
$ shopt -s failglob 
$ echo .[a-zA-Z0-9]*
bash: no match: .[a-zA-Z0-9]*
 
Old 03-10-2011, 09:17 AM   #14
s_pradeep
LQ Newbie
 
Registered: Mar 2011
Posts: 1

Rep: Reputation: 0
Smile

Code:
ls -1a | xargs -r -t /home/somewhere/target
where /home/somewhere/target is the target dir and you are running this from inside the source dir!! :-)
 
0 members found this post helpful.
Old 05-11-2011, 07:08 AM   #15
pixelatedpete
LQ Newbie
 
Registered: May 2011
Posts: 1

Rep: Reputation: 0
Lightbulb Tar

You can also use tar to do this:

cd to the source directory and then do this:

Code:
tar cf - . | (cd TARGET_DIR; tar xfp -)
 
0 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy files from multiple directories into one directory MadRabbit Linux - Newbie 8 02-07-2014 07:56 PM
copy 3 files to a directory and all subdirectories? snip128 Linux - Newbie 9 08-12-2005 09:18 PM
copy files from directory to directory without subfile ALInux Linux - General 2 06-03-2005 11:51 AM
home directory files gone, hidden files remain Grasshopper Linux - Security 12 04-10-2005 08:23 PM
Hundreds of hidden files in home directory doobers Linux - Newbie 3 02-14-2005 02:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration