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?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • 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 01 '19 at 05: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 01 '19 at 05:48
  • 2
    %d affects how C interprets the string it reads as input. – John1024 Mar 01 '19 at 06:09

1 Answers1

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().

Kusalananda
  • 333,661
  • http://mywiki.wooledge.org/ParsingLs is obligatory here. But also note that M. Wooledge does not touch upon the fact that dates do not have a stable single form (https://unix.stackexchange.com/questions/332228/) and are in locale-determined formats (https://unix.stackexchange.com/questions/115066/). That side note should really be a headline. (-: – JdeBP Mar 01 '19 at 09:42