Copy and overwrite directory recursively
I have a directory called ‘existing_folder’ and another directory called ‘temp’
I want to replace the contents of ‘existing_folder’ with those of ‘temp’ along with any sub directories.
Because the directory contains web pages, this has to be done in a way that ensures minimal downtime.
Is there a way to do this? What command should I use to achieve this?
3 Answers 3
If both orig_folder and temp are on the same physical hard drive, renaming (moving) them is essentially instantaneous. That means you could simply do
That will rename orig_folder to foo , then rename temp to orig_folder and finally delete foo . On the same filesystem, the two mv operations will take next to no time (0.004 seconds on my system).
If the source and target directories are not on the same file system, in order to minimize the time that the files are not available, you would first need to move the source directory to the same filesystem and then rename:
How do I copy folder with files to another folder in Unix/Linux? [closed]
Want to improve this question? Update the question so it’s on-topic for Stack Overflow.
Closed 7 years ago .
I am having some issues to copy a folder with files in that folder into another folder. Command cp -r doesn’t copy files in the folder.
3 Answers 3
The option you’re looking for is -R .
- If destination doesn’t exist, it will be created.
- -R means copy directories recursively . You can also use -r since it’s case-insensitive.
- Note the nuances with adding the trailing / as per @muni764’s comment.
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:
at a terminal prompt.
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.
Linux how to copy but not overwrite? [closed]
Want to improve this question? Update the question so it’s on-topic for Stack Overflow.
Closed 24 days ago .
I want to cp a directory but I do not want to overwrite any existing files even it they are older than the copied files. And I want to do it completely noninteractive as this will be a part of a Crontab Bash script. Any ideas?
8 Answers 8
As per comments rsync -a -v src dst is not correct because it will update existing files.
Is what you want. See the man page.
This will work on RedHat:
Updating and not overwriting is something different.
For people that find that don’t have an ‘n’ option (like me on RedHat) you can use cp -u to only write the file if the source is newer than the existing one (or there isn’t an existing one).
[edit] As mentioned in the comments, this will overwrite older files, so isn’t exactly what the OP wanted. Use ceving’s answer for that.How to force ‘cp’ to overwrite directory instead of creating another one inside?
I’m trying to write a Bash script that will overwrite an existing directory. I have a directory foo/ and I am trying to overwrite bar/ with it. But when I do this:
a new bar/foo/ directory is created. I don’t want that. There are two files in foo/ ; a and b . There are files with same names in bar/ as well. I want the foo/a and foo/b to replace bar/a and bar/b .
9 Answers 9
You can do this using -T option in cp .
See Man page for cp .
So as per your example, following is the file structure.
You can see the clear difference when you use -v for Verbose.
When you use just -R option.
When you use the option -T it overwrites the contents, treating the destination like a normal file and not directory.
This should solve your problem.
Do it in two steps.
If you want to ensure bar/ ends up identical to foo/ , use rsync instead:
If just a few things have changed, this will execute much faster than removing and re-copying the whole directory.
- -a is ‘archive mode’, which copies faithfully files in foo/ to bar/
- —delete removes extra files not in foo/ from bar/ as well, ensuring bar/ ends up identical
- If you want to see what it’s doing, add -vh for verbose and human-readable
- Note: the slash after foo is required, otherwise rsync will copy foo/ to bar/foo/ rather than overwriting bar/ itself.
- (Slashes after directories in rsync are confusing; if you’re interested, here’s the scoop. They tell rsync to refer to the contents of the directory, rather than the directory itself. So to overwrite from the contents of foo/ onto the contents of bar/ , we use a slash on both. It’s confusing because it won’t work as expected with a slash on neither, though; rsync sneakily always interprets the destination path as though it has a slash, even though it honors an absence of a slash on the source path. So we need a slash on the source path to make it match the auto-added slash on the destination path, if we want to copy the contents of foo/ into bar/ , rather than the directory foo/ itself landing into bar/ as bar/foo .)
rsync is very powerful and useful, if you’re curious look around for what else it can do (such as copying over ssh).
Copy and overwrite a file in shell script
I want to copy a certain file to a location, irrespective of that file already exists in the destination or not. I’m trying to copy through shell script.But the file is not getting copied. I’m using the following command
/bin/cp -rf /source/file /destination
but that doesn’t work.
4 Answers 4
This question has been already discussed, however you can write a little script like this:
Explaining this script a little bit
#!/bin/bash : tells your computer to use the bash interpreter.
if [ ! -d «$2» ]; then : If the second variable you supplied does not already exist.
mkdir -p «$2» : make that directory, including any parent directories supplied in the path.
Running mkdir -p one/two/three will make:
If you don’t supply the -p tag then you’ll get an error if directories one and two don’t exist:
fi : Closes the if statement.
cp -R «$1» «$2» : copies files from the first variable you supplied to the directory of the second variable you supplied.
So if you ran script.sh mars pluto , mars would be the first variable ( $1 ) and pluto would be the second variable ( $2 ).
The -R flag means it does this recursively, so the cp command will go through all the files and folders from your first variable, and copy them to the directory of your second variable.
Linux Copy File Command [ cp Command Examples ]
cp Command Syntax
Tutorial details | |
---|---|
Difficulty | Easy (rss) |
Root privileges | No |
Requirements | Terminal app/Shell prompt |
Time | 2m |
The syntax is as follows to copy files and directories using the cp command:
cp SOURCE DEST
cp SOURCE DIRECTORY
cp SOURCE1 SOURCE2 SOURCE3 SOURCEn DIRECTORY
cp [OPTION] SOURCE DEST
cp [OPTION] SOURCE DIRECTORY
Where,
- In the first and second syntax you copy SOURCE file to DEST file or DIRECTORY.
- In the third syntax you copy multiple SOURCE(s) (files) to DIRECTORY.
Note: You need to type the cp command at the dollar sign ($) prompt. This prompt means that the shell is ready to accept your typed commands. Do not type the dollar ($) sign. You need to open the Terminal app to use cp command on a Linux.
Linux Copy File Examples
To make a copy of a file called file.doc in the current directory as newfile.doc, enter:
$ cp file.doc newfile.doc
$ ls -l *.doc
Sample outputs:
You can copy multiple files simultaneously into another directory. In this example, copy the files named main.c, demo.h and lib.c into a directory named backup:
$ cp main.c demo.h libc. backup
If backup is located in /home/project, enter:
$ cp main.c demo.h libc. /home/project backup
Copy a file to another directory
To copy a file from your current directory into another directory called /tmp/, enter:
$ cp filename /tmp
$ ls /tmp/filename
$ cd /tmp
$ ls
$ rm filename
Verbose option
To see files as they are copied pass the -v option as follows to the cp command:
$ cp -v filename.txt filename.bak $ cp -v foo.txt /tmp
Preserve file attributes
To copy a file to a new file and preserve the modification date, time, and access control list associated with the source file, enter:
$ cp -p file.txt /dir1/dir2/
$ cp -p filename /path/to/new/location/myfile
This option ( -p ) forces cp to preserve the following attributes of each source file in the copy as allowed by permissions:
- Modification time/date
- Access time
- File flags
- File mode
- User ID (UID)
- Group ID (GID)
- Access Control Lists (ACLs)
- Extended Attributes (EAs)
Copying all files
The star wildcard represents anything i.e. all files. To copy all the files in a directory to a new directory, enter:
$ cp * /home/tom/backup
The star wildcard represents anything whose name ends with the .doc extension. So, to copy all the document files (*.doc) in a directory to a new directory, enter:
$ cp *.doc /home/tom/backup
Recursive copy
To copy a directory, including all its files and subdirectories, to another directory, enter (copy directories recursively):
$ cp -R * /home/tom/backup
Linux copy file command with interactive option
You can get prompt before overwriting file. For example, if it is desired to make a copy of a file called foo and call it bar and if a file named bar already exists, the following would prompt the user prior to replacing any files with identical names:
cp -i foo bar
Verbose output with cp command
If you pass the -v to the cp, it makes tells about what is going on. That is verbose output:
cp -v file1 file2
cp -avr dir2 /backups/
Conclusion
This page explained cp command that is used for copying files under Linux and Unix-like systems. For more info see man pages: ls(1).