Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I am trying to perform the following :

printf "Are you sure you want to copy %s (y/n) ? (file bigger than 10 MB) " "$0"

It works fine, nevertheless, I would like to display the actual size of my file, by doing something like :

printf "Are you sure you want to copy %s (y/n) ? (file bigger than 10 MB : %s) " "$0" "ls -l $0 | awk {'print $5'}"

However, I have a failure by doing so. I guess it is not the right way to do it.

share|improve this question

3 Answers 3

printf "Are you sure you want to copy %s (y/n) ? (file bigger than 10 MB: %lu) " "$0" \
  "$(wc -c < "$0")"

parsing the output of ls is not reliable (and you've forgotten the $(...) around it).

share|improve this answer

I assume this is in some form of shell like bash or ksh. X is the file (I also supposed it has space in its name)

printf "Are you sure you want to copy %s (y/n) ? (file bigger than 10 MB : %s) " "$x" \
   $(ls -ld -- "$x" | awk {'print $5'})

should do.

Please note the $( some code ) syntax.

share|improve this answer

If on a GNU system, prefer to use stat instead:

printf "Are you sure you want to copy %s (y/n) ? (file bigger than 10 MB : %s) " "$0" \
    "$(exec stat -Lc '%s' -- "$0")"
  • -L makes stat follow symlinks.
share|improve this answer
    
Note that ksh (as in the question's title) often suggests non-GNU systems. There are a number of different stat commands. The stat -c %s syntax is specific to GNU stat –  Stéphane Chazelas Aug 6 '14 at 13:54
    
@StéphaneChazelas Nicely done edit. Looks like people in unix.stackexchange.com is also strict with -- :) –  konsolebox Aug 6 '14 at 13:55
    
Yes, though unfortunately -- will not always be enough here. For instance it still won't work for a file called -. Which is why I'd recommend to use the portable wc -c < "$0" which has none of those issues (that gives a different result for symlinks though and would fail if you don't have read permission to the file). See also stat -c %s - < "$0" –  Stéphane Chazelas Aug 6 '14 at 13:58
    
Portability is always something that could cause debate so I'd avoid that. But just to tell, I prefer easier or more practical solutions when they're available on a target tool or system. If something's better I'd use it or propose it even if it's less portable as long as it suffice the requirements. In this instance, I see that stat could be better as it may no longer require to read the whole file just to get its size. –  konsolebox Aug 6 '14 at 14:04
    
wc doesn't read the whole size either if the file is a regular file (in all except old obscure implementations like old busybox ones) –  Stéphane Chazelas Aug 6 '14 at 14:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.