Home > Software engineering >  split directory with 10000 files into 2 directories
split directory with 10000 files into 2 directories

Time:12-08

I have directory /logos which contains approximately 10000 png images. Can you please suggest some script to make two new folders /logos-1 and /logos-2 each one with half of the images from initial folder? (or if it will simplify task, lets make first folder contain first 5000 images, second folder the remaining images)

Thank you in advance <3

CodePudding user response:

One approach could be to iterate over the files in the folder, keep and counter and move they files the other directory on each iteration:

counter=0
mkdir -p logos-0
mkdir -p logos-1
for file in logos/*
do
  [ -e "$file" ] || continue
  echo mv "$file" "logos-$((counter  %2))/"
done

Remove the echo if the mv commands looks appropriate.

CodePudding user response:

You can use rename, a.k.a. Perl rename and prename for that. I assume you don't really want the leading slashes and you aren't really working in the root directory - put them back if you are.

rename --dry-run -p -N 01 '$_ = join "", "logos-", $N  %2 1, "/$_"' *.png 

Sample Output

'1.png' would be renamed to 'logos-2/1.png'
'10.png' would be renamed to 'logos-1/10.png'
'2.png' would be renamed to 'logos-2/2.png'
'3.png' would be renamed to 'logos-1/3.png'
'4.png' would be renamed to 'logos-2/4.png'
'5.png' would be renamed to 'logos-1/5.png'
'6.png' would be renamed to 'logos-2/6.png'
'7.png' would be renamed to 'logos-1/7.png'
'8.png' would be renamed to 'logos-2/8.png'
'9.png' would be renamed to 'logos-1/9.png'

You can remove the --dry-run if the output looks good. The -p means it will create any necessary directories/paths for you. If you aren't familiar with Perl that means:

"Set N=1. For each PNG file, make the new name (which we must store in special variable $_) equal to the result of joining the word logos- with a number alternating between 1 and 2, with a slash followed by whatever it was before ($_)."


Using this tool confers several benefits:

  • you can do dry runs
  • you can calculate any replacement you like
  • you don't need to create directories
  • it will not clobber files if multiple inputs rename to the same output

On macOS, use homebrew and install with:

brew install rename
  • Related