Tag Info

Hot answers tagged

75

sudo cp -rp /home/my_home /media/backup/my_home From cp manpage: -p same as --preserve=mode,ownership,timestamps --preserve[=ATTR_LIST] preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context, links, xattr, all


44

While -R is posix well-defined, -r is not portable! On Linux, in the GNU and BusyBox implementations of cp, -r and -R are equivalent. On the other side, as you can read in the POSIX manual page of cp, -r behavior is implementation-defined. * If neither the -R nor -r options were specified, cp shall take actions based on the type and ...


42

%CPU should be low during a copy. The CPU tells the disk controller "grab data from sectors X–Y into memory buffer at Z". Then it goes and does something else (or sleep, if there is nothing else). The hardware triggers an interrupt when the data is in memory. Then the CPU has to copy it a few times, and tells the network card "transmit packets at memory ...


39

This is not a bug in the cp command. When you enter cp *.pdf, cp never sees the actual wildcards because the wildcards are expanded by bash, not by cp. How will cp know that you have entered only one argument? This is a side effect of bash wildcards and cannot be called a bug.


32

The standard coreutils cp command doesn't support this. There's a Gentoo patch floating around that adds it for different versions, although it's not included in Gentoo anymore for some reason; the version for coreutils 6.10 is in their bugzilla, and I'm sure there are lots of others around. If you don't want to patch cp, you need to use some other command. ...


29

The tradition in unix tools is to display messages only if something goes wrong. I think this is both for design and practical reasons. The design is intended to make it obvious when something goes wrong: you get an error message, and it's not drowned in not-actually-informative messages. The practical reason is that in unix's very early days, there still ...


27

A directory is (conceptually) a special "file" which contains a list of names, and the inode numbers those names point to. Some of names can be subdirectories. There is a special entry .. which points to the parent directory. So, its clear, changing the name of a file is easy: you just change the name in the directory entry, nothing else. This holds whether ...


21

Patrick has it more or less correct, but here's why. The way you copy a file under UNIX works like this: Try to read some (more) bytes from fileA. If we failed to get bytes because we're at (or past) the end of the file, we're done; quit. Otherwise, write the bytes to fileB and loop back to step 1. Knowing that, and knowing it's as simple as that, lets ...


21

You can also use rsync. sudo rsync -a /home/my_home/ /media/backup/my_home/ From the rsync manpage: -a, --archive This is equivalent to -rlptgoD. It is a quick way of saying you want recursion and want to preserve almost everything (with -H being a notable omission). The only exception to the ...


21

If you allow other tools than cp it's surely possible. For a single file you can use pv. It's a small tool providing nice statistics. pv inputfile > outputfile If you have multiple files or directories you can use tar: tar cf - sourceDirectory | pv | (cd destinationDirectory; tar xf -) You can wrap it in a shell function. It's less to type and you ...


21

The nightly version of rsync has a new parameter called --info=progress2 which shows the overall %.


21

Strictly speaking yes, you can always use rsync. From man rsync (emphasis mine): Rsync is a fast and extraordinarily versatile file copying tool. It can copy locally, to/from another host over any remote shell, or to/from a remote rsync daemon. It offers a large number of options that control every aspect of its behavior and ...


19

The difference is that one uses a lowercase "R" and the other uses a capital "R". Beyond that, no difference. Same thing if you use the --recursive long option.


19

You seem to understand what is happening perfectly well. In your example, *pdf indeed expands to file1.pdf file2.pdf this_is_a_folder.pdf. I don't see what's confusing you. cp is doing exactly what it should, you are telling it to copy file1.pdf and file2.pdf into this_is_a_folder.pdf and that is exactly what it is doing. There is no bug, it is working as ...


19

As Celada mentioned, there would be no point to using multiple threads of execution since a copy operation doesn't really use the cpu. As ryekayo mentioned, you can run multiple instances of cp so that you end up with multiple concurrent IO streams, but even this is typically counter-productive. If you are copying files from one location to another on the ...


