LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 10-05-2011, 12:45 AM   #16
gnif
LQ Newbie
 
Registered: Oct 2011
Posts: 4

Rep: Reputation: Disabled

Or you could just turn the option on in bash so that * matches dotfiles.

shopt -s dotglob
 
Old 12-28-2011, 09:25 AM   #17
jrdasm
LQ Newbie
 
Registered: Jan 2011
Posts: 6

Rep: Reputation: 0
Easier way:
Code:
cp -r source/. target/
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.
 
Old 12-28-2011, 04:34 PM   #18
gnif
LQ Newbie
 
Registered: Oct 2011
Posts: 4

Rep: Reputation: Disabled
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.
 
Old 12-29-2011, 03:11 AM   #19
jrdasm
LQ Newbie
 
Registered: Jan 2011
Posts: 6

Rep: Reputation: 0
Quote:
will cause cp to recurse up directories in some circumstances
No it doesn't
Quote:
especially if the mv command is used
The question is about "cp".
 
Old 12-29-2011, 03:31 AM   #20
gnif
LQ Newbie
 
Registered: Oct 2011
Posts: 4

Rep: Reputation: Disabled
Yes, sorry, I was thinking of when a for loop is used and the shell expands the . and .. paths.

And yes, I know this questions is about cp, but you will get some people that try the same trick with other commands such as mv.
 
Old 12-11-2019, 01:18 AM   #21
Telo
LQ Newbie
 
Registered: Jun 2017
Distribution: Debian
Posts: 11

Rep: Reputation: Disabled
Use zsh. It ignores . and ..

So you can easily (and I might add memorably), recursively with -a (or not without it) copy JUST the hidden files from one folder to another in bash:

Code:
$ zsh
...# cp -a folder1/.* folder2/.
^D
$
Without the fear that the -a will try to recurse into the . and ..
 
Old 12-13-2019, 03:33 PM   #22
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,794

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Quote:
Originally Posted by Akhran View Post
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.
 
1 members found this post helpful.
Old 12-13-2019, 05:20 PM   #23
ehartman
Senior Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 1,674

Rep: Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888
Quote:
Originally Posted by Akhran View Post
Seems that I can't copy hidden files with cp -r.
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.
 
  


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 11:15 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