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/
Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
JefferyLR
  • 119
  • 6

1 Answers1

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
  • 3
    You're right, it's horrible. [Don't loop over `find`'s output](https://unix.stackexchange.com/q/321697) and quote your vars... – don_crissti Oct 05 '17 at 20:50