LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   recursively cp all directories, files and hidden files (http://www.linuxquestions.org/questions/linux-newbie-8/recursively-cp-all-directories-files-and-hidden-files-808403/)

WildDrake! 05-17-2010 02:46 PM

recursively cp all directories, files and hidden files
 
Ubuntu 10.04

I want to copy all directories, files, and hidden files and hidden directories with one command. I want these items to replace any same items in the target directory.

I have tried several things, such as:

cp -r *
cp -aR *

but I only seem to get visible files and directories. Obviously, I am missing something. (A brain, probably....)

AlucardZero 05-17-2010 02:52 PM

Try rsync -av src dest

anomie 05-17-2010 03:00 PM

The problem with what you've tried so far is your shell expansion (*) is not picking up hidden files. Expanding a different way (.*) would.

Using rsync is a good approach. Another one is tar. For example:
Code:

$ cd foo
$ tar cf - . | tar -C /path/to/bar -x

That would copy all files (including hidden) from 'foo' to 'bar'.

antegallya 05-17-2010 04:04 PM

Or
cp -r * .*

chadwick 05-18-2010 02:51 AM

Wow, I can see how that could have caught lots of people off guard, thinking they'd copied all files but hadn't, and not noticing for quite some time. Good thing you noticed. In fact, thanks for reminding me to watch out for that.

If you specify a directory that you want to copy, then it includes everything below the directory that you're copying. For example:
Code:

cp -a from-directory/ to-directory/
would copy the entire from-directory/ and everything below it. If you just want the stuff below from-directory/ and not from-directory/ itself, then you could cd to from-directory/ and use the . to refer to the directory you're in:

Code:

cd from-directory/
cp -a . to-directory/

Or to avoid the cd, you could do:

Code:

cp -a from-directory/. to-directory/

i92guboj 05-18-2010 06:02 AM

By default bash doesn't include dot files in *, however the feature could be easily turned on with this:

Code:

shopt -s dotglob
Then the command would work as expected with just a simple *

WildDrake! 05-18-2010 02:00 PM

Thanks everybody! I will experiment with all these solutions and pick one to use in my script, but I am going to add all of them to my notes!!!

WildDrake! 05-18-2010 03:10 PM

Does shopt -s dotglob need to be turned off after you have used it? And if so, how do you do that? I have no man or info pages on shopt or dotglob

Thanks.

antegallya 05-18-2010 03:23 PM

shopt is a shell builtin, you can get documentation in bash's man page, in the "SHELL BUILTIN COMMANDS" section.
Options controlled by shopt are local to the shell where it is set (and inherited to subshells, if not told not to do so).
If you want to unset the option after using it, use
Code:

shopt -u dotglob

i92guboj 05-18-2010 04:39 PM

antegallya summed it all up nicely. It's a bash builtin, so it's the bash man page which you should be looking at. -s sets an option, -u unsets it, shopt alone will list the current state of things. If you want you can use the bash initialization files (also described in the man page) to set or unset some of these options automatically each time you open a new shell.

A given option is only valid on the shell you set it on and it is so until you either close that given shell or you unset the option explicitly using shopt -u.


All times are GMT -5. The time now is 11:10 PM.