- How to move (and overwrite) all files from one directory to another?
- 6 Answers 6
- cp Command in Linux: 7 Practical Examples
- 7 Examples of using cp command in Linux
- 1. How to copy a file
- 2. How to copy multiple files
- 3. Multiple ways of dealing with overwriting while copying files
- 4. How to copy a directory in Linux
- 5. How to copy only the content of a directory, not the directory itself
- 6. How to copy multiple directories
- 7. How to preserve the attributes while copying
- Conclusion
- How to replace all the contents from one folder with another one
- 3 Answers 3
How to move (and overwrite) all files from one directory to another?
I know of the mv command to move a file from one place to another, but how do I move all files from one directory into another (that has a bunch of other files), overwriting if the file already exists?
I know this is old question, but what about «rsync -az src/ dest/» it will copy all files which do not exist in dest/ directory from src/, and then just remove destination directory with «rm -rf dest/»
6 Answers 6
-f, --force do not prompt before overwriting
mv -f /a/b /c/d moves b inside d (so I have /c/d/a ) instead of overwriting d with a (I want /c/a ). How can I do such overwrite?
@Xenos you would need to delete the file d before you can create a directory d . That would require a couple of commands, there’s no force overwrite of a directory with a file that I know of. You could script that with just a few lines, but it would be better taken up as a separate question since it deviates from question the OPasked.
It’s just mv srcdir/* targetdir/ .
If there are too many files in srcdir you might want to try something like the following approach:
cd srcdir find -exec mv <> targetdir/ +
In contrast to \; the final + collects arguments in an xargs like manner instead of executing mv once for every file.
just a note: on directories with a couple of thousands files, you wont be able to use the * since the argument list will be too long for the shell to handle, so you would need to use «find» instead or similar solution
I’ve tried the following command «find . -exec mv <> ../clean-copy/Ressources/img/ +» but got the following error : «find: -exec: no terminating «;» or «+»» — Any idea why ?
@musiKk Thanks for the tip. Although my comment is a question, it’s here because I feel the answer is 99% helping but missing the 1% that would make it perfect.
It’s also possible by using rsync , for example:
rsync -va --delete-after src/ dst/
- -v , —verbose : increase verbosity
- -a , —archive : archive mode; equals -rlptgoD (no -H,-A,-X )
- —delete-after : delete files on the receiving side be done after the transfer has completed
If you’ve root privileges, prefix with sudo to override potential permission issues.
yup, just lost all my files. Shame on me for thinking people online know what they are talking about.
For moving and overwriting files, it doesn’t look like there is the -R option (when in doubt check your options by typing [your_cmd] —help . Also, this answer depends on how you want to move your file. Move all files, files & directories, replace files at destination, etc.
When you type in mv —help it returns the description of all options.
For mv, the syntax is mv [option] [file_source] [file_destination]
To move simple files: mv image.jpg folder/image.jpg
To move as folder into destination mv folder home/folder
To move all files in source to destination mv folder/* home/folder/
Use -v if you want to see what is being done: mv -v
Use -i to prompt before overwriting: mv -i
Use -u to update files in destination. It will only move source files newer than the file in the destination, and when it doesn’t exist yet: mv -u
Tie options together like mv -viu , etc.
cp Command in Linux: 7 Practical Examples
cp command in Linux is used for copying files and directories. In this tutorial, you’ll learn some of the essential usages of the cp command.
One of the commands that you must know in Linux is cp. It’s often called the copy command in Linux and it is actually short for copy and it does exactly as it name suggests: it copies.
cp is used for copying files from one location to other. cp can also be used to copy entire directories into a new location. You can use it to copy multiple files and directories as well.
7 Examples of using cp command in Linux
Let’s see how you can use cp command for various purposes:
1. How to copy a file
The simplest example would be to copy a file. To do that, you just have to specify the source file and the destination directory or file.
cp source_file target_directory/target_file
In the above example, if the target_fille doesn’t exist in the target_directory, it will create target_file.
However, if the new_file already exists, it will overwrite it without asking. This means the content of the existing target file will be changed with the content of the source file.
I’ll show you how to deal with overwriting of files later in this tutorial.
By default, cp command overwrites if the target file already exists. This behavior can be changed with -n or -i option, explained later.
2. How to copy multiple files
If you want to copy multiple files at once to a new location, you can do that in the following manner:
cp file1 file2 file3 fileN target_directory
This will copy all the specified files to the target directory. If the target directory has file(s) matching the name of the source file(s), it will be overwritten.
3. Multiple ways of dealing with overwriting while copying files
You probably won’t always want that your existing target files are overwritten and that’s totally logical.
To prevent overwriting existing files, you can use the -n option. This way, cp won’ overwrite existing files.
cp -n source_file target_directory
But maybe you want to overwrite some files. You can use the interactive option -i and it will ask you if you want to overwrite an existing file(s).
cp -i source_file target_directory cp: overwrite 'target_directory/source_file'?
You can enter y for overwriting the existing file or n for not overwriting it.
There is also an option for making automatic backups. If you use -b option with the cp command, it will overwrite the existing files but before that, it will create a backup of the overwritten files.
cp -b file.txt target_dir/file.txt ls target_dir file.txt file.txt~
The backup of the file ends with ~.
You can also use the update option -u when dealing with overwriting. With the -u option, source files will only be copied to the new location if the source file is newer than the existing file or if it doesn’t exist in the target directory.
- -i : Confirm before overwriting
- -n : No overwriting
- -b : Overwriting with backup
- -u : Overwrite if the target file is old or doesn’t exist
4. How to copy a directory in Linux
You can also use the cp command to copy a directory in Linux including all its files and sub-directories. You have to use the -r option here which stands for recursive.
cp -r source_dir target_dir
This will copy the entire source_dir into target_dir. Now the source_dir will be a subdirectory of the target_dir.
5. How to copy only the content of a directory, not the directory itself
In the previous example, you copied the entire directory into a new location.
But if you just want to copy the content of the source directory into the target directory, you should add /. at the end of the source directory. This will indicate that you only want to copy the content of the source directory.
Let’s see it with an example:
ls source_dir source_file_1 source_file2
Now copy the content of the source directory:
cp -r source_dir/. target_dir
If you check the contents of the target directory now, you’ll see that only the contents of the source directory have been copied.
ls target_dir source_file_1 source_file2
6. How to copy multiple directories
You can also copy multiple directories at once with cp command in Linux.
Just use it the same way you did for a single directory.
cp -r source_dir1 source_dir2 source_dir3 target_dir
It’s always the last argument in the command that is taken as the target directory.
If you want to copy only the content of multiple directories at once, you can do that as well:
cp -r source_dir1/. source_dir2/. source_dir3/. target_dir
In fact, you can mix directories, their content and files altogether.
cp -r source_dir1 source_dir2/. source_file target_dir
7. How to preserve the attributes while copying
When you copy a file to a new location, its attributes like the file permissions and the file timestamps are changed.
If you want to retain the attributes of the original file, you can copy the files with the option -p.
Let’s see it with an example.
ls -l /etc/services -rw-r--r-- 1 root root 19183 Dec 26 2016 /etc/services
If I try to copy this file normally, its attributes will be changed:
cp /etc/services . ls -l services -rwxrwxrwx 1 abhishek abhishek 19183 Nov 25 20:45 service
But if I use the option p, the copied file will retain the mode, ownership and the timestamp.
cp -p /etc/services . ls -l services -rw-r--r-- 1 abhishek abhishek 19183 Dec 26 2016 services
As you can see, you preserved the access mode and the timestamp pf the source file with the -p option.
But wait! Was it not supposed to preserve the ownership of the source files as well? But here the owner (root) of the source file has been changed to abhishek.
This is because only root has permission to change the ownership of a file owned by root. If you use the -p option with a file not owned by root, it will preserve the ownership. Or, you can run the command with sudo to preserve the ownership of a file owned by root.
You can also specify the attributes you want to preserve. But then you’ll have to use the –preserve option.
cp --preserve=timestamp /etc/services . ls -l services -rw-r--r-- 1 abhishek abhishek 19183 Dec 26 2016 services
As you can see in the above output, it preserved only the source file’s timestamp.
Conclusion
You can further explore the cp command by browsing its man page. The examples shown here are the most common ones that you’ll be using as Linux user, sysadmin or software developer.
In fact, the cp command is somewhat of a standard for copying files. Its syntax is followed in other copying commands like scp and docker cp.
If you liked this tutorial, please share this article on social media and various forums.
How to replace all the contents from one folder with another one
I have folders old and new . I want to replace all the contents in old/* to new/* . There can be many subdirectories in those folders. But there can be few files which are not in new/* but are in old/* , so I want them to stay as they are. How can I do that from a Linux shell?
3 Answers 3
rsync would probably be a better option here. It’s as simple as rsync -a subdir/ ./. check this unix.stackexchage answer for better solutions
use -f with the cp command
suppress cp to overwrite» prompt..
To override cp’s alias you can simply enclose it in quotes:
for more information follow these links:
Don’t use rsync for a one time copy, the overhead of hashing all dir contents is substantial, it only pays when keeping directories synchronized. For a gigantic one time copy, use a tarpipe.
Use rsync . It will synchronize the directories in one direction. So, if you want to update your old folder with everything from new , but keep what’s in there, just use:
rsync -avh --dry-run /path/to/new/ /path/to/old/
This will, in a first instance, just output the list of files that would be transferred. In that case: Everything found in new will be copied to old , unless it’s already there. Everything in old stays as it is.
If it looks fine to you, remove the —dry-run argument to transmit them for real.
The -avh flags just enable archive mode (which will preserve timestamps, etc.), verbosity and human-readable file-sizes. Nothing will be deleted from the destination unless you specify the —delete flag. Consult man rsync for more information.