205
votes
Copy files in Linux, avoid the copy if files do exist in destination
Just use cp -n <source> <dest>.
From man page:
-n, --no-clobber
do NOT overwrite an existing file (overrides a previous -i option)
26
votes
Accepted
Can I hard link files with rsync instead of copying them?
The answer has turned out to be yes, with rsync's --link-dest option. It's just not obvious because it's not a simple on/off flag like cp -l, because it takes a path argument.
So, cp -l a/ b/ can also ...
24
votes
Recursive, Non-Overwriting File Copy?
This is also achievable using cp. See here:
sudo cp -vnpr /xxx/* /yyy
xxx = source
yyy = destination
v = verbose
n = no clobber (no overwrite)
p = preserve permissions
r = recursive
21
votes
Why is a directory copied with the cp command smaller than the original?
That is because du by default shows not the size of the file(s), but the disk space that they are using. You need to use the -b option to get sum of file sizes, instead of total of disk space used. ...
13
votes
Accepted
How do I SCP from remote machine to local machine?
You are doing it wrong:
In Terminal DON'T login to the remote machine.
Just run:
scp user@remote:<path>/error.txt /Users/myname/Desktop
In other words: Run SCP locally on your Mac and tell it ...
12
votes
Why is a directory copied with the cp command smaller than the original?
It might be due to the size of the directory "files".
In most filesystems, on disk, a directory is much like a regular file (with just a list of names and node numbers, mostly), using more blocks as ...
12
votes
cp --backup=CTRL, why nil and never?
1.
In the oldest GNU Coreutils 'cp' release, --backup was purely a boolean option and did not take any parameter. There was a separate --version-control option (and a few environment variables) to ...
11
votes
Why do we use cp to copy files and not dd? (in unix-derivatives)
They do the same thing UNLESS you are specifying one of the options to dd which limits which bytes are copied, such as seek or skip or count or if you use the dd options to mutate bytes such as conv....
9
votes
Why is tar|tar so much faster than cp?
Cp does open-read-close-open-write-close in a loop over all files. So reading from one place and writing to another occur fully interleaved. Tar|tar does reading and writing in separate processes, and ...
8
votes
How do you copy directories in Linux in these situations using cp?
cp will always copy the file(s) at the start of the command to the file or directory at the end of the command. The slash doesn't really do much to the arguments, unless the argument is a symlink to ...
6
votes
Copy files in Linux, avoid the copy if files do exist in destination
POSIX solution
Other answers use -u or -n options of cp. Neither of these is required by POSIX; nor is rsync from yet another answer; nor is yes used in one of the comments.
Still, we can reproduce ...
6
votes
How to copy with cp to include hidden files and hidden directories and their contents?
The simplest way is:
cp -r /etc/skel/{.,}* /home/user
The expression {.,}* includes all files and directories (also starting with a dot).
If you don't want use above expression, then you can use the ...
6
votes
Linux issues with cp: -r not specified; omitting directory
cp: -r not specified; omitting directory '/home/administrator/Documents/myfile.10'
This means myfile.10 is a directory. cp won't copy it, unless you explicitly use -r (or equivalent) flag.
For cp in ...
5
votes
How to copy with cp to include hidden files and hidden directories and their contents?
You could use rsync.
rsync -aP ./from/dir/ /some/other/directory/
You can even copy over ssh
rsync -aP ./from/dir/ username@remotehost:/some/other/directory/
There are various flags you can use:
...
5
votes
Copy files in Linux, avoid the copy if files do exist in destination
Use cp -rn <sourcedirname>/. <destdirname>
The r switch makes the copy recursive over the directories.
The n switch (long version no-clobber) ensures existing files are never over-...
5
votes
Copying a file only when it is newer than the destination
yes|cp -ruv /from/* /to/.
yes - Answer yes to all the questions.
r - Recursive
u - update
v - Progress
works like xargs.
I don't know how to explain academically.
How to force cp to overwrite ...
5
votes
How can I recursively copy files by file extension, preserving directory structure?
Another approach
find . -name '*.txt' -exec rsync -R {} /path/to/destdir \;
5
votes
cp only files, skipping directories
cp indeed doesn't have an option to get only files, but as berserck said, you can use find together with cp. In fact, this is really straightforward:
# Copy only files
find /path/to/file -type f -...
4
votes
How to copy symbolic links?
Use -P option, as Ignacio Vazquez-Abrams wrote above. What he didn't mention is -P has no effect without -R. So you need at least cp -RP.
(The site doesn't let me to comment yet so I posted a ...
4
votes
Best way to compare two very large directories in Linux
You can run on both server command like:
find /starting/path -type f -exec sha1sum {} \; >>/tmp/sums
and then compare files /tmp/sums from the servers. This command will calculate hash of ...
3
votes
How do you copy directories in Linux in these situations using cp?
How do I copy /home/src/somedir to /dest/ such that the path always ends up as /dest/somedir, regardless of whether /dest/somedir already exists?
cp -rv /home/src/somedir /dest
We are copying «...
3
votes
Copying many files without stopping on errors on OSX
I use Beyond Compare for exactly this purpose (it's commercial software but has a free trial). You tell it to copy a file, a folder, or a whole drive or any combination and it finishes to the end of ...
3
votes
Recursive, Non-Overwriting File Copy?
My distro didn't have clobber available, so:
echo n | cp -vipr xxx yyy
xxx = source
yyy = destination
v = verbose
i = interactive (prompt to overwrite) | which is why the command is preceded with ...
3
votes
Unix copy for each line in file
To improve performance you might consider do this:
tar -cf - 'cat source.txt' | (cd $dest ; tar -xf -)
Compress files inside your source and extract them will do the job faster than loop!
3
votes
How can I recursively copy files by file extension, preserving directory structure?
I was trying to do the same thing on macOS, but none of the options really worked for me. Until i discovered ditto.
I had to copy many .wav files, and have it skip Video files... So here is what I ...
3
votes
How to copy symbolic links?
On Ubuntu when copying links and files in a directory:
cp --no-dereference --preserve=links
3
votes
Accepted
cp only files, skipping directories
I suggest you to use rsync instead of cp, if you can it has the option -m that will exclude empty folders, example:
rsync -am <origin> <destination>
Explanation of the command in ...
3
votes
Accepted
Permission denied when directly copying from SSHFS to SD
SSHFS is FUSE. Without allow_other the filesystem runs with the privileges of the user that ran it, and only that user can access the filesystem. In your case even root cannot access the filesystem.
...
Community wiki
2
votes
How to copy with cp to include hidden files and hidden directories and their contents?
My solution for this problem when I have to copy all the files (including . files) to a target directory retaining the permissions is: (overwrite if already exists)
yes | cp -rvp /source/directory /...
2
votes
cp -r -l in MacOS (recursive copy preserving hard links)
The macOS Finder copy does it right, preserving hard links, even to a different Volume. But only if it is a simple 1-item copy without joining.
You see if it works right away, because if it does, the ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
cp × 218linux × 120
bash × 35
rsync × 25
command-line × 24
macos × 22
unix × 19
find × 16
file-transfer × 14
ubuntu × 12
symbolic-link × 12
mac × 11
shell × 10
backup × 8
mv × 7
terminal × 6
hard-drive × 5
permissions × 5
filesystems × 5
scp × 5
xargs × 5
hardlink × 5
ssh × 4
usb × 4
centos × 4