0

Every folder here got about 1000 items, and I wish to "move" the last 100 items to another directory by creating the same folder name and store it.

Example:

/original/folder1/
/original/folder2/
       ...
/original/folder50/

Ibwish to move the last 100 items from every folder above to destination below which is not created at the very beginning.

/dest/folder1/
/dest/folder2/
      ....
/dest/folder50/
  • 1
    by what criteria is last defined? Files are placed in folder in arbitrary order – RomanPerekhrest Oct 5 '17 at 9:49
  • How to get the last N files in a directory? should get you started... – don_crissti Oct 5 '17 at 10:48
  • @RomanPerekhrest the last 100 files in every single folder – JefferyLR Oct 5 '17 at 16:43
  • 1
    "Last" implies an ordering. What ordering are you using? Date? Name? Size? Something else? – roaima Oct 5 '17 at 20:50
0

This is a horrible solution. But it will work. Pay attention to the paths and what cut field to set based on your directory structure.

for i in `find original/ -type d | grep -v 'original/$'`;
  do foldernames=`echo $i | cut -d "/" -f 3-`; 
  mkdir -p dest/$foldernames ; 
  ls $i | tail -n 100 | xargs -i -t mv $i/{} dest/$foldernames ; 
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.