2018年6月11日 星期一

[Linux 常見問題] Linux delete file with size 0

Source From Here 
Question 
How do I delete a certain file in linux if its size is 0. I want to execute this in an crontab without any extra script. 

How-To 
This will delete all the files in a directory (and below) that are size zero: 
// -size n[cwbkMG]: File uses n units of space.
// -print0: print the full file name on the standard output, followed by a null character (instead of the newline character that -print uses).
// xargs -0: Input items are terminated by a null character instead of by whitespace
# find /tmp -size 0 -print0 | xargs -0 rm --

If you just want a particular file; 
  1. // -s: Detect file exist or not and should be not empty!  
  2. if [ ! -s /tmp/foo ] ; then  
  3.   rm /tmp/foo  
  4. fi  
Supplement 
How to delete many 0 byte files in linux? 
VBird - Ch12 - 學習 Shell Scripts

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...