Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have created a folder(directory) named 'Parent' which contains several files(namely 'file1', 'file2'...'file100') and a sub-directory named 'Child1'which itself may contain files and folders . Now, I want to copy all the contents of 'Parent' excluding the 'Child1' into the folder 'Child1'.

The final content of 'Child1' should be something like this : Old content of Child1 + 'file1' ... 'file100'+ 'Folder2' + 'Folder3' + etc.

How can I achieve this? Please help.

share|improve this question

5 Answers 5

Assuming you are using bash as your interactive shell, you can enable extglob which allows you to specify "all files except these ones".

shopt -s extglob
cd Parent
cp !(Child1) Child1/
share|improve this answer

Three excellent answers that demonstrate the flexibility of linux. And it's always a good idea to be familiar with basic command line operations. Personally I prefer a hybrid approach and I try using mc (midnight commander) when it's available.

sudo apt-get install mc

will get it for you. Surviving tyrannosaurs like me might notice the resemblance with Norton commander from the DOS days. Its operation is quite intuitive. You have two panels. You switch between them with TAB. There are Fn key shortcuts for basic operations, like view, edit, copy, move, rename, delete, etc. mc

In your case, all you have to do is type mc in your terminal window and navigate to Parent folder in one panel, child folder on the other. Then select parent panel and press + and then enter. This will select all files and folders in the panel. With arrow keys go on top of Child1 folder and press Ins key. This will de-select that folder (Ins key toggles select status of an item.) Now press F5 and all selected content will be copied to the other panel (which is inside Child1 folder.)

I am providing this information only for the sake of completeness and to further demonstrate the variety of solutions available. By all means, go on and learn the basic linux commands and their variations. You can always rely on them being available at your fingertip on any system.

share|improve this answer
    
your explanation was indeed good. But, I just started learning Shell Scripting and while learning cp command this question hit me. So, wanted to achieve through BASIC commands without the help of other packages. –  tHe_VaGaBonD May 23 '13 at 9:37

If the files are actually called 'file1' etc., you can do this:

cd Parent
cp file* Child1/

Otherwise you'll have to list the files in the cp command.

share|improve this answer

Try :

cd [WhereeverParentLies]
cp ./* ./Child1/

There will be a message that cp omitted the "Child1" directory.

share|improve this answer
    
Yes, it only copies files. This is not a general solution, but in the problem only the copying of files was mentioned. –  Manuel May 23 '13 at 9:56
    
@Manuek : but it also omitted 'Child2' and 'Child3' which are other directories present in Parent. So your suggestion just copies FILES and no FOLDER from Parent to Child1. However,I achieved that with cp -R ./* ./Child1/ with warning message : cp: cannot copy a directory, ‘./child1’, into itself, ‘./child1/child1’ which is obviously expected. –  tHe_VaGaBonD May 23 '13 at 9:56
    
@Manuek : thanx, I edited the question. However question statement > Now, I want to copy all the contents of 'Parent' excluding the > 'Child1' into the folder 'Child1'. should have conveyed that ,I wanted to copy the folders too. –  tHe_VaGaBonD May 23 '13 at 10:01
    
Ok, then it depends on the folders... If the number is low, i would manually cp -r them –  Manuel May 23 '13 at 10:08
find Parent/ -maxdepth 1 -type f | xargs cp -t Parent/Child/

This finds all files (excluding directory) inside your Parent/ directory and copies each of those files to Parent/Child

find

    -type f match file only
    -maxdepth 1 find only in the Parent/ directory, do not find in directories inside Parent

cp

    -t target the directory where you want to copy the files
share|improve this answer
    
find | xargs is bad practice. It'll fail for filenames containing whitespace or quote characters. Using the -exec action of find is the preferred, and fully safe, way. –  geirha May 22 '13 at 13:30
    
thanks for letting me know. I haven't encountered that yet so I didn't know it could be a problem. so would the command be find Parent/ -maxdepth 1 -type f -exec cp -t Parent/Child? –  llt May 23 '13 at 4:52

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.