Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How can I make cp -r copy absolutely all of the files and directories in a directory

Requirements:

  • Include hidden files and hidden directories.
  • Be one single command with an flag to include the above.
  • Not need to rely on pattern matching at all.

My ugly, but working, hack is:

cp -r /etc/skel/* /home/user
cp -r /etc/skel/.[^.]* /home/user

How can I do this all in one command without the pattern matching? What flag do I need to use?

share|improve this question

8 Answers 8

up vote 83 down vote accepted

Don't specify the files:

cp -r /etc/skel /home/user

(Note that /home/user must not exist already, or else it will create /home/user/skel.)

share|improve this answer
1  
Perfect! Thanks! –  eleven81 Oct 27 '09 at 20:00
19  
Is it possible to use something similar if /home/user/skel does exist? –  bradley.ayers Aug 24 '11 at 2:10
    
@bradley.ayers I think one could copy into a temporary subdirectory then move them to the upper level (since moving in the same drive is fast). Less than ideal, but shorter than other solutions to me. –  Halil Özgür Mar 16 '13 at 9:58
4  
@bradley.ayers Bruno's answer below addresses your question –  Mark Aug 20 '13 at 15:38

I came here having Googled for a solution to the same problem, then I realized that it's easy to do with find. The advantage it doesn't depend on the shell, or special utilities that may not be installed.

find /etc/skel/ -mindepth 1 -exec cp -r {} /home/username/ \;

I tried the trick with trailing slash, but that didn't work for me.

share|improve this answer

Note that there is a command-line trick (works in, at least, sh, bash, and ksh): Just suffix the from directory with a slash. This will pour the contents of the from directory into the to directory (ironically, I had first learned about this trick when using rsync).

Example:

/tmp$ mkdir test_dir1
/tmp$ cd test_dir1/
/tmp/test_dir1$ touch aa
/tmp/test_dir1$ touch .bb
/tmp/test_dir1$ cd ..
/tmp$ mkdir test_dir2

/tmp$ cp -r test_dir1/* test_dir2
/tmp$ ls -1a test_dir2
.
..
aa

/tmp$ cp -r test_dir1/ test_dir2
/tmp$ ls -1a test_dir2
.
..
.bb
aa
share|improve this answer

rsync is good, but another choice:

cp -a src/ dst/

From the main help:

   -a, --archive
          same as -dR --preserve=all

   -d     same as --no-dereference --preserve=links

   -R, -r, --recursive
          copy directories recursively
share|improve this answer

Use rsync:

rsync -rtv source_folder/ destination_folder/

share|improve this answer

If your source and target directory have the same name, even if target directory exists, you can simply type:

cp -R /etc/skel /home/

This will copy the /etc/skel directory into /home/, including hidden files and directories.

Eventually, you can copy the directory and rename it in a single line :

cp -R /etc/skel /home/ && mv /home/skel /home/user
share|improve this answer
    
Or you could simply use cp -r /etc/skel /home/user for renaming skel to user... –  David Sep 13 '13 at 12:46
    
That's right, only if /home/user does not exist yet. –  Gabriel Hautclocq Oct 14 '13 at 11:52

bash itself has a good solution, it has a shell option, You can cp, mv and so on.:

shopt -s dotglob # for considering dot files (turn on dot files)

and

shopt -u dotglob # for don't considering dot files (turn off dot files)

above solution standards of bash

NOTE:

shopt # without argument show status of all shell options
-u # abbrivation of unset 
-s # abbrivation of set
share|improve this answer
    
That's usefull when you want to copy just content without creating new directory inside destination. Especially when destination dir is mount point. –  kaszynek Nov 11 '13 at 12:27
2  
This really is the best answer and gets to the heart of the question.. –  Stephen May 23 '14 at 16:51
1  
It's setopt for zsh, in case anyone else is wondering. –  Pat Dec 29 '14 at 23:18

Lets say you created the new folder (or are going to create one) and want to copy the files to it after the folder is created

mkdir /home/<new_user>
cp -r /etc/skel/. /home/<new_user>

This will copy all files/folder recursively from /etc/skel in to the already existing folder created on the first line.

share|improve this answer
1  
If I didn't get it wrong, this didn't copy hidden/dot files. –  Halil Özgür Mar 16 '13 at 9:56
6  
Works well for me. Note that the '.' is critical to it working. –  Mark Aug 20 '13 at 15:39
14  
This is the best and most correct answer, should be the accepted one. –  thnee Oct 14 '13 at 15:11
7  
It works, but, why ? Can't find a reference to this in the manual. –  Julien Palard Jan 14 '14 at 13:37
2  
I think it works because normally, this would create a new folder with the name of the last folder in the first argument. However, since that name is ., this behavior would require it to create an already-existing directory, so it just skips that step. –  Zenexer Jun 11 '14 at 20:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.