1

I was checking what exactly each field is in the output of ls -l. The example in this post solves my question. But again I'm wondering what type of each field is. The strings are obvious. But how about the numbers? Like 10, 2048, are they integers or strings? Is there any way I can check the type of each field?

  • ls -l produces a string. If you feed the output of ls -l to bash or awk or another program, how that program interprets the strings is up to it. – John1024 Mar 1 at 5:43
  • Ok, I was confused because I saw someone read the output using scanf ("%s, %s, %s, %s, %d, %s, %s, %s, %s, some_variable, some_variable, ...") Like why the middle one is %d instead of %s – user8314628 Mar 1 at 5:48
  • 2
    %d affects how C interprets the string it reads as input. – John1024 Mar 1 at 6:09
2

The text produced by commands in the terminal are text strings. Viewing raw binary data in the terminal, without converting it to some text representation, generally has a tendency to produce "garbled output" (for example, when running cat on a compressed file) and may in some cases even put the terminal in a unusable state.

The numbers in the output of ls -l are text strings.

To read text as integers into e.g. a C program, the program would have to read them as text strings and later convert them to integers using e.g. strtol(), or use scanf() (or equivalent) with the correct format string.

Shell tools that read integers, such as awk for example, generally already do this conversion behind the scene according to the rules implemented by the tool.


Side note: A C program should not parse the numbers from the output of ls -l. It should call stat() or lstat() on the files returned by readdir().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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