Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I copy everything in current directory to one if it's subdirectory in linux console?

share|improve this question

migrated from stackoverflow.com Aug 25 '10 at 18:00

This question came from our site for professional and enthusiast programmers.

    
Do you mean copy or move? – Craig Aug 25 '10 at 19:14

If you want to copy the contents of the folder recursively (will throw 1 error, alternatives below):

cp -r * sub/

A little more hacky, but works on non-empty subdirectories:

TARGETDIR='targetdir here';cp -r `find . -maxdepth 1 ! -name "$TARGETDIR"` "$TARGETDIR"

Another oneliner:

TARGETDIR='targetdir here';for file in *;do test "$file" != "$TARGETDIR" && cp "$file" "$TARGETDIR/";done

Or recursive:

TARGETDIR='z';for file in *;do test "$file" != "$TARGETDIR" && cp -r "$file" "$TARGETDIR/";done
share|improve this answer
    
this does not work in case sub/ is not empty -> sub will copied into sub again, unless that is what @Oguz wanted. – Rahul Aug 25 '10 at 17:49
    
Ok I'm finally satisfied with the find code :D – Lekensteyn Aug 25 '10 at 17:59
2  
Your second command fails for filenames that include spaces. Use xargs or -exec. No need for grep - use ! -name "$TARGETDIR" or similar. You have unmatched quotes around $file. I don't think a recursive cp will work the way you intend in any but your first command. – Dennis Williamson Aug 25 '10 at 19:00
    
Wohaa, missed a quote in the last codes. Good comment Dennis, I never thought of using -name in this case :) – Lekensteyn Aug 25 '10 at 19:21

I would suggest moving the target directory outside the source directory and then put it back again; mv is free (if you are careful not to move to a different filesystem), unless you are expecting other processes to interfere/be interfered.

Most solutions posted above won't work if there are spaces in filenames. I would suggest using variants of find -print0 | xargs -0, or find -exec, etc.

share|improve this answer

Supposing target is the name of the target subdirectory, if your shell is bash:

shopt -s extglob
cp -r !(target) target/

In ksh, you can directly do cp -r !(target) target/.

In zsh, you can do setopt ksh_glob then cp -r !(target) target/. Another possibility is setopt extended_glob then cp -r ^target target/.

share|improve this answer

Will this work for you?

cp -r * subdir/

If you meant to move instead of copying everything in the current dir to a subdirectory, you could do:

mv * subdir/
share|improve this answer
    
this does not take care of non-empty directories – Rahul Aug 25 '10 at 17:47
    
I just tested, and it worked. – karlphillip Aug 25 '10 at 17:49
    
you need to copy the non-empty directories recursively like @Lekensteyn suggested. – Rahul Aug 25 '10 at 17:53

This will copy everything, including dot files, and not including the target directory itself, to the target directory SUBDIR:

for i in `ls -a | grep -Ev '^(SUBDIR|\.\.?)$'`; do cp $i SUBDIR; done
share|improve this answer
1  
Fails for filenames that include spaces. – Dennis Williamson Aug 25 '10 at 18:54

This goes in say file dirCopy.sh



for i in `ls`
do
        if [ $i != "subDir" ]
        then
                `cp -r $i subDir`
        fi
done

run it as " sh dirCopy.sh " in your console

share|improve this answer
1  
No need for ls: for file in *. No need for backticks - use $(). No need for backticks (or $()) around the cp command (that will produce an error message). – Dennis Williamson Aug 25 '10 at 18:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .