I have created a folder (directory) named Parent
which contains several files (namely file1
file2
... file100
) and subdirectories (Folder2
, Folder3
etc) and a particular sub-directory named Child1
which itself may contain files and folders.
Now, I want to copy all the contents of Parent
excluding 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?
Assuming you are using bash as your interactive shell, you can enable extglob which allows you to specify "all files except these ones".
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.
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.
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.
Try :
There will be a message that cp omitted the "Child1" directory.
Less elegant than
extglob
, but also works...Remove
echo
after testing to actually move the files. If you have hidden files and want to include them, first runshopt -s dotglob
.As a script (again, you need to remove
echo
to actually move files):rsync
has an option to exclude files or directories using--exclude
option.So in this case I think
rsync
would be a nice choice.If the files are actually called 'file1' etc., you can do this:
Otherwise you'll have to list the files in the cp command.
The simplest way is to move the
Child1
directory elsewhere, temporarily:This finds all files (excluding directory) inside your Parent/ directory and copies each of those files to Parent/Child
You can use
ls
to list the contents of "Parent",pipe the result to
grep -v
to filter out "Child1",and then send the resulting list to
cp
.This should do the trick: (from within the "Parent" dir)