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

沒有留言:

張貼留言

[ JS 常見問題 ] Javascript Date output formatting

Source From  Here   Question   I need to  capture the Now() date and time in the YYYY/mm/dd hh:m:sec format . It needs to be in GMT time. Ho...