LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 05-17-2010, 02:46 PM   #1
WildDrake!
Member
 
Registered: Dec 2003
Location: Current Location: Colorado
Distribution: Ubuntu 14.04, Mint 16
Posts: 88

Rep: Reputation: 15
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....)
 
Old 05-17-2010, 02:52 PM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,610

Rep: Reputation: 518Reputation: 518Reputation: 518Reputation: 518Reputation: 518Reputation: 518
Try rsync -av src dest
 
Old 05-17-2010, 03:00 PM   #3
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora, Lubuntu, FreeBSD
Posts: 3,930
Blog Entries: 5

Rep: Reputation: Disabled
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'.
 
Old 05-17-2010, 04:04 PM   #4
antegallya
Member
 
Registered: Jun 2008
Location: Belgium
Distribution: Debian
Posts: 109

Rep: Reputation: 42
Or
cp -r * .*
 
Old 05-18-2010, 02:51 AM   #5
chadwick
Member
 
Registered: Apr 2005
Location: At the 100th Meridian where the great plains begin
Distribution: Debian Testing on T60 laptop
Posts: 105

Rep: Reputation: 16
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/

Last edited by chadwick; 05-18-2010 at 03:04 AM.
 
Old 05-18-2010, 06:02 AM   #6
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,036

Rep: Reputation: 372Reputation: 372Reputation: 372Reputation: 372
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 *
 
1 members found this post helpful.
Old 05-18-2010, 02:00 PM   #7
WildDrake!
Member
 
Registered: Dec 2003
Location: Current Location: Colorado
Distribution: Ubuntu 14.04, Mint 16
Posts: 88

Original Poster
Rep: Reputation: 15
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!!!
 
Old 05-18-2010, 03:10 PM   #8
WildDrake!
Member
 
Registered: Dec 2003
Location: Current Location: Colorado
Distribution: Ubuntu 14.04, Mint 16
Posts: 88

Original Poster
Rep: Reputation: 15
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.
 
Old 05-18-2010, 03:23 PM   #9
antegallya
Member
 
Registered: Jun 2008
Location: Belgium
Distribution: Debian
Posts: 109

Rep: Reputation: 42
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
 
Old 05-18-2010, 04:39 PM   #10
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,036

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
List all files in a directory recursively, without the directories? b10m3ch4 Linux - General 4 11-12-2009 05:07 PM
Using chmod to recursively change directories / files [GOD]Anck Linux - Software 6 11-10-2008 06:16 PM
deleting files recursively, but not directories tantan Linux - General 2 03-07-2007 06:01 AM
Copying hidden files recursively? mrd Linux - General 1 09-30-2005 07:51 AM
List all files and recursively open directories. unreal128 Linux - General 2 07-16-2005 02:06 PM


All times are GMT -5. The time now is 06:59 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration