1

I have a directory of 150 files that I wanted split into sub-directories of 25 files each: 1-25 into dir1, 26-50 into dir2, and so on. How can I accomplish this?

dir1/fre_4_g2_c3654214.h3

dir1/fre_4_g2_c0585433.h3

dir1/fre_4_g2_c3565415.h3

... and so on

and what I'd want to do is

dir1/fre_4_g2_c3654214.h3

dir2/fre_4_g2_c0585433.h3

dir3/fre_4_g2_c3565415.h3
6
  • You should tell us what you already tried!
    – Lucas
    May 16, 2016 at 20:21
  • I looked at this post: unix.stackexchange.com/questions/228494/… but was unable to figure out how to use it for what I'm doing. Specifically, I tried for f in *.dat; do mkdir -p "${f:5:2}"; mv "$f" "${f:5:2}/"; done and got 1 directory with 4 files and another with over 100
    – e1v1s
    May 16, 2016 at 20:22
  • Add some examples of your file names? Are they all similarly named? Do they have file extensions of some sort? etc
    – Grimdari
    May 16, 2016 at 20:25
  • Yes, they're all similarly named and have the same extension
    – e1v1s
    May 16, 2016 at 20:26
  • notice how in the first 3 lines that the parent directory is dir1 and the bottom 3 examples the parent directory is different for each file?
    – e1v1s
    May 16, 2016 at 20:35

1 Answer 1

1

This will sort the files alphanumerically and move the first 25 files into subdirectory dir0, the next 25 into dir1, etc., until all files are moved:

 n=0; for f in *; do d="dir$((n++ / 25))"; mkdir -p "$d"; mv -- "$f" "$d/$f"; done

For those who prefer their commands spread over multiple lines:

n=0
for f in *
do
    d="dir$((n++ / 25))"
    mkdir -p "$d"
    mv -- "$f" "$d/$f"
done

This will work under either ksh or bash.

1
  • 1
    Problem Solved.
    – e1v1s
    May 17, 2016 at 3:28

Not the answer you're looking for? Browse other questions tagged .