Linux Bash Shell Script - サイズが0のファイルを見つけて削除します。

Linux Bash Shell Script - サイズが0のファイルを見つけて削除します。

ファイルのリストは変数に保存されますREMOVEFILES。スクリプトはファイルのリストを1つずつ確認し、ファイルがZERO十分に大きい場合は削除する必要があります。問題を解決するのに役立ちます。

#!/bin/bash  
REMOVEFILES=target_execution.lst,source_execution.lst;  
echo $REMOVEFILES  
for file in $(REMOVEFILES)  
do  
        echo "$file";  
        if [[ -f "$file" ]]; then  
        find $file -size 0c -delete;  
        else  
         :  
        fi  
done  

./a.sh: line 3: REMOVEFILES: command not found

ベストアンサー1

で、zshこれらのファイルに1行に1つのファイルパスが含まれていてサイズが0の通常のファイルである場合、.lstそのファイルは削除したいファイルです(ファイル自体ではありません)。.lst

#! /bin/zsh -
  
lists=(
  target_execution.lst
  source_execution.lst
)

rm -f -- ${(f)^"$(cat -- $lists)"}(N.L0)

それでbash、あなたはいつもそれをすることができます

#! /bin/bash -
lists=(
  target_execution.lst
  source_execution.lst
)
to_remove=()
process() {
  [[ -f "$1" && ! -L "$1" && ! -s "$1" ]] && to_remove+=( "$1" ) 
}
for file in "${lists[@]}"; do
  readarray -c 1 -C process < "$file"
done
rm -f -- "${to_remove[@]}" 

おすすめ記事