7

I want to select the first 3,000 files in a folder which contains 10,000 files. How do I select only the first 3,000? And if possible, how can I subsequently select the next 3,000 and then 3,000 after that?

I need to copy them into separate folders, each with 3,000 files.

1 Answers1

11

There is no easy method to do that from a stock file manager. You can do it with Shift + Arrow Up (or Arrow Down) but you will need to select the amount of files yourself.

Command line:

This will copy (cp) 3000 files (-n 3000) to /opt/ (-t "$directory"):

cd /dir/with/files/
find . -maxdepth 1 -type f -print0 | head -z -n 3000 | xargs -0 -r -- cp -t "/opt/" --
  • Change 3000 to another number if needed
  • Change /opt/ to your desctination.
  • Use mv -tf to move instead of cp -t when you know cp does what you want (the mv is needed to clear the 3000 files)
Rinzwind
  • 310,127
  • mv: target './10000000.jpg' is not a directory

    This error shows up when I use the mv command. However it works just fine when I use the cp command.

    Also, is there a way to delete the first 3,000 files in the same way?

    – sidhantunnithan Apr 14 '19 at 11:04
  • Edit - It worked just by changing the flag from -f to -t for the mv command. – sidhantunnithan Apr 14 '19 at 11:13
  • then tf. f to force overwrite ;) And the cp is just to test the 1st group. mv delete the 3000 files. – Rinzwind Apr 14 '19 at 11:21
  • You can probably omit the -- end-of-options argument, since find . will prepend ./ to every filename. You could also consider using printf './%s\0' * in place of the find -maxdepth 1 command – steeldriver Apr 14 '19 at 12:12