xcopy "C:\SomeFolderName" "D:\SomeFolderName" /h /i /c /k /e /r /y

Use the above command. It will definitely work.

In this command data will be copied from c:\ to D:, even folders and system files as well. Here's what the flags do:

  • /h copies hidden and system files also
  • /i if destination does not exist and copying more than one file, assume that destination must be a directory
  • /c continue copying even if error occurs
  • /k copies attributes
  • /e copies directories and subdirectories, including empty ones
  • /r overwrites read-only files
  • /y suppress prompting to confirm whether you want to overwrite a file
  • /z Copies over a network in restartable mode.

more flags are found here https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/xcopy

Answer from SHABAZ KHAN on Stack Overflow
Top answer
1 of 4
30
cp dir1/* dir2

cp will not copy directories unless explicitly told to do so (with --recursive for example, see man cp).

Note 1: cp will most likely exit with a non-zero status, but the files will have been copied anyway. This may be an issue when chaining commands based on exit codes:&&, ||, if cp -r dir1/* dir2; then ..., etc. (Thanks to contrebis for their comment on that issue)

Note 2: cp expects the last parameter to be a single file name or directory. There really should be no wildcard * after the name of the target directory. dir2\* will be expanded by the shell just like dir1\*. Unexpected things will happen:

  • If dir2 is empty and depending on your shell and settings:
    • you may just get an error message, which is the best case scenario.
    • dir2/* will be taken literally (looking for a file/directory named *), which will probably lead to an error, too, unless * actually exists.
    • dir2/* it will just be removed from the command entirely, leaving cp dir1/*. Which, depending on the expansion of dir1/*, may even destroy data:
      • If dir1/* matches only one file or directory, you will get an error from cp.
      • If dir1/* matches exactly two files, one will be overwritten by the other (Bad).
      • If dir/* matches multiple files and the last match is a, you will get an error message.
      • If the last match of dir/* is a directory all other matches will be moved into it.
  • If dir2 is not empty, it again depends:
    • If the last match of dir2/* is a directory, dir1/* and the other matches of dir2/* will be moved into.
    • If the last match of dir2/* is a file, you probably will get an error message, unless dir1/* matches only one file.
2 of 4
16

It's the shell that expands wildcards, not the commands. So cp dir1/* dir2/* first expands the two wildcards, then calls cp on the result. This is not at all what you apparently expect: depending on how many files there are already in dir2, dir2/* may expand to one or more argument. The command cp doesn't know which of its arguments came from expanding the first pattern and which ones came from expanding the second pattern. It expects its last argument to be the name of the destination directory. Thus, to copy all the files from the directory dir1 into the directory dir2, the last argument must be the directory dir2:

cp dir1/* dir2

Since * matches all files, cp attempts to copy all files. This includes directories: directories are files too. It skips directories, but reports an error. It copies the content of special files such as named pipes (something had better be writing to them, or cp will block), etc.

To copy only regular files, you need to restrict the matching. In zsh, you can use the glob qualifier . for that:

cp dir1/*(.) dir2

Other shells don't have this. You can use the find command to filter on file types. Assuming that you're running non-embedded Linux or Cygwin:

find dir1 -maxdepth 1 -type f -exec cp -t dir2 {} +

On Linux, FreeBSD and OSX:

find dir1 -maxdepth 1 -type f | xargs -I {} cp {} dir2
Discussions

copying all file
I'm not sure if you're looking for a better way to do this, or just an explanation about why it doesn't work. A simple way to accomplish what you want is to use * instead of .. At the end of your source directory. cp -i /source/* /destination As for why you're seeing the behavior you're seeing... Hopefully someone can help clear this up for me, because I got a bit confused while looking into it. The simple explanation would be that . represents a directory, and cp will not copy any directories without the -r option. That being said, cp doesn't really seem to handle it as a directory. This seems to be because the trailing . character isn't expanded by the shell, and is instead interpreted by cp internally. The way cp handles it is... weird. It seems like kind of a special case. Practically it can be used to specify hidden files in a recursive copy. cp -r /source/. /destination will copy all files in the source directory including hidden files. cp -r /source/* /destination will copy all files in the source directory excluding hidden files. In both cases the directory itself isn't copied, just the contents. In the example above the . does not seem to represent a directory. It behaves more like a special wildcard character that includes hidden files. That would indicate that cp doesn't consider it a directory, and so -r should not be required... I don't really understand WHY it behaves that way and I can't seem to find any good explanation for it anywhere. So good question. I'm stumped. Hopefully someone else can elaborate. More on reddit.com
🌐 r/linuxquestions
6
8
June 29, 2018
How to copy all files recursively into another folder with permission?
How to copy all files recursively into another folder with permission? Who owns the files? If some of the files are owned by root and others by your user, you need to do it this way: $ rsync -av (source path) (destination path) This creates an exact copy of all files and directories, permissions included, from source to destination. Don't do this with system directories, only with data directories, and definitely not for directories like /dev. More on reddit.com
🌐 r/linuxquestions
12
2
January 14, 2021
Copying all folders and files from one location to another | Access World Forums
I have been scouring the web and forums and am unable to find anything to my issue. I am in need of a way to push a button on a form and have it copy all files and folders in one location and then essentially paste (generic MS term I know) them to another location. The only way I know how to do... More on access-programmers.co.uk
🌐 access-programmers.co.uk
June 30, 2023
Any EASY Way to Move Multiple Files from Multiple Folders to Root Folder?
Somewhere in the past, I used an application that sucked in all my photos and arranged them in individual folders by month and year. So when viewing My Photos, I have folders named by month going back ten years to the genesis of my first digital camera.… More on learn.microsoft.com
🌐 learn.microsoft.com
17
232
🌐
GeeksforGeeks
geeksforgeeks.org › python › copy-all-files-from-one-directory-to-another-using-python
Copy all files from one directory to another using Python - GeeksforGeeks
April 26, 2025 - import shutil import os s = 'fol1' # source directory d = 'fol2' # destination directory if not os.path.exists(d): shutil.copytree(s, d) else: print("Already exists") ... Explanation: This code checks if fol2 exists using os.path.exists(d). If it doesn't exist, it uses shutil.copytree(s, d) to copy all contents of fol1 to fol2. If fol2 already exists, it prints "Already exists" to prevent overwriting. shutil.copy2() copies individual files and preserves the file's metadata (like timestamps and permissions), unlike shutil.copy() which doesn't retain such metadata.
🌐
IBM
ibm.com › docs › en › aix › 7.2.0
Copying files (cp command)
Use the cp command to create a copy of the contents of the file or directory specified by the SourceFile or SourceDirectory parameters into the file or directory specified by the TargetFile or TargetDirectory parameters.
Find elsewhere
🌐
Reddit
reddit.com › r/linuxquestions › copying all file
r/linuxquestions on Reddit: copying all file
June 29, 2018 -

Hey,

I wanted to copy all all files from one directory to another, like this:

cp -i /source/. /destination

I knew the source had no subdirectories and I wanted a prompt if files needed to be overwritten.

Now, this didnt work got the error " -r not specified, omitting directory /source/"

It worked with -r. I dont understand why though, there wre

Cheers,

Top answer
1 of 4
6
It is early and with one eye open look into “rsync”.
2 of 4
2
I'm not sure if you're looking for a better way to do this, or just an explanation about why it doesn't work. A simple way to accomplish what you want is to use * instead of .. At the end of your source directory. cp -i /source/* /destination As for why you're seeing the behavior you're seeing... Hopefully someone can help clear this up for me, because I got a bit confused while looking into it. The simple explanation would be that . represents a directory, and cp will not copy any directories without the -r option. That being said, cp doesn't really seem to handle it as a directory. This seems to be because the trailing . character isn't expanded by the shell, and is instead interpreted by cp internally. The way cp handles it is... weird. It seems like kind of a special case. Practically it can be used to specify hidden files in a recursive copy. cp -r /source/. /destination will copy all files in the source directory including hidden files. cp -r /source/* /destination will copy all files in the source directory excluding hidden files. In both cases the directory itself isn't copied, just the contents. In the example above the . does not seem to represent a directory. It behaves more like a special wildcard character that includes hidden files. That would indicate that cp doesn't consider it a directory, and so -r should not be required... I don't really understand WHY it behaves that way and I can't seem to find any good explanation for it anywhere. So good question. I'm stumped. Hopefully someone else can elaborate.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › visual-basic › developing-apps › programming › drives-directories-files › how-to-copy-a-directory-to-another-directory
How to: Copy a Directory to Another Directory - Visual Basic | Microsoft Learn
September 15, 2021 - Use the CopyDirectory method to copy a directory to another directory. This method copies the contents of the directory as well as the directory itself. If the target directory does not exist, it will be created.
🌐
Access World
access-programmers.co.uk › home › forums › microsoft access discussion › modules & vba
Copying all folders and files from one location to another | Access World Forums
June 30, 2023 - The only way I know how to do this is essentially using: If Dir("C:\Folderlocationtocheck") = "" Then MkDir ("C:\Createmissingfolder\") End If to create each and every single folder in the new location then using: FileSystemObject.CopyFile "c:\oldfilelocation\*.*", "c:\newfilelocation\" However, the problem is this takes way to much time to setup as I have to map out each folder and file location to get them to copy over. There is also the problem that new files and folders are created and without including them in the cumbersome way I currently know how to do this then they would never be copied when they are needed to be.
🌐
Linode
linode.com › docs › guides › how-to-copy-files-and-directories-in-linux
How to Copy Files and Directories in Linux | Linode Docs
July 18, 2022 - The -r option enables the cp command to operate recursively and copy a directory along with any files and subdirectories it contains. cp has a number of options, allowing users to run it interactively, use verbose mode, or preserve the file ...
🌐
C# Corner
c-sharpcorner.com › blogs › copy-files-from-one-directory-to-another1
Copy files from one directory to another directory in C#
August 23, 2019 - Step 2. We can use File.Copy method to copy a file from current location to a new location. We can also change the file name as well. The following code snippet reads all files in Your Path folder. Note: Replace "Your Path" with the full path of your directory you want to move and "Your Destination" with your new full path.
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-copy-the-contents-of-a-folder-to-another-folder-from-terminal-in-ubuntu.php
How to Copy the Contents of a Folder to Another Folder from Terminal in Ubuntu
You can use the cp command to copy files locally from one directory to another. For instance, if you want to copy the contents of the source folder to the destination folder in your current working directory, you can use the following command: ...
🌐
PhoenixNAP
phoenixnap.com › home › kb › sysadmin › how to copy files and directories in linux
How to Copy Files and Directories in Linux (With Examples)
December 19, 2025 - To copy an entire directory and its subdirectories and files, use the –r option. The following command copies all files and directories from the Documents directory to the new_destination directory:
🌐
DZone
dzone.com › coding › java › how to copy files from one directory to another in java: example
Copy Files From One Directory to Another in Java
October 23, 2021 - import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; /** * Java program to copy a file from one directory to another like from src to dest * * @author Javin Paul */ public class Testing { public static void main(String args[]) { // Using Apache Commons FileUtils class File srcFile = new File("bin/HelloWorld.class"); File destDir = new File("target"); try { FileUtils.copyFileToDirectory(srcFile, destDir); System.out.println("File successfully copied to destination directory in Java"); } catch (IOException e) { e.printStackTrace(); } } } Output File successfully copied to destination directory in Java