I am trying to get the filesize of each file in a folder by using stat in a for loop. I know why the following is going wrong, but I don't have enough knowledge yet on how to fix this. #!/bin/bashfor file in .* *; do     size=$(stat --printf=%s)     echo $sizedoneHowever, as stat is missing the filename, I logically get the following error:stat: missing operandTry 'stat --help' for more information.How do I make sure stat gets the filename of every file (only files) in the folder?EDIT: Coming from a Powershell background, I would use the variable '$_'. Does the Unix shell offer a similar variable?
Filesize in for loop
shell;shell script
You are not giving any argument (filename) to stat leading to that error.You are using variable file to store the each filename and loop over them but you are not giving the filenames as arguments to stat. In a nutshell you need to pass $file (the value of variable file) to stat.Also you can do all you are doing in one line :for file in .* *; do stat -c '%s-%n' $file ; doneIf you insist on the printf formatted output you need an extra \n :for file in .* *; do stat --printf='%s-%n\n' $file ; doneHere i have also added %n so that you can get which file's size is being shown.EDIT :If you want to run this only on files excluding directories you need use an additional check. Here i have used test ([) builtin to check if file is a regular file, if so i.e. [ -f $file ] returns exit code 0  then (&&) run stat on the file :for file in .* *; do [ -f $file ] && stat -c '%s-%n' $file ; done