how it is done meme

Linux cp command how to copy files to a directory excluding current directory name

So you want to copy files from one directory to another including all sub directories and files. It sounds simple at first. Say you have a directory with files named /directory1 and you want to move the files and directories within that directory to another named /directory2.

How do you do this with the cp command? You may have tried navigating to the parent of /directory1 and tried

cp -r directory1 directory2

That will work. It will move the contents of /directory1 into /directory2. However, the directory structure will now be off if you just expected the contents of directory1 to be inside directory2, now you will have /directory1 inside /directory2.

If that is what you want you win. If you want just the contents of directory1 to be copied into directory2 then you need to do something different.

There is more than one way to achieve this. One ways is to navigate into the actual directory you want to copy and typing something like this

cp -r . /path_to_directory2 

This period after -r tells cp to copy all of the files and directories of the current directory you are located in, to directory 2, where ever that is.

You can also do this little trick I found while messing around.

cp -r directory1/ directory2

Here you navigate to the parent folder of directory1 or wherever if you use an absolute path. Note the trailing slash directory1/ If you don’t leave the slash then you get the contents of directory1 with the directory1 folder inserted into directory2. By adding the slash on the end you get the contents only of directory1 inserted into directory2 instead of the directory1 placed inside directory2.


Posted

in

,

by

Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d