Copy files given a range of numbers

Hi,
I have about 500 files in a directory. The filenames are numbered i.e. 1.dat , 2.dat , 3.dat ,..., 500.dat .

I have 5 other empty directories where I have to copy the files in different range of numbers, for example, 1.dat to 300.dat in dir1, or 200.dat to 500.dat in dir2, another example 100.dat to 400.dat in dir3, and so on. As you can see I am copying any 300 files in given the range to any directory.

The problem that I am facing is that I cannot specify the range. I tried the command below thinking that each run will select different files in any combination, but they turn out to be the same in all 5 directories. For me what matters the most is that only 300 files in any order or combination should be copied, and they should not strictly follow any serial order of numbers. But the prime requirement is that most of the files in different directories should be different barring few common ones.

cp `ls -ltr | grep ^- | head -300 | tr -s " " | cut -d " " -f9` destination

I am using Linux with BASH.

Use bash sequence expression:

cp {1..200}.dat dir1/
1 Like