Tag Info

Hot answers tagged

39

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 ...


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.


38

%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 ...


30

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. ...


28

I don't like the idea to overwrite binaries from coreutil when there are simpler solutions, so here are mine: rsync: Rsync copies files and has a -P switch for a progress bar. So if you have rsync installed, you could use a simple alias in your shells dotfile: alias cp='rsync -aP' The downside is, that rsync is a little bit slower than cp, but you should ...


26

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 ...


22

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 build a patched cp and mv which then both support the -g switch to show progress. There are instructions and patches at this page. However: The page instructs you to do $ sudo cp src/cp /usr/bin/cp $ sudo cp src/mv /usr/bin/mv which overwrites the original cp and mv. This has two disadvantages: Firstly, if an updated coreutils package arrives at ...


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

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 ...


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.


15

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 ...


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 ...


13

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 ...


12

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 ...


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

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, ...


11

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 ...


11

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.


10

You should try rsync instead of cp: rsync -avz linux_path /mnt/windows_share/ and crontab instead of the perl loop: crontab -e and add the following line to it: * * * * * rsync -avz linux_path /mnt/windows_share/ It's going to be executed every minute, and if that's an option in your case, it's more robust than the while loop.


10

You should put an alias in your start up script: alias cp='cp -i' You can put this directly in ~/.bashrc, but I have in my ~/.bashrc: if [ -f ~/.bash_aliases ]; then . ~/.bash_aliases fi and in ~/.bash_aliases I have: alias realias='source ~/.bash_aliases' alias cp='cp -i' alias rm='rm -i' and when I have added/changed things to that file I do ...


9

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 ...


9

You can also let the shell do the iterations with a for loop. This is the most basic way that doesn't rely on external tools like parallel or xargs, and I find it easiest to understand: for name in $(printf "s%02i " $(seq 1 50)); do mkdir $name cp test.txt $name done There are a million other ways to write this, e.g.: for name in $(seq -w 1 50); ...


9

Derobert's Why unix mv program doesn't need -R (recursive) option for directories but cp does need it? basically answers your question: copying or removing a regular file is different from doing the same operation with a directory, because for a directory you have to process all the files contained therein. Hence the operation is fundamentally different. ...


9

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" ...


8

If fileA.big is grown during the copy, the copy will include the data that was appended. If the file is truncated shorter than where the copy is currently at, the copy will abort right where its at and the destination file will contain what was copied up to the time it aborted.


8

It's nothing to do with smbfs, cp always requires the -r (recursive) flag to copy a directory. You should get the same if you try to cp .vim /tmp/: $ cp .vim /tmp/ cp: omitting directory `.vim' zsh: exit 1 cp .vim /tmp/ $ cp -r .vim /tmp/ $


8

For cp, the destination is the last argument on the command line. You have specified 2/g as the last argument. Before cp is executed, the command parameters are expanded. 1/* expands to 1/a 1/b 1/c. 2/* expands to 2/f 2/g. The final executed command is cp -r 1/a 1/b 1/c 2/f 2/g, which will copy all the arguments (except the last one) to 2/g. If you are ...


8

What am I doing wrong? That's OK. find finds already copied files in new and tries to copy them again, therefore a warning message is displayed. can I use "+" with this command so that files are copied in a single "bundle"? There are thousands of files! Yes, but you need to modify you command this way: find /var/www/import -iname 'test*' -newer ...



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