I was using the file command to gather information about some text files (well, XML files, but the XML info did not interest me), but there was one bit of information that I missed in the output: whether the file was indented with tabs or spaces.

Is there any command that will give me that? Or any sort of configuration I can use to get file to display that information?

I know I can use grep -l -P "^\x09", for example, but I'm looking for something that already does what I need, and, presumably, already went through all the corner cases I haven't yet thought of.

  • 1
    The XML format does not care whether the file is indented with spaces or tabs, or not at all, or even if the whole document lives on a single line. – Kusalananda Jun 23 at 21:33
  • I'm not sure the spec is complete; does the existence of one tab-indented line count, or do they all have to be tabbed over? If spaces, how many spaces? – Jeff Schaller Jun 23 at 22:53
  • @Kusalananda The XML tools don't care. The person reading XML, which is intended to be human-readable, does. – Daniel C. Sobral Jun 24 at 3:06
  • I don't think you would be able to find a program for the sole purpose of telling you if a file has tabs in it or not, however to make it simple, I can suggest cat -A <file> | grep '\^I' – GypsyCosmonaut Jun 24 at 5:50

To detect indentation properly in a structured text format, the file command would have to implement a proper parser for that format. The occurrence of the character sequence \n␣ (newline followed by a space) is not enough to say that the file is indented by spaces as that character sequence could occur inside an XML attribute.

I don't think literal tabs can occur unencoded in XML attributes, but I'm not sure about CDATA sections.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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