문제

I couldn't find this question here. How do compare file sizes of two files, then do something only if the two files are of different sizes, in Bash?

도움이 되었습니까?

해결책 2

To complement @Steve Bennett's helpful answer:

The stat options indeed vary across platforms; it seems that the POSIX standard doesn't mandate a stat utility (only a library function).

Here's a bash function that works on both Linux and BSD-derived OSs, including OSX:

# SYNOPSIS
#   fileSize file ...
# DESCRIPTION
#   Returns the size of the specified file(s) in bytes, with each file's 
#   size on a separate output line.
fileSize() {
  local optChar='c' fmtString='%s'
  [[ $(uname) =~ Darwin|BSD ]] && { optChar='f'; fmtString='%z'; }
  stat -$optChar "$fmtString" "$@"
}

Sample use:

if (( $(fileSize FILE1.txt) != $(fileSize FILE2.txt) )); then
  echo "They're different."
fi

다른 팁

Bash doesn't seem to have this as a built in, but you can use stat.

if [ $(stat -c %s FILE1.txt) -ne $(stat -c %s FILE2.txt) ]; then 
   echo "They're different."
fi

The exact arguments to stat may vary on the OS.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top