Monday, June 6, 2022

[SOLVED] Move into a subfolder all 1-level folders inside a parent directory if a foldersize is set

Issue

I have a directory (anime) that have many folders with different folder sizes.
I want to move all that folders inside a subfolder called "over40gb" if these folders have a size over 40gb

I test a script but this works only if I want to move one folder (called 'tomove') into another one folder (called 'over40gb')

if [ $(du -m -s tomove/ | cut -f1) -gt 40000 ] ; then 
  mv tomove/ $HOME/over40gb/
fi

Is possible to extend this script for all folders (just 1-level folders) inside anime directory into over40gb subfolder ?

I start from this situation

anime
  |
  + tomove1 (30 gb)
  + tomove2 (45 gb)
  + tomove3 (40 gb)

And I want to move in this way

anime
  |
  + tomove1 (30 gb)
  |
  + over40gb
       |
       + tomove2 (45 gb)
       + tomove3 (40 gb)

Solution

if you only want level one folders then it's as simple as this

z=$HOME/theanime/
mkdir -p $HOME/over40gb/
for x in $(ls -1 --color=never -d ${z}*/); do
  y=$(du --max-depth=0 --block-size=1M $x | awk '{print $1}')
  if [ $y -ge 40000 ]; then
    mv ${x} $HOME/ofer40gb/
  fi
done

if you need anything else than level one folders i'm going to need to edit this answer.

the "over40gb" folder is in the same directory as the tomove directory's but you could just change that



Answered By - tomas
Answer Checked By - Willingham (WPSolving Volunteer)