Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Listing directories with tab as delimiter

When we list files in unix using ls -l command the output is a table with space as a separator, for example the following

(jupyter-lab) ➜  mylab ls -l
total 2
drwxr-sr-x. 2 hs0424 ragr  0 Feb  1 12:17 A bad directory
drwxr-sr-x. 2 hs0424 ragr  0 Feb  1 12:18 A very bad directory

I want to convert to a tab separated file (.tsv), just changing spaces to \t, such as ls -l | sed -E 's/ +/\t/g' would not work since filenames contain spaces. Do we have better solution ?

Hard to show expected output with tabs but if we use \t as a replacement of tab, I want something as follows,

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

(jupyter-lab) ➜  mylab ls -l
total 2
drwxr-sr-x.\t2\ths0424\tragr\t0\tFeb 1\t12:17\tA bad directory
drwxr-sr-x.\t2\ths0424\tragr\t0\tFeb 1\t12:18\tA very bad directory

(Edit 1)
We can assume access to GNU tools

>Solution :

Use GNU find -printf or stat, either of which let you provide an arbitrary format string, instead of ls.

find . -mindepth 1 -maxdepth 1 -printf '%M\t%y\t%g\t%G\t%u\t%U\t%f\t%l\n'

or

stat --printf='%A\t%G\t%g\t%U\t%u\t%n\n' *
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading