Hot answers tagged

187

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


61

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 c sourceDirectory | pv | tar x -C destinationDirectory You can wrap it in a shell function. It's less to type and you get ...


55

Rsync has a flag called progress2 which shows the overall %. rsync --info=progress2 source dest


54

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


49

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


47

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


41

Recursive means that cp copies the contents of directories, and if a directory has subdirectories they are copied (recursively) too. Without -R, the cp command skips directories. -r is identical with -R on Linux, it differs in some edge cases on some other unix variants. By default, cp creates a new file which has the same content as the old file, and the ...


41

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.


39

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


38

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


34

Use this instead: cp -R inputFolder/. outputFolder This works in exactly the same way that, say, cp -R aaa/bbb ccc works: if ccc doesn't exist then it's created as a copy of bbb and its contents; but if ccc already exists then ccc/bbb is created as the copy of bbb and its contents. For almost any instance of bbb this gives the undesirable behaviour that ...


33

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


33

Is there any reason why people resist using find's -exec? It's very handy. find . -name '*.csv' -exec cp --parents \{\} /target \; Know your tools. ;-)


32

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


32

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


30

Diffs can be more complicated than just comparing one file versus another. The can compare entire directory hierarchies. Consider the example that I want fix a bug in GCC. My change adds a line or two in 4 or 5 files and deletes a handful of lines in those and other files. If I want to communicate these changes to someone, potentially for inclusion into ...


28

This isn't working because the command date returns a string with spaces in it. $ date Wed Oct 16 19:20:51 EDT 2013 If you truly want filenames like that you'll need to wrap that string in quotes. $ touch "foo.backup.$(date)" $ ll foo* -rw-rw-r-- 1 saml saml 0 Oct 16 19:22 foo.backup.Wed Oct 16 19:22:29 EDT 2013 You're probably thinking of a different ...


25

*.* only matches filenames with a dot in the middle or at the end. For example: abc.jpg def. * matches the filenames above, plus the names which don't have a dot at all. for example: data


25

Your . trick can only be used when you're copying a directory, not a file. The -T option works with both directories and files. If you do: cp srcfile destfile and there's already a directory named destfile it will copy to destfile/srcfile, which may not be intended. So you use cp -T srcfile destfile and you correctly get the error: cp: cannot ...


24

install not only copies files but also changes its ownership and permissions and optionally removes debugging symbols from executables. It combines cp with chown, chmod and strip. It's a convenient higher-level tool to that accomplishes a common sequence of elementary tasks. install is a BSD command (added in 4.2BSD, i.e. in the early 1980s). It has not ...


23

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


23

The syntax for scp is: If you are on the computer from which you want to send file to a remote computer: scp /file/to/send username@remote:/where/to/put Here the remote can be a FQDN or an IP address. On the other hand if you are on the computer wanting to receive file from a remote computer: scp username@remote:/file/to/send /where/to/put scp can ...


22

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


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

The following copies the first 1000 files found in the current directory to $destdir. Though the actual files depends on the output returned by find. $ find . -maxdepth 1 -type f |head -1000|xargs cp -t $destdir


21

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


21

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


20

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


20

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


20

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



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