LinuxQuestions.org
Review your favorite Linux distribution.
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 09-22-2010, 07:16 AM   #1
x111
Member
 
Registered: Jul 2010
Distribution: Slackware
Posts: 49

Rep: Reputation: 7
Copy content of two directories


I'm sorry if the answer to this question is easy. But I've been searching the web as well as experimenting it myself for a while without coming up with any solution. I have 2 directories named source and dest. How can I use the 'cp' command to copy all the files and directories inside "source" into "dest".
For example:
In "source" we have: a.jpg, b.jpg, subdir1, subdir2.
In "dest" we have: c.jpg, subdir3.
After copying, in "dest" we have: a.jpg, b.jpg, c.jpg, subdir1, subdir2 (as well as hidden files/directories too). I don't mind if it overwrites the content of "dest" (because sometimes "dest" is an empty directory").

I have tried:
Code:
cp -r source/* dest/*
cp -r source/* dest/
cp -r source/* dest
cp -r source/ dest/*
cp -r source/ dest/
cp -r source/ dest
cp -r source dest/*
cp -r source dest/
cp -r source dest
But with all the above, I've always ended up with either nothing new in "dest", or a subdirectory "source" inside "dest/".
So, is there any way to have 'cp' do the trick, or do we need another tool to do this.

The reason I'm asking is I'm building an customized live debian system, which has root as its default user at every boot time. Because /etc/skel is automatically copied to new normal users only, I have to write a script to do the job for a super user.
Thanks for all your help and suggestion

Last edited by x111; 09-22-2010 at 07:41 AM. Reason: check spelling
 
Old 09-22-2010, 07:43 AM   #2
suprstar
Member
 
Registered: Aug 2010
Location: Atlanta
Distribution: ubuntu, debian
Posts: 142
Blog Entries: 2

Rep: Reputation: 23
cp -r source dest

will work and copy all hidden files. Do not have an empty 'dest' folder to begin with. With:

cp -r dir1/* dir2

bash expands the *, which excludes hidden files.
 
Old 09-22-2010, 08:01 AM   #3
x111
Member
 
Registered: Jul 2010
Distribution: Slackware
Posts: 49

Original Poster
Rep: Reputation: 7
Quote:
Originally Posted by suprstar View Post
cp -r source dest

will work and copy all hidden files. Do not have an empty 'dest' folder to begin with. With:

cp -r dir1/* dir2

bash expands the *, which excludes hidden files.
Thank you superstar, but using 'cp -r source dest' give me "dest/source" (source directory inside dest).
And 'cp -r source/* dest' doesn't copy anything to dest.
 
Old 09-22-2010, 08:05 AM   #4
x111
Member
 
Registered: Jul 2010
Distribution: Slackware
Posts: 49

Original Poster
Rep: Reputation: 7
I'm wondering where I can get the source code of the script which copies the content of /etc/skel -> /home/newly-added-user ?
 
Old 09-22-2010, 08:06 AM   #5
suprstar
Member
 
Registered: Aug 2010
Location: Atlanta
Distribution: ubuntu, debian
Posts: 142
Blog Entries: 2

Rep: Reputation: 23
Quote:
Originally Posted by x111 View Post
Thank you superstar, but using 'cp -r source dest' give me "dest/source" (source directory inside dest).
And 'cp -r source/* dest' doesn't copy anything to dest.
Like I said, DONT have an empty 'dest' directory to begin with. Let cp create dest.
 
1 members found this post helpful.
Old 09-22-2010, 10:13 AM   #6
saagar
Member
 
Registered: Jul 2008
Location: Chennai, India
Distribution: RHEL5, Ubuntu
Posts: 191

Rep: Reputation: 37
Code:
 cp /etc/skel/* /home/<User>
then,

Code:
 cp /etc/skel/.* /home/<User>
It this helping you?
 
1 members found this post helpful.
Old 09-22-2010, 11:36 AM   #7
x111
Member
 
Registered: Jul 2010
Distribution: Slackware
Posts: 49

Original Poster
Rep: Reputation: 7
oh suprstar, I'm sorry I didn't tell you that although the dest directory is empty, I can't remove it and let cp create a new one. It may be a root directory of a partition.

@saagar: Thank you for your reply, the first command doesn't work. It says 'omitting directory...". The second tries to copy all the hidden files but it also copies the parents of the source directory (because ".*" includes "..").
Good news is when I try with just:

cp -r source/. dest

It works like a charm. Thank you all for the support. Now I've learnt a very sharp skill to synchronize 2 directories.
 
  


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
[SOLVED] cannot compare two directories, and print files that have same name and content apanimesh061 Fedora 3 09-17-2010 10:17 AM
How do I copy over directories to directories with the same name without overwriting? SentralOrigin Linux - General 1 03-14-2009 01:09 AM
RHEL4 : Backup directories without content IanSolo666 Linux - Enterprise 4 03-27-2008 02:54 AM
VSFTP - problems uploading directories full of content animehair Linux - Software 1 01-03-2005 08:34 AM
How does one delete directories with content as the SU? Ferrell Ramey Linux - General 2 08-22-2002 07:16 PM

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

All times are GMT -5. The time now is 02:17 AM.

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