- How to copy in bash all directory and files recursive?
- 4 Answers 4
- How to Copy All Files from a Directory to another Directory in Linux
- How to Copy Files with “cp” Command in Linux:
- Copy a file with the same name:
- Copy a file with a different name:
- Copy Multiple Files with “cp” Command:
- How to Copy files with the “rsync” command in Linux:
- Conclusion:
- About the author
- Syeda Wardah Batool
- Copy files from one directory into an existing directory
- 10 Answers 10
- Nov, 2021 Update:
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
How to Copy All Files from a Directory to another Directory in Linux
Copying the file or folder means creating a new file having duplicate content as in the existing file.
Sometimes, we need to copy the files or folders rather than having a backup program. The files can be copied with the same name, or you can change the name as well.
Copying a file, folder, or directory is a simple and basic task in the Linux operating system. Rename, delete or copy commands are used as daily purpose operations while working with the command-line interface.
Although there are multiple commands to copying the files, the “cp” and “rsync” command are widely used simplest approaches.
How to Copy Files with “cp” Command in Linux:
The “cp” command is one of the commonly used commands to perform the copy operation. You can copy files or folders from source to destination, i-e, one directory through this command.
The syntax of the “cp” command is:
Let’s take a look at an example to understand the “cp” command tool better.
In the home directory, create a “temp” folder with the text file named “text_file1.txt” and add random content to it.
Copy a file with the same name:
To copy a “text_file1.txt” file directory with the same name, open the terminal and type the mentioned “cp” command with the right path.
Get the folder’s path by right-clicking on the file and navigate to the “Properties” option (it is the easy way to get the path link).
A dialogue box will open with the complete path of a text file:
Use this path with the “cp” command to copy file:
This command will copy the “text_file1.txt” file to the “temp2” folder.
To verify it, type the “ls” command in the terminal:
Copy a file with a different name:
To copy the file in the current working directory with the different name, type the following “cp” command with file location:
Verify it using the “ls” command”:
Copy Multiple Files with “cp” Command:
To copy multiple files with the “cp” command, navigate the terminal to the directory where files are saved and then run the “cp” command with the file names you want to copy and the destination path.
$ cp text_file1.txt text_file2.txt text_file3.txt / home / wardah / temp2
Run the mentioned command to verify if files are copied successfully:
Above mentioned scenarios are how to copy a single or selected file in a directory. Now, use the wildcard character (*) to copy present files of one directory to any other specific directory.
Run the “ls” command to check how many files exist in the temp directory:
Instead of mentioning all file names in the terminal, use the wildcard (*) with the directory path to copy all the files into destination:
Now, run the “ls” command again to check if all files are copied in the “temp2” directory:
How to Copy files with the “rsync” command in Linux:
The “rsync” command is another versatile Linux tool to synchronize and copy files and directories locally as well as remotely.
The syntax of the “rsync” command is to copy files is:
It is a pre-built tool in many Linux distribution. However, if you don’t get it on your system, install it by executing the following command:
To copy a file from one place to another, run the following command:
To copy all the directory files to another location, the command would be:
(The “-a” with the “rsync” command is used to copy directories recursively)
Here are the two concepts:
If you add a trailing slash (/) with the path, it will copy the content of the source directory to the destination directory, just like shown in the image:
But, if you don’t add it, it will copy the source directory inside the destination directory, like:
The above command will copy a “dir1” directory to the “dir2” directory.
Conclusion:
Copying a file or directory is the basic command one can operate. One can use it multiple times while using Linux operating system.
This guide has seen the two simplest approaches, the “cp” command and the “rsync” command. Using these commands, we have learned how to copy a single file, multiple files, and even copy one directory to another.
About the author
Syeda Wardah Batool
I am a Software Engineer Graduate and Self Motivated Linux writer. I also love to read latest Linux books. Moreover, in my free time, i love to read books on Personal development.
Copy files from one directory into an existing directory
How do I do this? I tried cp -r t1 t2 (both t1 and t2 are existing directories, t1 has files in it) but it created a directory called t1 inside t2, I don’t want that, I need the files in t1 to go directly inside t2. How do I do this?
Why was this closed? It is arbitrary if a bash command is a built-in or external command (e.g. printf exists as both on most systems), so cp questions can well be seen as bash questions, which is a programming language. I have never seen a Python question talking about file copy be closed.
I arrived in search of a reminder about the syntax of the Bash shell copy command, and I am happy to report that these commands seem also to work against the underlying NTFS filesystem on my Windows installation.
10 Answers 10
The dot at the end tells it to copy the contents of the current directory, not the directory itself. This method also includes hidden files and folders.
@CiroSantilli六四事件法轮功包卓轩 If you copy a directory, cp will create a directory and copy all the files into it. If you use the pretend folder called «.», which is the same as the directory holding it, the copy behaves this way. Let’s say t1 contains a file called «file». cp will perform the operation equivalent to «cp t1/./file t2/./». It is copying the folder «.», but copying files into t2’s «.» folder strips the «./» because «t2/./» is the same as «t2/». Technically, this means it’s POSIX built in behavior. but probably not in the way you might have been expecting!
once I tested using the source path trailing a dot (t1/.) it copied the entire t1 folder with its content into the t2 folder. So I got a t1 folder inside the t2 folder. But once I used * instead of dot it did work and copied only the content of t1 into t2. So I think the best answer to this question is the following script -> cp -R t1/* t2/
Or if you have directories inside dir1 that you’d want to copy as well
Depending on how your shell is configured, you’ll probably need to use «dir1/* dir1/.*» instead of «dir1/*» if you want to also catch hidden files.
Copying dir1/.* is not a good idea, as it copies dir1/.. (i.e. the parent of the directory you’re actually trying to copy). It also copies dir1/. which is fine, except that it’s already (mostly) been copied, so you’re doing the work twice.
You can get around the dir1/.* /hidden files problem by cd-ing into the directory you want to copy from, and then referring to it as . . So, if you want to copy all files including hidden files from a directory into an existing directory, you can: cd [source dir] , cp . [path to destination dir, with no trailing slash] .
If you want to copy something from one directory into the current directory, do this:
This assumes you’re not trying to copy hidden files.
Assuming t1 is the folder with files in it, and t2 is the empty directory. What you want is something like this:
Bear in mind, for the first example, t1 and t2 have to be the full paths, or relative paths (based on where you are). If you want, you can navigate to the empty folder (t2) and do this:
Or you can navigate to the folder with files (t1) and do this:
Note: The * sign (or wildcard) stands for all files and folders. The -R flag means recursively (everything inside everything).
The trailing slash on the source directory changes the semantics slightly, so it copies the contents but not the directory itself. It also avoids the problems with globbing and invisible files that Bertrand’s answer has (copying t1/* misses invisible files, copying `t1/* t1/.*’ copies t1/. and t1/. which you don’t want).
Your solution does not work, at least not on my installation (ubuntu 12.10) $ mkdir t1 $ mkdir t2 $ touch t1/one $ touch t1/two $ touch t1/.three $ cp -R t1/ t2 $ ls t2/ t1 (sorry no codeformat in comments, readable version at pastebin.com/yszSxV6G)
For inside some directory, this will be use full as it copy all contents from «folder1» to new directory «folder2» inside some directory.
$(pwd) will get path for current directory.
Notice the dot (.) after folder1 to get all contents inside folder1
cp -r $(pwd)/folder1/. $(pwd)/folder2
Nov, 2021 Update:
This code with Flag «-R» copies perfectly all the contents of «folder1» to existing «folder2»:
Flag «-R» copies symbolic links as well but Flag «-r» skips symbolic links so Flag «-R» is better than Flag «-r».
-R, --dereference-recursive For each directory operand, read and process all files in that directory, recursively, following all symbolic links.
-r, --recursive For each directory operand, read and process all files in that directory, recursively. Follow symbolic links on the command line, but skip symlinks that are encountered recursively. Note that if no file operand is given, grep searches the working directory. This is the same as the ‘--directories=recurse’ option.