linux shell file size

前端 未结 6 1654
感动是毒
感动是毒 2020-12-15 12:35

I want to get size of file into variable ? How to do that?

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

gave the size but how to store it in

相关标签:
6条回答
  • 2020-12-15 12:56
        a=\`stat -c '%s' testing.txt\`;
        echo $a
    
    0 讨论(0)
  • 2020-12-15 12:59
    size=`ls -l | grep testing.txt | cut -f6 -d' '`
    
    0 讨论(0)
  • 2020-12-15 13:03

    you can do it this way with ls (check man page for meaning of -s)

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

    Or you can use stat with -c '%s'

    Or you can use find (GNU)

    $ var=$(find testing.txt -printf "%s")
    
    0 讨论(0)
  • 2020-12-15 13:05
    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)
    
    0 讨论(0)
  • 2020-12-15 13:07

    You can get the file size in bytes with the command wc, which is fairly common in Linux systems since it's part of GNU coreutils

    wc -c < file
    

    In a bash script you can read it into a variable like this:

    FILESIZE=$(wc -c < file)
    

    From man wc:

    -c, --bytes
           print the byte counts
    
    0 讨论(0)
  • 2020-12-15 13:12
    filesize=$(stat -c '%s' testing.txt)
    
    0 讨论(0)
提交回复
热议问题