How do I copy folder with files to another folder in Unix/Linux?
The option you're looking for is -R
.
cp -R path_to_source path_to_destination/
- If
destination
doesn't exist, it will be created. -R
meanscopy directories recursively
. You can also use-r
since it's case-insensitive.- To copy everything inside the source folder (symlinks, hidden files) without copying the source folder itself use
-a
flag along with trailing/.
in the source (as per@muni764
's /@Anton Krug
's comment):
cp -a path_to_source/. path_to_destination/
Ansible: copy a directory content to another directory
Resolved answer:
To copy a directory's content to another directory I use the next:
- name: copy consul_ui files
command: cp -r /home/{{ user }}/dist/{{ item }} /usr/share/nginx/html
with_items:
- "index.html"
- "static/"
It copies both items to the other directory. In the example, one of the items is a directory and the other is not. It works perfectly.
How to copy and paste a whole directory to a new path recursively?
Here is an example that will recursively clone a directory to another destination directory.
class Program
{
static void Main(string[] args)
{
CloneDirectory(@"C:\SomeRoot", @"C:\SomeOtherRoot");
}
private static void CloneDirectory(string root, string dest)
{
foreach (var directory in Directory.GetDirectories(root))
{
//Get the path of the new directory
var newDirectory = Path.Combine(dest, Path.GetFileName(directory));
//Create the directory if it doesn't already exist
Directory.CreateDirectory(newDirectory);
//Recursively clone the directory
CloneDirectory(directory, newDirectory);
}
foreach (var file in Directory.GetFiles(root))
{
File.Copy(file, Path.Combine(dest, Path.GetFileName(file)));
}
}
}
copy a whole folder (not just contents) into another folder in cmd terminal
The reason xcopy C:\folderA C:\folderB\folderA /E
does not work is because folderA
does not exist in C:\folderB
. Which is evident from the directory structure in your question:
/folderA
/folderB
/folderC
You need to use the /I
switch to create the directory if it does not exist. So your command should be:
xcopy C:\folderA C:\folderB\folderA /E /I
Note that Microsoft also advises to use the /O
, /X
, /H
and /K
switches with xcopy
when you want to retain the folder's permissions. There effects follow:
/H - Copies hidden and system files also.
/K - Copies attributes. Typically, Xcopy resets read-only attributes.
/O - Copies file ownership and ACL information.
/X - Copies file audit settings (implies /O)
Source: HOW TO: Copy a Folder to Another Folder and Retain its Permissions
If you would like more information about copying a folder and its contents to another folder using xcopy
and alternatives to using xcopy
, then check out this Super User question: Commmand line command to copy entire directory (including directory folder) to another directory.
Copy entire directory contents to another directory?
FileUtils.copyDirectory()
Copies a whole directory
to a new location preserving the file
dates. This method copies the
specified directory and all its child
directories and files to the specified
destination. The destination is the
new location and name of the
directory.The destination directory is created
if it does not exist. If the
destination directory did exist, then
this method merges the source with the
destination, with the source taking
precedence.
To do so, here's the example code
String source = "C:/your/source";
File srcDir = new File(source);
String destination = "C:/your/destination";
File destDir = new File(destination);
try {
FileUtils.copyDirectory(srcDir, destDir);
} catch (IOException e) {
e.printStackTrace();
}
Copy directory contents into a directory with python
I found this code working which is part of the standard library:
from distutils.dir_util import copy_tree
# copy subdirectory example
from_directory = "/a/b/c"
to_directory = "/x/y/z"
copy_tree(from_directory, to_directory)
Reference:
- Python 2: https://docs.python.org/2/distutils/apiref.html#distutils.dir_util.copy_tree
- Python 3: https://docs.python.org/3/distutils/apiref.html#distutils.dir_util.copy_tree
Copy the entire contents of a directory in C#
Much easier
private static void CopyFilesRecursively(string sourcePath, string targetPath)
{
//Now Create all of the directories
foreach (string dirPath in Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories))
{
Directory.CreateDirectory(dirPath.Replace(sourcePath, targetPath));
}
//Copy all the files & Replaces any files with the same name
foreach (string newPath in Directory.GetFiles(sourcePath, "*.*",SearchOption.AllDirectories))
{
File.Copy(newPath, newPath.Replace(sourcePath, targetPath), true);
}
}
Copy directory to another directory using ADD command
ADD go /usr/local/
will copy the contents of your local go
directory in the /usr/local/
directory of your docker image.
To copy the go
directory itself in /usr/local/
use:
ADD go /usr/local/go
or
COPY go /usr/local/go
Related Topics
Problems Resteasy 3.09 Corsfilter
How to Ignore Pkix Path Building Failed: Sun.Security.Provider.Certpath.Suncertpathbuilderexception
What Is the Purpose of Accesstype.Field, Accesstype.Property and @Access
Find Maximum, Minimum, Sum and Average of a List in Java 8
Java_Home Should Point to a Jdk Not a Jre
Getting Nosuchmethoderror:Javax.Servlet.Servletcontext.Getvirtualservername()
@Valid When Creating Objects with Jackson Without Controller
How Does Bitshifting Work in Java
Concurrent Threads Adding to Arraylist at Same Time - What Happens
How to Disable Loggers of a Class or of Whole Package
How Are Servlet Url Mappings in Web.Xml Used
Often Big Numbers Become Negative
What's the Meaning of System.Out.Println in Java
How to Change Java Logging Console Output from Std Err to Std Out