Running MacOS in a terminal which is essentially BSD, hence posting my question here instead of askdifferent. I want to trim my image dataset down in subsequent steps, by deleting files randomly. Some directories have over a 1 million jpgs. My data is in master directory with subdirectories that is only a maxdepth of 1:
-master -data1 image.jpgs -data2 image.jpgs -data3 image.jpgs -data4 image.jpgs ... and so forth
I found this link:
... and came up with:
for f in *.jpg; do find "$f" -type f -print0 | sort -R | tail -n +50001 | xargs -0 rm; done
While it does work, I'd like for it to do this recursively for subdirectories, so I don't have to manually do it for every directory. So my questions/requests are:
- Can I optimize this somehow to speed it up?
- Will sort/tail return an error when it encounters a directory with fewer than 50,000 files?