How do I split a directory into multiple directories without corrupting the individual files?

Here’s my problem:
I have a directory sized 24.7 GB containing 18,854 jpeg files on my computer, and I want to burn it to DVDs, so I need to split it into multiple directories, therefore each one must not exceed 4.7 GB. Also, I don’t want to corrupt any of the pictures.
Can you help me?

I’m not sure if this is the best way, but it should be fairly quick and effective. Paste the following line in the terminal after you have cd’d into the directory containing your jpegs:

MAX=4000000000; TOTAL=0; for file in *.jpg; do TOTAL=$((TOTAL+$(stat -c %s -- "$file"))); DIR=$(($TOTAL/$MAX)); mkdir -p $DIR; ln "$file" "$DIR/$file"; done

The above command will make numbered directories containing copies (actually hard links) of your jpeg files. No directory will contain more than $MAX bytes worth of files.

One thing to be aware of is that hard links are actually aliases for the original files – if you alter them, the changes will show up in both the link AND the original file. So be careful with them. It is probably best to just delete them when you are done.

Also, I’m assuming you don’t have any numbered directories in your current directory full of jpeg files. If you do, then don’t use the above command because it will mix the copies/aliases with the existing files in the subdirectories.

You can use the following command to verify the sizes of the directories after you’ve run the above command:

du -chd 1 *
2 Likes