Hot answers tagged cp
36
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 ...
28
%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 ...
25
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. ...
24
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 ...
21
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 ...
18
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 ...
13
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 ...
12
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 ...
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
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 ...
11
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 ...
9
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.
9
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 ...
8
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 ...
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
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); ...
8
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.
...
8
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 ...
7
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/
$
7
cp is a part of coreutils, therefore it is present everywhere. furthermore, it primarily was designed to copy files inside one computer.
rsync isn't a part of coreutils, it isn't present even on the default environment. moreover it was primarily designed to transfer files over network. Also rsync has more dependencies comparing to coreutils, however this ...
7
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 ...
7
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. ...
7
cp doesn't know what other programs may have the files open. There's no magic in cp. The design of unix purposefully avoids putting any kind of locks on files unless there's a compelling reason (compelling meaning the kernel needs it). On this topic, see Does redirecting output to a file apply a lock on the file?
Such situations, where a file is produced by ...
7
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 ...
7
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.
6
if you're using bash you should be able to use
mv apple.[^0]* /other/directory/
this will move any files of which the extension does not start with 0 ( "^" at the beginning of [ ] means "not" in bash). If you're sure there's only one character as an ending you could also use
mv apple.[^0] /other/directory/
and if you have to make sure only files which ...
6
It is the shell that is providing the illusion that a symlink pointing to a directory is a real directory rooted at the location of the symlink. cd is by necessity a shell builtin, and tab-completion is also a function performed by the shell. Since cd is performed by the shell it knows about the symlink and can maintain the illusion. Most Unix commands ...
6
It seems like you want to do a dir sync job.
Because the -u, --update option of cp
copy only when the SOURCE file is newer than the destination file or when the destination file is missing
So you can add a cronjob such as cp -auv SOURCEDIR/* DESTDIR which will copy those files whose modification time have changed. That means DESTDIR will ...
6
With zsh, you could do:
n=0; cp 00802_Bla_Aquarium_?????.jpg(^e:'((n++%4))':) /some/place
POSIXly, same idea, just a bit more verbose:
# put the file list in the positional parameters ($1, $2...).
# the files are sorted in alphanumeric order by the shell globbing
set -- 00802_Bla_Aquarium_?????.jpg
n=0
# loop through the files, increasing a counter at ...
6
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" ...
Only top voted, non community-wiki answers of a minimum length are eligible
