Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Possible Duplicate:
How to move 100 files from a folder containing thousands?

Is it possible to copy only the first 1000 files from a directory to another?

Thanks in advance

share|improve this question

marked as duplicate by Michael Mrozek Jan 16 '12 at 14:24

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

What do you mean by "first"? Did you look at this related question ? – Mat Jan 16 '12 at 12:14
If a folder has 50000 files copy only the first 1000. I'll look at the question thnx. – chchrist Jan 16 '12 at 12:24
What to you mean by "first"? Alphabetical? By time changed/accessed? Any set of 1000? – Mat Jan 16 '12 at 12:25
Any without criteria – chchrist Jan 16 '12 at 12:31
Sounds like you are trying to copy files using batches. – Russell Jan 16 '12 at 12:50
up vote 21 down vote accepted

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
share|improve this answer
Unless I would write a script, I would probably go with this one. – Stéphane Gimenez Jan 16 '12 at 15:08
@chchrist, Mark this as answer. – shgnInc May 4 '14 at 6:09
doesn't work to me if paths has spaces in it – miguelfg Jan 20 at 16:39

A pure shell solution (which calls cp several times).

for i in ${srcdir}/*; do
  [ $((N--)) = 0 ] && break
  cp -t "${dstdir}" -- "$i"

This copies a maximum number of $N files from $srcdir to $dstdir. Files starting with a dot are omitted. (And as far as I know there's no guaranty that the set of chosen files would even be deterministic.)

share|improve this answer

The following scary 1-liner:

perl -MFile::Copy -e 'opendir(DIR,$ARGV[0]);$n=1000; copy($_,"$ARGV[1]/$_") while($n-- && readdir(DIR)) 

works for file containing spaces, quotes, etc., which tend to break shell-based solutions (short of $IFS contortions). 'Course if your file names are behaved, shell is fine.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.