Hot answers tagged

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)
Balmipour's user avatar
  • 2,239
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 ...
nnyby's user avatar
  • 1,399
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
SEMbyotic's user avatar
  • 341
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. ...
Matija Nalis's user avatar
  • 2,646
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 ...
Tonny's user avatar
  • 30.4k
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 ...
jcaron's user avatar
  • 1,788
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 ...
u1686_grawity's user avatar
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....
stew's user avatar
  • 230
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 ...
Pum Walters's user avatar
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 ...
Eric Renouf's user avatar
  • 1,714
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 ...
Kamil Maciorowski's user avatar
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 ...
simhumileco's user avatar
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 ...
Kamil Maciorowski's user avatar
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: ...
RoshP's user avatar
  • 51
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-...
taatparya's user avatar
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 ...
Felippe Silvestre's user avatar
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 \;
Marc's user avatar
  • 151
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 -...
jpenna's user avatar
  • 199
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 ...
1234ru's user avatar
  • 201
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 ...
Romeo Ninov's user avatar
  • 5,712
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 «...
Ángel's user avatar
  • 1,268
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 ...
Tim D's user avatar
  • 31
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 ...
Shōgun8's user avatar
  • 199
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!
Ami Meidar's user avatar
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 ...
Benjamin McGuire's user avatar
3 votes

How to copy symbolic links?

On Ubuntu when copying links and files in a directory: cp --no-dereference --preserve=links
Greg's user avatar
  • 317
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 ...
berserck's user avatar
  • 258
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. ...
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 /...
Rajneesh Gadge's user avatar
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 ...
Flexpert's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible