tags:

views:

82

answers:

4

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 shell variable?

+7  A: 
filesize=$(stat -c '%s' testing.txt)
Ignacio Vazquez-Abrams
+2  A: 
size=`ls -l | grep testing.txt | cut -f6 -d' '`
Burntime
+1  A: 

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")
ghostdog74
+1  A: 
size() {
  file="$1"
  if [ -b "$file" ]; then
    /sbin/blockdev --getsize64 "$file"
  else
    echo $(stat --format %s "$file")
    #echo $(find "$file" -printf %s)
    #echo $(du -b "$file" | cut -f1)
  fi
}

fs=$(size testing.txt)
pixelbeat