How to copy in bash all directory and files recursive?
SourceDir contains also sub-folders. Problem that in DestFolder not only all tree, but in up level all another levels and files. How to fix ?
4 Answers 4
cp -r ./SourceFolder ./DestFolder
So, to clarify, capital -R option will copy the root dir again; small -r option keeps the root paths the same.
cp is not a bash command but a separate executable, so it is system-specific. True bash commands on the other hand are the same across operating systems.
I had to add a slash at the end of my source folder. Not sure why. I was using build vars too. cp -r «$CONFIG_PATH/» «$CODESIGNING_FOLDER_PATH»
cp -r ./SourceFolder ./DestFolder
code for a copy with success result
cp -rv ./SourceFolder ./DestFolder
code for Forcefully if source contains any readonly file it will also copy
cp -rf ./SourceFolder ./DestFolder
also try this cp -r ./dist/* ./out ;
this command will copy dist/* files to out dir;
@SherylHohman It’s different because he put a /* on the end of the source folder. I don’t know why that matters though.
this response instead of copying the entire folder copies content of the source folder into the destination (./out) folder
You might find it handy to keep your attributes set
cp -arf ./SourceFolder ./DestFolder
cp -arf . throws the error «cp: the -R and -r options may not be specified together.» Changing it to cp -af . solves it. I’m not sure if it’s a typo on your end of if cp -arf . actually worked for you, but I hope this helps in case anyone is getting the same error. Reference: stackoverflow.com/a/32695418/5810737
Command line recursive/deep directory copy in Linux?
What is a good, general way to make a recursive/deep directory copy in Linux that works in most cases? I’ve used simple things like cp -R as well as fairly elaborate cpio incantations. Are there any significant strengths or weaknesses that cause you to prefer one over the other? Which one do you use most often?
4 Answers 4
NAME cp - copy files and directories -a, --archive same as -dpR -d same as --no-dereference --preserve=links -p same as --preserve=mode,ownership,timestamps -R, -r, --recursive copy directories recursively
So in answer to your question:
Copy everything recursively from directory /foo to directory /bar while preserving symbolic links and file/directory ‘mode’ ‘ownership’ & ‘timestamps’.
This is certainly a very straight-forward and elegant solution. Have you ever found any drawbacks to this approach? Distributed file systems? Extremely large copies? Have you found other solutions like cpio, tar, or rsync to be more efficient in some cases?
Unless there’s a physical error I’ve never had a local ‘cp’ fail from memory (I’ve only done TB sized transfers) to any place mounted localy (regardless of NFS or Samba mounting). Checkout ‘USAGE’ under the rsync man page for examples you’ll find instructive. Favourite of mine: $ rsync —partial —progress —rsh=ssh —archive —verbose —compress foo/ user@hosname:~/bar (rsync -avzP)
Well I know that busybox, GNU & BSD cp are ok. Other than legacy Unix boxes I’ve never seen what won’t that work on? I don’t really see the point otherwise although I completely understand Zoredache’s sentiment of «It always seems to get the job done correctly, so I have never really looked to hard to find a replacement.»
@gyaresu Please consider splitting your second comment out into another answer so that we can vote on it.
I use a command like «cd $srcdir ; tar -c . | tar -C $destdir -x « most often. But I also use rsync -a $src $dst.
The biggest strength of the tar solution is that it is what I had to use on a system many years ago that didn’t have cpio, rsync or a cp that would copy recursively. Tar is pretty much everywhere. It is stuck on my head because I used it a lot, there probably are more elegant ways. It always seems to get the job done correctly, so I have never really looked to hard to find a replacement.
I like «the ubiquity of tar» argument a lot. I am proficient in vi for similar reasons even though I prefer other editing environments.
-R has long since been part of the standard (opengroup.org/onlinepubs/009695399/utilities/cp.html) though not -a.
It’s also relevant to this one, as Zoredache said he encountered a system that «didn’t have [. ] a cp that would copy recursively». Basically, -R -> portable, -a -> not portable.
This was back in ~95 and the system was running a late 80’s version of Unix. I don’t think -R was an option for cp, but I am not certain. I learned that tidbit reading the usenet.
Take a look a rsync . I like it because you copy less data when keeping two directories up to date . it can also work remotly. In its simplest form rsync -a /src /dest
This is a really good point. Essentially rsync has the ability to compute a «directory diff» and transfer only what is needed to synchronize the directories. So you can repeatedly call something like rsync -a src/ dest to «continually copy» directories recursively (the trailing / on src is needed for proper synchronization) whereas cp -a src/ dest doesn’t work like that. The cp command will make a new src directory in dest after the first cp. You’d need something like cp -au src/* dest for subsequent copies.
The other reason I like rsync is you can pass it the -P flag, which shows a progress bar. It gives you some idea of how long something will take
How do I create a copy of a directory in Unix/Linux? [closed]
I want to recursively create a copy of a directory and all its contents (e.g. files and subdirectories).
3 Answers 3
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 means copy 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/
i wonder why this exact command in dockerfile copies all source directory files into destination, instead of copying just whole directory.
I believe the ‘/’ on the end makes a difference and that might account for your experience. If the source includes the trailing slash it will copy what is in the directory only. If it does not include the trailing slash, it will copy the directory as well and then the contents inside of it. My memory is this behavior varies by command and maybe event by OS a bit. Here’s a reference with more info.
I would say if you don’t want to include the source and you want to make sure everything is copied (symlinks, hidden files) without copying the source parent folder is to use -ra source/. destination. This will make sure the content of the folder is copied, but not the parent folder itself, which is sometimes handy. And the difference is the /.
Note the importance of «Slash dot» on your source in cp -r src/. dest I know it is mentioned but I still seem to miss it every time.
You are looking for the cp command. You need to change directories so that you are outside of the directory you are trying to copy.
If the directory you’re copying is called dir1 and you want to copy it to your /home/Pictures folder:
Linux is case-sensitive and also needs the / after each directory to know that it isn’t a file. ~ is a special character in the terminal that automatically evaluates to the current user’s home directory. If you need to know what directory you are in, use the command pwd .
When you don’t know how to use a Linux command, there is a manual page that you can refer to by typing:
Also, to auto complete long file paths when typing in the terminal, you can hit Tab after you’ve started typing the path and you will either be presented with choices, or it will insert the remaining part of the path.
There is an important distinction between Linux and Unix in the answer because for Linux (GNU and BusyBox) -R , -r , and —recursive are all equivalent, as mentioned in this answer. For portability, i.e. POSIX compliance, you would want to use -R because of some implementation-dependent differences with -r . It’s important to read the man pages to know any idiosyncrasies that may arise (this is a good use case to show why POSIX standards are useful).