Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linux shell file size

Tags:

linux

shell

How can I get the size of a file into a variable?

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

gave the size, but how can I store it in a shell variable?

like image 361
webminal.org Avatar asked Feb 10 '10 10:02

webminal.org


People also ask

How do I find the size of a file in Shell?

It would be best to use the stat and other commands under Linux to check the file size. The stat command displays information about the file including its size. Another option is to use the wc command, which can count the number of bytes in each given file.

How do I find the exact file size in Linux?

Using the ls Command –l – displays a list of files and directories in long format and shows the sizes in bytes. –h – scales file sizes and directory sizes into KB, MB, GB, or TB when the file or directory size is larger than 1024 bytes. –s – displays a list of the files and directories and shows the sizes in blocks.

How do I see file size in bash?

Another method we can use to grab the size of a file in a bash script is the wc command. The wc command returns the number of words, size, and the size of a file in bytes.

How do you check the size of a file?

Right-click the file and click Properties. The image below shows that you can determine the size of the file or files you have highlighted from in the file properties window. In this example, the chrome. jpg file is 18.5 KB (19,032 bytes), and that the size on disk is 20.0 KB (20,480 bytes).


2 Answers

filesize=$(stat -c '%s' testing.txt)
like image 73
Ignacio Vazquez-Abrams Avatar answered Oct 26 '22 13:10

Ignacio Vazquez-Abrams


You can do it this way with ls (check the man page for the 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")
like image 45
ghostdog74 Avatar answered Oct 26 '22 12:10

ghostdog74