简体   繁体   English

Bash脚本-需要从目录中删除文件,直到目录大小小于8 TB

[英]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. 以下是我需要为其创建bash脚本的高级方案。

  • A directory on the server - /sasdata2/SAS-USERS/PBU/ 服务器上的目录- /sasdata2/SAS-USERS/PBU/
  • The size of the above directory should not be more than 8 TB 以上目录的大小不应超过8 TB
  • If the size exceeds 8 TB, need to delete the oldest files by 'created/modified' date to get the size back down to 8 TB. 如果大小超过8 TB,则需要在“创建/修改”日期之前删除最早的文件,以将大小减小到8 TB。
  • Several sub-directories in this main directory but nothing should be removed from the sub-directories. 此主目录中有几个子目录,但不应从子目录中删除任何内容。 Only files in the main directory can be removed. 只能删除主目录中的文件。

Below is a script I am using, but it is going in an infinite loop on the while statement. 下面是我正在使用的脚本,但是它在while语句中处于无限循环中。 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

I think your while condition is wrong. 我认为您的条件不对。 du -b will give you bytes, why comparing to 900? du -b会给您字节,为什么要加上900?

Beside, Why do you need a loop on du and delete arbitrary 25 files? 另外,为什么需要在du上循环并删除任意25个文件? 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 用du计算您超过8TB的字节数,然后使用ls -ltr并循环删除文件并求和它们的大小,直到达到超出字节数为止

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM