Sameer Qayyum
Sameer Qayyum

Reputation: 1

Bash Script - Need to remove files from a directory till the Directory size is Less than 8 TB

Below is the high level scenario for which I need to create a bash script.

Below is a script I am using, but it is going in an infinite loop on the while statement. Need your help in making a script for the above scenario.

while [ "$(du -shb /sasdata2/SAS-USERS/PBU | awk '{print $1}')" -gt 900 ]
do
  find /sasdata2/SAS-USERS/PBU -maxdepth 0 -type f -printf '%T@\t%p\n' | \
  sort -n | head -n 25 | cut -d $'\t' -f 2-  | xargs -d '\n' rm -f
done

Upvotes: 0

Views: 89

Answers (1)

Eran Ben-Natan
Eran Ben-Natan

Reputation: 2615

I think your while condition is wrong. du -b will give you bytes, why comparing to 900?

Beside, Why do you need a loop on du and delete arbitrary 25 files? calculate with du how many bytes you are over 8TB, then ls -ltr and in a loop delete files and sum their size until you reach the over bytes

Upvotes: 1

Related Questions