18

With bash, you could use brace expansion mv blob/a_long_directory_name/{c/x.x,evenmore/y.y}


18

Rsync can be slower than cp in some situations. For example when the destination exists and rsync ends up doing some expensive comparisons for each block, does not find equal blocks and copies the complete source file anyways. Also when destination files don't exist, rsync does not provide any advantage above cp.


17

Good options for copying a directory tree except for some files are: rsync: this is basically cp plus a ton of exclusion possibilities. rsync -a --exclude='.*' /source/ /destination pax: it has some exclusion capabilities, and it's in POSIX so should be available everywhere (except that some Linux distributions don't include it in their default ...


15

You can pass --remove-source-files to rsync to move files instead of copying them. But in your case, there's no point in using rsync, since the destination is empty. A plain mv will do the job as fast as possible. In your case, what could make a difference to performance is the choice of network protocol, if you have a choice among NFS, Samba, sshfs, sftp, ...


15

You can do this with GNU find and GNU mv: find /dir1 -mindepth 2 -type f -exec mv -t /dir1 -i '{}' + Basically, the way that works if that find goes through the entire directory tree and for each file (-type f) that is not in the top-level directory (-mindepth 2), it runs a mv to move it to the directory you want (-exec mv … +). The -t argument to mv lets ...


14

In the unix world, each tool is designed to do one job and do it well. Why would cp worry about outputting progress when another tool like pv does it already? In the same vein, why do so many programs dump stuff to the screen without any pagination? Because there are already tools for that job such as more (or less). Why do most programs that require editing ...


14

Lowercase -r was an older option, introduced in 4.1BSD, which would simply copy all non-directories as files. That is, if it encountered a device or FIFO, it would open it, read the contents, and create a file at the destination with the contents. Uppercase -R was a standardized option (introduced to BSD in 4.4BSD, though earlier versions had it as a ...


14

Why not use rsync --progress [SRC] [DST] Do check the man rsync page because it has a lot of very useful options. -a for archive is a good start, but it depends on your exact requirements. Copying through a pipe will unnecessarily slow down the copy process, especially if it is files based.


13

Yes, by running stat on target file and local file, and get a file size, i.e stat -c "%s" /bin/ls And you get the percentage of data copied by comparing the two value, that's it In a very basic implementation that will look like this: function cpstat() { local pid="${1:-$(pgrep -xn cp)}" src dst [[ "$pid" ]] || return while [[ -f "/proc/$pid/fd/3" ...


12

cp does not know about opened files. So if first user uploads big file and cronjob (or any other process) starts copying this file, it will only copy as much as was already written. You can think about this in this way - cp makes copy of what is currently on the disk, no matter if the file is complete. Otherwise, you could not copy log files for example.


11

First of all, the shell is globbing the '*' for you. Either escape it with \ or use quotes around *.html Like so: find myDir -name "*.html" or find myDir -name \*.html Skip the use of xargs with find's -exec switch: find myDir -name "*.html" -exec cp {} ~/otherDir \; This works because {} takes the place of the file that find found, and is executed once ...


11

Let me start by asking another question: What is the difference between cp and cp -R? Well, without the -R flag, it's only possible to copy files, because it's rather unusual that someone wants to non-recursively copy a directory: A non-recursive copy would just result in a second name for the directory, pointing to directly the same directory structure. ...


11

rsync works the best for showing the progress during the copying progress. ex: rsync -avh --progress sourceDirectory destinationDirectory


11

This could, possibly, be a faster alternative, and you won't clog the network for two days: Take one or two large USB (USB 3 if you have it) or FireWire disks, connect it to the server and copy the files to the disk. Carry the disk to your local machine. Copy the files to the machine.


11

Brace expansion will get the job done. man bash and search for Brace Expansion. cp *.{txt,jpg,png} destination/ EDIT: In keeping with the OP's request, the command above was missing the verbose option: cp -v *.{txt,jpg,png} destination/



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