簡體   English   中英

linux shell文件大小

[英]linux shell file size

我想將文件大小變為變量? 怎么做?

ls -l | grep testing.txt | cut -f6 -d' '

給出了大小但是如何將它存儲在shell變量中?

filesize=$(stat -c '%s' testing.txt)

你可以用ls這樣做(檢查-s的意思的手冊頁)

$ var=$(ls -s1 testing.txt|awk '{print $1}')

或者您可以將stat-c '%s'

或者你可以使用find(GNU)

$ var=$(find testing.txt -printf "%s")
size() {
  file="$1"
  if [ -b "$file" ]; then
    /sbin/blockdev --getsize64 "$file"
  else
    wc -c < "$file"  # Handles pseudo files like /proc/cpuinfo
    # stat --format %s "$file"
    # find "$file" -printf '%s\n'
    # du -b "$file" | cut -f1
  fi
}

fs=$(size testing.txt)
size=`ls -l | grep testing.txt | cut -f6 -d' '`

您可以使用命令wc獲取文件大小(以字節為單位),這在Linux系統中很常見,因為它是GNU coreutils的一部分

wc -c < file

在bash腳本中,您可以將其讀入如下變量:

FILESIZE=$(wc -c < file)

來自man wc

-c, --bytes
       print the byte counts
    a=\`stat -c '%s' testing.txt\`;
    echo $a

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM