E
E
Egor Samusev2012-08-08 22:36:48
bash
Egor Samusev, 2012-08-08 22:36:48

How to copy several thousand files from one directory to another?

Simple task: copy files from /tmp/src to /tmp/dst. /tmp/src contains several thousand files.
On FreeBSD, the usual solution doesn't work:
cp -p /tmp/src/* /tmp/dst
Error: "/sbin/cp: Argument list too long." So I decided like this:
find /tmp/src -type f -exec cp -p {} /tmp/dst \;
Is it possible to do something differently or easier? How about in other *nixes?

Answer the question

In order to leave comments, you need to log in

8 answer(s)
A
Alexey Huseynov, 2012-08-08
@kibergus

Don't use *, which the shell will expand into an argument list. Copy the entire directory.
cp -raT /tmp/src /tmp/dst The
key parameter is -T. Make it work properly even if the dst directory already exists

G
gaelpa, 2012-08-08
@gaelpa

rsync?

I
Iliapan, 2012-08-08
@Iliapan

Umm... pack the folder and then unpack

C
ColorFlow, 2012-08-09
@ColorFlow

archive, copy, unzip

@
@sledopit, 2012-08-09
_

If you already use find, then it's better like this:

find /tmp/src -type f -exec cp -p -t /tmp/dst {} \+
works much faster than your version.
because with \; at the end, a new process will be launched for each file, and in the case of \+, one process will be launched for many files. find will sort out how many files it can cram into its arguments at one time.
ps. This is the first time I see cp in /sbin.

C
cronfy, 2012-08-09
@cronfy

If find is better with xargs.
In general, it worked great in FreeBSD:
cp -a src/ tmp/
Notice the slashes at the end. Works with both the existing dst/ folder and the missing one.

A
amarao, 2012-08-09
@amarao

man xargs
I don't remember the exact parameters, but something like this:
ls -1|xargs -n 30 cp -d destignation
The key is the maximum number of arguments per entry (-n) and -d for cp (allows you to swap dest and src).

S
srosts, 2013-11-21
@srosts

Create a directory /tmp/dst
# mkdir /tmp/dst
go to the created directory
# cd /tmp/dst
copy all files from the directory /tmp/src to the current directory /tmp/dst
# cp /tmp/src/* .
Folders are not copied, which was not the task.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question