Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when I use the command to get the file size in bye using wc -c the command return two values, the size in byte and the file name, ex: the output for wc -c my_file is 322 my_file I need to get only the first value to use it if condition, and I need to use this specific command not any other one.. Any help please, thank you.

What I have tried:

wc -c my_file
Posted
Updated 30-May-20 8:59am

You can pipe awk[^] to isolate the size:
Bash
wc -c my_file | awk '{print $1}'
 
Share this answer
 
Comments
k5054 30-May-20 12:55pm    
I've never used awk, except for the odd time when someone else's solution to a problem I was trying to solve used it, and I could copy and/or adapt to my situation. That being said, I thought there would be a awk one-liner that could get the file size. Googling around didn't provide any answers, but with a little help from a couple of different sites, I came up with this
awk 'BEGIN {sum=0} {sum+=length($0)+1} END {print sum}'

If you were going to stick with an awk only solution (probably out of sheer obstinacy), is there anything better?
phil.o 30-May-20 13:52pm    
If the last line is not an empty line, you may be off by one (I'm not sure, though; this would need to be checked). In this case, this might overcome the issue:
awk 'BEGIN {sum=0} {sum+=length($0)} END {print sum+NR-1}'
There is no option not to print the file name, you'll need to use a pipe as phil.o showed. Some other options:
#use cat
cat my_file | wc -c

#use cut
wc -c my_file | cut -f1 -d ' '

#you could also use stat, if you don't want to use a pipe
stat -c "%s" my_file


If your file is UTF-8, and you need to get a count of the glyphs, then you probably want to stick with wc -m


I'm sure there's a number of other ways to get that answer too.
 
Share this answer
 
v2
I did it using this :
wc -c < my_file
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900